Admin Forum:AJAX cleans away JavaScript?

Hello! I'm an admin on Locomotive Wiki and I have a question concerning the use of AJAX and JavaScript at the same time.

A little while ago I asked here for help with adding a tag to URLs (admins' user pages) for use in the recent changes. Rappy gave me some cool jQuery, something along the lines of this if memory serves me correctly (personal test):

$('[href="/wiki/User:Example"],').attr('title', 'This user is an administrator');

I've now modified this text (with some help from others), to run better and include more admins. The problem now (though I have known about it for awhile) is that the AJAX refresher deletes the tags upon activation. Is there a way of getting around this problem? I did good job of appending " [admin] " to the links in question in my personal js (if I do say so myself), but I wasn't thinking since that being javascript as well: the ajax deleted it with the tags. As before any help with this problem? :) STARFLEET  ACADEMY  01:26, May 12, 2012 (UTC)


 * Look for ajaxcallagain in the ajax RC docs- that'll fix it. -- Kangaroopowah  ( Talk ) 01:30, May 12, 2012 (UTC)


 * Come again? n00b to codes. :P STARFLEET  ACADEMY  01:37, May 12, 2012 (UTC)


 * The technical reason why they disappear is because javascript (and thus jQuery) apply only to the elements on the page at the time. Ajax refresh gets the new list of recent changes and puts it in place of the elements in the body of the page - thus, it overwrites the changes made by your JS.  is an array of functions that are run every time ajaxrc refreshes the page. So by adding your function to that array, it will re-apply all of your changes to the new elements on the page.


 * Basically place ajaxCallAgain = [$('[href="/wiki/User: "],').attr('title', 'This user is an administrator')]; above your import to AjaxRC. -- Kangaroopowah  ( Talk ) 01:41, May 12, 2012 (UTC)


 * That won't work, ajaxCallAgain requires a function reference. You put in the name of your function, like.

Okay, I've added ajaxcallagain. But it still won't work: See here. I've now broken the whole thing! XD STARFLEET  ACADEMY  01:58, May 12, 2012 (UTC)


 * You don't want a string containing the function name, just the actual function name. Functions are objects in javascript, so you can use them like they're variables.


 * Monchoman, I don't understand. What exactly am I doing wrong? :/ STARFLEET  ACADEMY  03:22, May 12, 2012 (UTC)


 * Try ajaxCallAgain = [AdminTag,FounderTag]; (with the quotes in there, they are just strings, not function references) — Mathmagician 03:36, May 12, 2012 (UTC)


 * Sigh, that didn't work either Mathmagician. It completely breaks the script. To the point where the text doesn't highlight and the ajax refresher doesn't display. To contain this here a bit more... here's the import:

 // Ajax auto-refresh var ajaxPages = ['Special:RecentChanges','Special:WikiActivity']; var AjaxRCRefreshText = 'Auto-refresh'; ajaxCallAgain = [AdminTag,FounderTag]; importScriptPage('AjaxRC/code.js', 'dev'); // END of ajax auto-refresh


 * Any other thoughts? STARFLEET  ACADEMY  03:59, May 12, 2012 (UTC)

Sorry, I didn't notice you had your functions wrapped by document.ready, as in $; -- Try replacing your entire common.js with this: /* Any JavaScript here will be loaded for all users on every page load. */ // Ajax auto-refresh var ajaxPages = ['Special:RecentChanges','Special:WikiActivity']; var AjaxRCRefreshText = 'Auto-refresh';

/* You can remove this comment * this calls FounderTag and AdminTag * after every time the ajax reloads */ ajaxCallAgain = [FounderTag,AdminTag];

importScriptPage('AjaxRC/code.js', 'dev'); // END of ajax auto-refresh

/* You can remove this comment * these next two lines will call FounderTag * and AdminTag only once, after the page * finishes loading */ $(FounderTag); $(AdminTag);

function FounderTag { $('[href="/wiki/User:HenryDuckFan"]').attr('title', 'This user is the Founder'); }

function AdminTag { $('[href="/wiki/User:Starfleet_Academy"], [href="/wiki/User:SirHandelFalcon"], [href="/wiki/User:Kpt_kraut_99"]').attr('title', 'This user is an Administrator'); }

You can see it working on my test wiki if you like, just go here and hover over my name, the title should say "This user is the Founder" both with/without the ajaxRC — Mathmagician 04:11, May 12, 2012 (UTC)