0

I'm loading a javascript external file from another javascript file present in the document and since its loaded, I want to call a function from the loaded js file. Here is the load function:

function loadScript(url) {
    var head = window.top.document.getElementsByTagName('head')[0];
    var script = window.top.document.createElement('script');
    script.src = url;
    script.type= "text/javascript";
    head.appendChild(script);
    if(script.readyState) {  //IE
        script.onreadystatechange = function() {
            if ( script.readyState === "loaded" || script.readyState === "complete" ) {
                script.onreadystatechange = null;
                console.log("[BANDEAU] script loaded");
                testAlert();
            }
        };
    } else {  //Others
        script.onload = function() {
            console.log("[BANDEAU] script loaded");
            testAlert();
        };
    }
}

So it works nice because the javascript file is succesfuly loaded but I cannot access the testAlert() method from the loaded javascript file, as I try in the code above, right after printing that the script is loaded. When I try to get the type of the function with typeOf on window[testAlert], I get an undefined. But when I try to execute the testAlert() method in the developer console, it works perfectly. Does anyone see what I'm doing wrong ?

Does the position in the DOM between the caller javascript file and the loaded javascript file might be the reason ?

2
  • Why are you appending the script to window.top instead of window? Commented Dec 15, 2016 at 15:40
  • 1
    To put it another way: when that code runs, is window the same as window.top or not? If they're different, then that's your problem. Commented Dec 15, 2016 at 16:27

2 Answers 2

2

You need to assign the load handlers BEFORE changing the src

function loadScript(url) {
  var head = document.getElementsByTagName('head')[0]; // window.top in frames/iFrames
  var script = document.createElement('script');
  script.type = "text/javascript";

  if (script.readyState) { //IE
    script.onreadystatechange = function() {
      if (script.readyState === "loaded" || script.readyState === "complete") {
        script.onreadystatechange = null;
        console.log("[BANDEAU] script loaded");
        testAlert(); // window.top.testAlert() if needed
      }
    };
  }
  else {
    script.onload = function() {
      console.log("[BANDEAU] script loaded");
      testAlert(); // window.top.testAlert() if needed
    };
  }
  script.src = url;
  head.appendChild(script);
}
Sign up to request clarification or add additional context in comments.

3 Comments

@HubertSolecki are you loading that file from an <iframe> or something?
@Pointy yes... just noticed that.... Just needed to add parent keyword right before my testAlert function...
@HubertSolecki right, window.parent.testAlert() or window.top.testAlert().
1

In addition to what mplungjan said, I'm pretty sure you'd have to do an eval() on the loaded script in order to have a legitimate address for the call to testAlert().

Also, check out this link for more info.

4 Comments

Keep in mind that eval() is dangerous, so I wouldn't recommend it as a long-term solution. The link I provided features more mature (and safer) solutions.
This is untrue. The OP is creating a <script> tag and appending it to the document; the normal behavior of a <script> tag is that the browser loads the code and runs it.
True. The parent does run the code, but the loadScript() function won't automatically receive a pointer to testAlert(). But I see your answer in the above comment, and that totally works; reference testAlert() with window.parent.testAlert(). That's a lot better than my idea of using eval().
Thanks everyone for your answers. The problem was that I called the function within an iFrame, juts noticed that so I needed to call the function on the parent pointer.

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.