1

I'm probably just missing something basic about onreadystatechange. Why doesn't either console message occur here? c.js runs just fine.

<span id="rightHere"></span>

<script>

var c = document.createElement("script");
c.src = "lib/c.js";
c.onreadystatechange = function () {
    console.log("IT FIRED");
    if (c.readyState == 'complete') {
        console.log("IT IS COMPLETE");
        ipmElements.control.login.autoLogin();
    }
}
var hook = document.getElementById("rightHere");
hook.parentNode.insertBefore(c, hook);

</script>

The larger problem is wanting to do something like this for fallback if a hosted library can't be reached, as shown in this answer there: https://stackoverflow.com/a/7864346/4866743

Community
  • 1
  • 1
  • 1
    You don't seem to be actually fetching the file, just defining its location. Docs (https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/onreadystatechange) indicate that the `onreadystatechange` event is applied to a new request. – Nathan Hornby Dec 18 '15 at 18:26
  • 1
    What about adding `c.onload`? The link you posted uses both events. – DonovanM Dec 18 '15 at 18:29
  • Oh, right, no onload. Worked now! But can anyone explain why both are needed? It seems redundant. – Chris Morrow Dec 18 '15 at 19:36

0 Answers0