Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8994949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:32:18+00:00 2026-06-15T23:32:18+00:00

I have a userscript that is dynamically loading an external javascript file and for

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:32:20+00:00Added an answer on June 15, 2026 at 11:32 pm

    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...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some javascript (a greasemonkey/userscript) that will insert some input fields into a
I have a userscript (read: my Javascript on someone else's site) that allows users
I have a userscript that has to change a whole list of options depending
I have a userscript for Google Reader , and as part of that userscript
I've got a rather hideous and large javascript file that I've inherited from a
I have a question regarding JavaScript and methods by which its code can be
I have an userscript running on chrome. I think jquery has been loaded because
I have this code // // ==UserScript== // @name test // @description test //
I have a Greasemonkey userscript which runs most of its code in an unprivileged
I have userscript. I recentyly added jquery to it. Before I added jquery that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.