Tj McDonald
Tj McDonald

Reputation: 33

Why is external javascript library blocking javascript on my page?

Here's a very simple question that my simple mind can't answer: why isn't the anonymous and onload function below being run when I load in the external library? I am missing something really, really basic. Library.js has only one line: console.log('library'). How much more basic can one get?

<script type="text/javascript" src='js/library.js' />
<script type="text/javascript">
    (function () {
        console.log('anon');
    })();

    window.onload = function () {
        console.log('onload');
    } ();   
</script>

Upvotes: 1

Views: 153

Answers (1)

I Hate Lazy
I Hate Lazy

Reputation: 48789

Your script syntax is invalid. You should have a separate closing tag instead of the self-closing version.

                                           <!--------- v --------->
<script type="text/javascript" src='js/library.js'></script>

You also have an issue in that you're invoking the onload function immediately.

window.onload = function () {
    console.log('onload');
}/* () */;
 // ^^---remove these to assign the function instead of invoking it.

Upvotes: 2

Related Questions