JS code example: implementation of random loading of different CSS styles

Source: Internet
Author: User
Tags date define min return set cookie setcookie

Random load CSS style JS effect is actually very good implementation, the code of this article is as follows, the specific idea is to use a default CSS style: Default.css. Another css:skin1.css,skin2.css,skin3.css with three other names. Of course, you can use more stylesheets, and then random replacement in load, because the first load of the DEFAULT.CSS style is written directly on the page, and JS random load behind the CSS file will overwrite the previous CSS, as long as the element name in the CSS is the same.

var Init = {

Style Sheet File directory path
Baseskinurl: "/blog/css/skin/",

List of style sheet file names
Styles: ["Default", "Skin1", "skin2", "skin3"],

Key value for Style cookie
Cookiekey: "Css9_blog_random_css",

Define the method to get the random number between Min and Max, including Min and Max
Getrandomnum:function (min, max) {
return min + math.floor (math.random () * (Max-min + 1));
},

Define method, get cookie value
Getcookie:function (name) {
var arr = Document.cookie.match (New RegExp ("(^)" + name + "= ([^;] *)(;$)"));
if (arr!= null) {
Return unescape (arr[2]);
}
return null;
},

Define method, set cookie value
Setcookie:function (sname,svalue,objhours,spath,sdomain,bsecure) {
var SCookie = sname + "=" + encodeURIComponent (svalue);
if (objhours) {
var date = new Date ();
var ms = Objhours * 3600 * 1000;
Date.settime (Date.gettime () + ms);
SCookie + = "expires=" + date.togmtstring ();
}
if (spath) {
SCookie + + ";p ath=" + spath;
}
if (Sdomain) {
SCookie + + ";d omain=" + sdomain;
}
if (bsecure) {
SCookie + = "secure";
}
Document.cookie=scookie;
},

Define methods to randomly load CSS by getting random numbers
Loadcss:function () {
var length = This.styles.length,
Random = This.getrandomnum (0, Length-1),
Cookiestyle = This.getcookie (This.cookiekey),
Currentstyle = "Default";

Recalculate random numbers if the current randomly fetched style is the same as the style in the cookie
while (this.styles[random] = = Cookiestyle)
{
Random = This.getrandomnum (0, Length-1)
}

Currentstyle = This.styles[random];

Save new style to Cookie,cookie valid time is 24 hours
This.setcookie (This.cookiekey, Currentstyle,, "/", "websbook.com", false);

Write a custom style to the if (currentstyle!= "Default")
{
document.write (' <link rel= "stylesheet" type= "Text/css"
href= "' + This.baseskinurl + this.styles[random] + '. css '/> ');
}
}
}

Init.loadcss ();


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.