PHP and Web Technologies Freak
It may be useful sometimes to provide your visitors with the option to change the appearance of your website. Perhaps you may want to provide theme support for your site. Or you may simply want to provide a bigger font size version of your site for older users who may not be able to read the small-sized text you use by default. This article describes how you can use JavaScript to dynamically change the cascading style sheets, or CSS, of your website.
Here we go;
1st put your css files in ./css directory like
./css/theme1.css
./css/theme2.css
./css/theme3.css
Then put below code in your head tag.
<script> var StyleFile = "theme" + document.cookie.charAt(6) + ".css"; document.writeln('<link rel="stylesheet" type="text/css" href="css/' + StyleFile + '">'); </script>
After that u can make style changer buttons
<div id="styleswitcher">
<ul>
<li><a href="javascript: document.cookie='theme='; window.location.reload();" title="Default" id="defswitch">d</a></li>
<li><a href="javascript: document.cookie='theme=1;'; document.cookie='theme=1; path=/;'; window.location.reload();" title="Blue" id="blueswitch">b</a></li>
<li><a href="javascript: document.cookie='theme=2'; document.cookie='theme=2; path=/;'; window.location.reload();" title="Green" id="greenswitch">g</a></li>
<li><a href="javascript: document.cookie='theme=3'; document.cookie='theme=3; path=/;'; window.location.reload();" title="Brown" id="brownswitch">b</a></li>
</ul>
</div>
Compare Your Auto Insurance Quote Between Multiple Carriers In Your State
April 24th, 2009 at 06:34
[...] Javascript CSS style changer | Ersin Acar [...]
Sunny Singh
October 24th, 2009 at 01:07
How would you make it so the page doesn’t need to be refreshed?
Otherwise awesome script, can’t believe how short it is.
Bruce Ali
October 28th, 2010 at 18:00
Hi.
I want to use it in Masterpage ASP.net
but it does not work.
Can you please tell me how can I use this script such that script works on my masterpage.`?
Thank you
admin
November 3rd, 2010 at 22:03
Hi,
Sorry but i have no experince about ASP.Net but its Javascript. You can use it where ever you want.
Btw whats the problem? Do you have an example page ? Maybe i can help you.
ravi
November 6th, 2010 at 20:04
nice tutorial on javascript
reO
January 7th, 2011 at 21:04
very needfull, but not workin
wjwoodley
April 3rd, 2011 at 20:58
This looks great, but it isn’t working for me.
What do you mean by ‘in ./css directory’?