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 ();