tags:

views:

370

answers:

3

Hello,

Am wondering how it would be possible to unload a CSS from a page. e.g. In my page I have included a file called a.css. Now I want the user to be able to change the theme, which is CSS driven, thus he/she should be able to unload a.css and then I can load b.css (else they will conflict)

Any idea how to go about this?

+4  A: 
var firstLink = document.getElementsByTagName('link')[0];
firstLink.parentNode.removeChild(firstLink)

This would remove the first link element on the page - not sure how your html is structured but I'm sure you can use it as an example. You might want to check the type attribute if it's 'text/css' and you're targeting the right media (screen), or possibly check if the href contains 'css' anywhere if you have other link elements that aren't css references.

Note you can also re-set the href attribute to point to a non-existing page instead of removing the element entirely.

meder
Isn't it better to use an id for the link tag?
rahul
It's uncommon to see the id attribute set on the link element but I'm sure for this exact purpose the OP would probably want to, less parsing and dealing with multiple link elements.. good idea.
meder
+1.. posted before me and should be marked correct ..
Xinus
I like your solution better though, setting an attribute instead of removing it entirely.
meder
+9  A: 

Take the link element and disable it

document.getElementsByTagName('link')[0].disabled = true;
Xinus
Hm, this seems to work nicely too.
meder
A: 

And if I need to unload only layout.css and I dont know the order (may vary on some page).

Jiri