How and when do browsers implement real-time changes to a document's DOM?

Posted by Mark on Stack Overflow See other posts from Stack Overflow or by Mark
Published on 2010-05-30T00:14:33Z Indexed on 2010/05/30 0:22 UTC
Read the original article Hit count: 619

Filed under:
|
|
|
|

My website dynamically embeds an external Javascript file into the head tag. The external Javascript defines a global variable myString = "data". At what point does myString become accessible to Javascript within the website?

<html>
<head>
    <script type="text/javascript">
        myString = null;
        external = document.createElement("script");
        //externalScript.js is one line, containing the following:
        //myString = "data";
        external.setAttribute("src", "externalScript.js");
        external.setAttribute("type", "text/javascript");
        document.getElementsByTagName("head")[0].append(external);
        alert(myString);
    <script>
</head>
<body>
</body>
</html>

This code alerts null (when I thought it would alert "data") in Chrome and IE, even though the DOM has loaded in externalScript.js at this point. When is externalScript.js actually evaluated by the browser and at what point do I have access to the new value of myString?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about dom