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

Source: Internet
Author: User
Keywords Web page production Ajax javascript
Tags ajax code cookie course date default different document

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


 


//Style sheet file name list


styles: ["Default", "Skin1", "skin2", "skin3"],


 


//Style cookie key value


cookiekey: "Css9_blog_random_css",


 


//Definition method to obtain 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;


},


 


//Definition method to randomly load CSS
by obtaining random numbers

loadcss:function () {


var length = this.styles.length,


random = this.getrandomnum (0, length-1),


Cookiestyle = This.getcookie (This.cookiekey),


currentstyle = "Default";


 


///If the current randomly fetched style is the same as the style in the cookie, recalculate the random number


while (this.styles[random] = = Cookiestyle)


{


random = this.getrandomnum (0, length-1)


}


 


currentstyle = This.styles[random];


 


//Save the new style Cookie,cookie valid for 24 hours


This.setcookie (This.cookiekey, Currentstyle, "/", "websbook.com", false);


 


///If the style name is not default style, write the custom style to the <head/> label


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.