PDA

View Full Version : JavaScript Function that loads CSS link


soundreamer
11-22-2005, 06:22 PM
Is it possible to have a JavaScript function in the page source that makes the page use a selected CSS file when the function gets executed?:)

]|V|[agnus
11-22-2005, 06:42 PM
Yes. Look up "styleswitchers," perhaps as two words, but that's the idea.

soundreamer
11-22-2005, 07:11 PM
Ammm... without reloading the page? I don't get it... can you make an example?

liorean
11-22-2005, 07:25 PM
Do you want to add a stylesheet to the document, or just change which alternate is the present stylesheet?

Adding a stylesheet is easy:function addStylesheet(sUrl, sType, sTitle, bAlternate, sMedia){
var
link=document.createElement('link');
link.href=sUrl||'';
link.type=sType||'text/css';
link.title=sTitle||'';
link.rel=(bAlternate?'alternate ':'')+'stylesheet';
link.media=sMedia||'screen, projection';
document.getElementsByTagName('head').item(0).appendChild(link);
}

A switcher is a slightly more involved thing to write, but it's got better backwards compatibility.

]|V|[agnus
11-22-2005, 07:34 PM
Ammm... without reloading the page? I don't get it... can you make an example?

I repeat:

Look up "styleswitchers," perhaps as two words, but that's the idea.

soundreamer
11-22-2005, 08:00 PM
10x a lot dude!:thumbsup: Now there is a way for me to make my mIRC Script with HTML theme script execute everything on the page, and even change the script's theme while chatting! :D Respect man!!! :cool: