Javascript calls CSS

Source: Internet
Author: User

// 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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.