I have a JavaScript problem.
I searched on google how to append using javascript and I found an example. I want to manually append a script tag to head or body, but the example just appends in the head.
I edited the example so that the script tag can be appended in the head or body, but the code doesn't work.
Can anyone help me fix my problem? And please show me the demo on jsfiddle :D
Thanks :)
Here my script:
//JS
//Defined with loadjscssfile("yourcssurl.js", "js", "head"); or addCss("yourcssurl.js", "js", "body");
//CSS
//Defined with loadjscssfile("yourcssurl.css", "css", "head"); or addCss("yourcssurl.css", "css", "body");
function loadjscssfile(filename, filetype, pos) {
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 == filetype) {
document.getElementsByTagName(pos)[0].appendChild(fileref)
}
else if (typeof fileref != "undefined") {
document.getElementsByTagName("head")[0].appendChild(fileref)
}
}
loadjscssfile("myscript.js", "js", "body") //dynamically load and add this .js file
loadjscssfile("javascript.php", "js", "body") //dynamically load "javascript.php" as a JavaScript file
loadjscssfile("mystyle.css", "css", "body") ////dynamically load and add this .css file
And here is the original script, before I edited: Dynamically loading an external JavaScript or CSS file
Sorry for my bad english :)