My Technical Notes

Monday, 4 February 2013

Prevent JQuery From Loading Twice In GreaseMonkey

Given a script:


$(function() {
   alert('greasemonkey!');
});

What you will notice is that the alert will appear twice. The reason is because the GreaseMonkey script loads for the current page and for each iframe in the page. To prevent this, you can exit early if GreaseMonkey is loading it for an iframe:


if (window.top != window.self)  // Don't run on frames or iframes
    return;

$(function() {
   alert('greasemonkey!');
});

Source: SO - Why does jQuery load twice in my GreaseMonkey Script

No comments: