DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Switching Css Style Sheets With Javascript
// switch css style sheets with javascript (changing font size)
//this part goes within the head tags
<link rel="stylesheet" type="text/css" href="standard.css" title="standard" />
<link rel="alternate stylesheet" type="text/css" href="medium.css" title="medium" />
<link rel="alternate stylesheet" type="text/css" href="large.css" title="large" />
<script src="fontsize.js" type="text/javascript" language="javascript"></script>
//this part goes where you want the links that the user will click on to show up (the script part should probably be on one line with no breaks
<td valign="top" id="adjustcell">
<script type="text/javascript">
if (document.getElementById){ document.getElementById('adjustcell').innerHTML = '<table width="100" cellspacing="0" cellpadding="0" border="0"><tr>
<td colspan="4" height="10"> </td></tr><tr><td width="25"> </td><td width="25"><a href="#" onclick="setActiveStyleSheet(\'default\'); return false;">normal text size</a></td>
<td width="25"><a href="#" onclick="setActiveStyleSheet(\'medium\'); return false;">medium text size</a></td>
<td width="25"><a href="#" onclick="setActiveStyleSheet(\'large\'); return false;">large text size</a></td>
</tr>
<tr><td width="100" align="right" colspan="4" style="color: #ffffff;">Adjust Text Size</td></tr></table>';}
</script>
</td>
//contents of fontsizeswitcher.js (some of this is to switch images if you are using images instead of text links.
// var graphics_path = "" ;
var graphics_init = 0 ;
function init_path()
{
graphics_path = document.getElementById("default").src ; // "graphics" ;
graphics_path = graphics_path.substring(0,graphics_path.lastIndexOf("/"));
graphics_path = graphics_path.substring(graphics_path.indexOf(".com/")+4);
graphics_init = 1 ;
}
function setActiveStyleSheet(title) {
var i, a, main;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
a.disabled = true;
if(a.getAttribute("title") == title) {
a.disabled = false;
}
}
}
pressButton(title);
}
function unpressButton(which) {
if ( graphics_init == 0 )
{ init_path() }
document.getElementById(which).src = graphics_path + "/" + which + ".gif";
}
switchButtons = ["default", "medium", "large"];
function pressButton(which) {
if ( graphics_init == 0 )
{ init_path() }
for (i = 0; i < switchButtons.length; i++) {
if (which == switchButtons[i]) {
document.getElementById(which).src = graphics_path + "/" + which + "_p.gif";
} else {
document.getElementById(switchButtons[i]).src = graphics_path + "/" + switchButtons[i] + ".gif";
}
}
if (which == "null") {
document.getElementById("default").src = graphics_path + "/default_p.gif";
}
}
function getActiveStyleSheet() {
var i, a;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) {
return a.getAttribute("title");
}
}
return null;
}
function getPreferredStyleSheet() {
var i, a;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1
&& a.getAttribute("rel").indexOf("alt") == -1
&& a.getAttribute("title")
) return a.getAttribute("title");
}
return null;
}
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
} else {
expires = "";
}
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
window.onload = function(e) {
if ( graphics_init == 0 )
{ init_path() }
smallpressed = new Image();
smallpressed.src=graphics_path + "/default_p.gif"
medpressed = new Image();
medpressed.src=graphics_path + "/medium_p.gif"
largepressed = new Image();
largepressed.src=graphics_path + "/large_p.gif"
if (document.getElementById) {
var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
}
}
window.onunload = function(e) {
var title = getActiveStyleSheet();
createCookie("style", title, 365);
}





