Hussein Al-Mosawi
Hussein Al-Mosawi

Reputation: 1684

Check if a JavaScript file was loaded

First, this is not a duplicate of this question... And you'll see why in a second.

I have a file that I want to reference in my HTML

    <script src="dist/js/script.js"></script>

But I want to check if it was loaded or not and when it loads I want to do something with it...

I have tried using the load event(addEventListener) but for some reason (I think because I have async features in the external file) it doesn't work.

I'm using an inline script tag after the external file and this (inline script) block of code gets executed before the script tag above it (The external file), and I want the external file to load first before my inline script

How can I make it so that the inline <script> runs after my external file has been 100% loaded?

Upvotes: 2

Views: 1965

Answers (2)

greentec
greentec

Reputation: 2280

Try window.onload. This event fires after all scripts are loaded.

function init() {
    // your code
}

window.onload = init;

Upvotes: 3

Govi-Boy
Govi-Boy

Reputation: 99

Attach an function to the load event: <script src="dist/js/script.js" onload ="SomeFunction()"></script>

Upvotes: 1

Related Questions