1

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!

asked Dec 28, 2015 at 13:26

1 Answer 1

2

Use onload

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

before setting the src.

answered Dec 28, 2015 at 13:27
Sign up to request clarification or add additional context in comments.

6 Comments

what about his fileref being not in global scope for final if condition?
I didn't get it @gurvinder372
@gurvinder372 i don't think that will be a problem, it will never be out of scope inside the function.
var fileref=document.createElement('script') is inside the first if condition, his second if condition uses that fileref variable.
@gurvinder372 javascript does not have block scope, it just has function scope, i hope you are aware of this.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.