// Original address: [url] http://www.alistapart.com/articles/alternate/#/url]
Function setactivestylesheet (title) {// you can set the style based on the specified title passed.
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;
}
}
}
Function getactivestylesheet () {// gets the title of the currently used Style
VaR I,;
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 () {// obtain the default style/backup Style
VaR I,;
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) {// create a cookie
If (days ){
VaR date = new date ();
Date. settime (date. gettime () + (days * 24x60*60*1000 ));
VaR expires = "; expires =" + date. togmtstring ();
}
Else expires = "";
Document. Cookie = Name + "=" + value + expires + "; Path = /";
}
Function readcookie (name) {// obtain the specified cookie
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) {// The cookie record style is called when the window is loaded.
VaR cookie = readcookie ("style ");
VaR Title = cookie? COOKIE: getpreferredstylesheet ();
Setactivestylesheet (title );
}
Window. onUnload = function (e ){
VaR Title = getactivestylesheet ();
Createcookie ("style", title, 365 );
}
VaR cookie = readcookie ("style ");
VaR Title = cookie? COOKIE: getpreferredstylesheet ();
Setactivestylesheet (title );
<! -- Default style -->
<Link Title = "GR" Media = "screen" xhref = "style.css" type = "text/CSS" rel = "stylesheet"/>
<! -- Backup style -->
<Link Title = "or" Media = "screen" xhref = "style2.css" type = "text/CSS" rel = "alternate stylesheet"/>
<SCRIPT xsrc = "styleswitcher. js" type = "text/JavaScript"> <! -- Control the default style --> <a xhref = "#" onclick = "setactivestylesheet ('gr '); Return false;"> style1 </a>
<! -- Control the backup style --> <a xhref = "#" onclick = "setactivestylesheet ('or'); Return false;"> style2 </a>