Implement the webpage skin replacement function, JavaScript + CSS

Source: Internet
Author: User
Tags set cookie setcookie

Javascript + CSS enable webpage skin replacement

Principle: By accessing cookies and Dom operations, different style sheet files are called to enable front-end Skin replacement.

HTML code:

1. To have a style sheet link with ID, we need to call different href through this link.

<Link href = "CSS/main0.css" rel = "stylesheet" type = "text/CSS" id = "cssfile"/>

2. Skin selection button (added for each Li in the backgroundOnclickEvent to trigger skin replacement)

<Ul id = "skin">
<Li id = "skin_0" Title = "gray"> gray </LI>
<Li id = "skin_1" Title = "green"> green </LI>
<Li id = "skin_2" Title = "yellow"> yellow </LI>
<Li id = "skin_3" Title = "blue"> blue </LI>
<Li id = "skin_4" Title = "pink"> Pink </LI>
<Li id = "skin_5" Title = "Purple"> purple </LI>
</Ul>

JS section:

1. Skin replacement method

// Set cookie, select button, page skin
Skin. setskin = function (n ){
VaR skins = ("skin"). getelementsbytagname ("Li ");
For (I = 0; I <skins. length; I ++)
{
Skins [I]. classname = ""; // initialize the button status
}
Skin. setcookie (n); // Save the current style
("Skin _" + n). classname = "selected"; // you can specify the style of the selected skin button.
("Cssfile"). href = "CSS/main" + N + ". CSS"; // set the Page Style
}

2. Access cookies

// Save the current skin n to the cookie
Skin. setcookie = function (n ){
VaR expires = new date ();
Expires. settime (expires. gettime () + 24x60x60x365*1000 );
VaR flag = "skin_cookie =" + N;
Document. Cookie = Flag + "; expires =" + expires. togmtstring ();
}
// Return the skin style set by the user
Skin. readcookie = function (){
VaR Skin = 0;
VaR mycookie = Document. Cookie;
VaR name = "skin_cookie ";
VaR start1 = mycookie. indexof (name + "= ");
If (start1 =-1 ){
Skin = 0; // if not set, the default style is displayed.
}
Else {
VaR start = mycookie. indexof ("=", start1) + 1;
VaR end = mycookie. indexof (";", start );
If (END =-1 ){
End = mycookie. length;
}
VaR values = Unescape (mycookie. substring (START, end ));
If (values! = NULL)
{
Skin = values;
}
}
Return skin;

}

3. Bind a skin change button event

Skin. addevent = function (){
VaR skins = ("skin"). getelementsbytagname ("Li ");
For (I = 0; I <skins. length; I ++)
{
Skins [I].Onclick= Function () {skin. setskin (this. Id. substring (5 ))};
}
}

4. Set skin style after page loading

Window. onload = function (){
Skin. setskin (skin. readcookie (); // set the skin style based on the returned value of the read cookie.
Skin. addevent (); // bind button event

Source: Blue ideal

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.