Alvaro Silvino
Alvaro Silvino

Reputation: 9743

how to know if script is loaded

I'm trying to load a script like:

function loadjscssfile(filename, filetype){
    if (filetype=="js"){ //if filename is a external JavaScript file
        var fileref=document.createElement('script')
        fileref.setAttribute("type","text/javascript")
        fileref.setAttribute("src", filename)
    }
    else if (filetype=="css"){ //if filename is an external CSS file
        var fileref=document.createElement("link")
        fileref.setAttribute("rel", "stylesheet")
        fileref.setAttribute("type", "text/css")
        fileref.setAttribute("href", filename)
    }
    if (typeof fileref!="undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref)
};

loadjscssfile("js/view/Header.js", "js") //dynamically load "javascript.php" as a JavaScript file
       

How can I make sure the script was fully loaded?

Thanks!

Upvotes: 1

Views: 110

Answers (1)

void
void

Reputation: 36703

Use onload

fileref.onload = function(){
  alert("Loaded");
};

before setting the src.

Upvotes: 2

Related Questions