|
Partners
|
Dynamically removing/ replacing an external JavaScript or CSS fileAny external JavaScript or CSS file, whether added manually or dynamically, can be removed from the page. The end result may not be fully what you had in mind, however. I'll talk about this a little later.
|
| Demo: | "myscript.js" source:var petname="Spotty"
alert("Pet Name: " + petname)
|
"mystyle.css" source:#demotable td{
background-color: lightyellow;
}
#demotable b{
color: blue;
}
|
Replacing an external JavaScript or CSS file isn't much different than
removing one as far as the process goes. Instead of calling
parentNode.removeChild(), you'll be using
parentNode.replaceChild() to do the bidding instead:
function createjscssfile(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)
}
return fileref
}
function replacejscssfile(oldfilename, newfilename, filetype){
var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist using
var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
var allsuspects=document.getElementsByTagName(targetelement)
for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(oldfilename)!=-1){
var newelement=createjscssfile(newfilename, filetype)
allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i])
}
}
}
replacejscssfile("oldscript.js", "newscript.js", "js") //Replace all occurences of "oldscript.js" with "newscript.js"
replacejscssfile("oldstyle.css", "newstyle", "css") //Replace all occurences "oldstyle.css" with "newstyle.css"
Notice the helper function createjscssfile(), which is
essentially just a duplicate of loadjscssfile() as seen on the previous
page, but modified to return the newly created element instead of actually
adding it to the page. It comes in handy when
parentNode.replaceChild() is called in replacejscssfile()
to replace the old element with the new. Some good news here- when you
replace one external CSS file with another, all browsers, including IE7,
will reflow the document automatically to take into account the new file's
CSS rules.
| Demo: | "oldscript.js" source:var petname="Spotty"
alert("Pet Name: " + petname)
"newscript.js" source: var petname="Beauty"
alert("Pet Name: " + petname)
|
"oldstyle.css" source:#demotable2 td{
background-color: lightyellow;
}
#demotable2 b{
color: blue;
}
"newstyle.css" source: #demotable2 td{
background-color: lightblue;
}
#demotable2 b{
color: red;
}
|
So when is all this useful? Well, in today's world of Ajax and ever larger web applications, being able to load accompanying JavaScript/ CSS files asynchronously and on demand is not only handy, but in some cases, necessary. Have fun finding out what they are, or implementing the technique. :)