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!
fionaredmond
6597 silver badges16 bronze badges
asked Dec 28, 2015 at 13:26
Alvaro Silvino
9,83314 gold badges56 silver badges81 bronze badges
1 Answer 1
Use onload
fileref.onload = function(){
alert("Loaded");
};
before setting the src.
answered Dec 28, 2015 at 13:27
void
36.8k10 gold badges73 silver badges111 bronze badges
Sign up to request clarification or add additional context in comments.
6 Comments
gurvinder372
what about his
fileref being not in global scope for final if condition?Alvaro Silvino
I didn't get it @gurvinder372
void
@gurvinder372 i don't think that will be a problem, it will never be out of scope inside the function.
gurvinder372
var fileref=document.createElement('script') is inside the first if condition, his second if condition uses that fileref variable.void
@gurvinder372 javascript does not have block scope, it just has function scope, i hope you are aware of this.
|
lang-js