3

I have a userscript that is dynamically loading an external javascript file and for some reason it is running twice.

Here is the code loading the file:

var jqScript = document.createElement('script');
jqScript.setAttribute('type', "text/javascript");
jqScript.setAttribute('src', "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.js");
(document.body || document.head || document.documentElement).appendChild(jqScript);

var extraScript = document.createElement('script');
extraScript.setAttribute('src', "http://youtube.thegoblin.net/layoutFix/script.js");
document.body.appendChild(extraScript);

And the code from the external file:

alert(1);

$.get(
"http://www.youtube.com/my_subscriptions",
function(data) {
   var dataSplit = data.split("<ol class=\"vm-vertical-nav\">")[1].split("</ol>")[0].split("<li>");
   var dataString = [];
   for (var i=2;i<dataSplit.length;i++)
   {
       dataString[i - 2] = dataSplit[i].split(">")[1].split("</a")[0];
   }
  alert(dataString);
}
, "text");

When I run my code I get both alerts from the external .js file, but I get them twice.

What have I done? How can I make them only be called once?

UPDATE:


I have narrowed it down. It is happening because it is being appended to the Body of the iframes that are in the page.

How can I only append it to the current page and not the iframes as well?

3
  • Have you looked at the rendered code and see if it's actually loading the JavaScript reference twice? Not really sure what the rest of your code looks like so it's hard to be sure but looking at this seems to be fine. Commented Dec 16, 2012 at 7:44
  • I have looked at the DOM and I can see that the script has been appended twice, once to the main document's body, the second time to the iframes body from an ad on the page. I already knew that, I just need to know how to append to the main document without appending to the iframe as wll Commented Dec 16, 2012 at 7:53
  • Essentially a duplicate of "Run greasemonkey script only once per page load?", "execute function only once", and "Why does jQuery load twice in my GreaseMonkey Script". Although those are tagged Greasemonkey instead of userscripts, the principle and cure is the same. Commented Dec 16, 2012 at 12:31

1 Answer 1

2

I think what's happening is that your userscript doing the append is being applied to all windows (the main one, and the iframes), not that your code doing the append inadvertently appends to the iframes as well as the main window. E.g., your append code is being run for the main window and for each iframe.

There are a couple of ways to know whether you're in an iframe. One is to check for window.frameElement being !null. So:

if (!window.frameElement) {
    // ...your append code here...
}

According to MDN, that will work with just about any version of IE, any version of Firefox, and Chrome 18 or higher.

Another way to check is to see if window.top != window.self, so:

if (window.top != window.self) {
    // ...your append code here...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it worked! Just to be clear to anyone else who reads this, I added the above "window.top != window.self" if statement around the code in my external script file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.