首页/javascript培训班/JS换皮肤及cookie读写大全

JS换皮肤及cookie读写大全

发布-网页培训班 | 浏览量-

这是JS换皮肤及cookie读写的详细页面,介绍了和javascript,有关的知识、技巧、经验,和一些javascript源码等。
要做皮肤切换,就涉及到js存取cookie的问题

function getcookieval(offset)
//获得cookie解码后的值
{
var endstr = document.cookie.indexof (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function setcookie(name, value)
//设定cookie值
{
var expdate = new date();
var argv = setcookie.arguments;
var argc = setcookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
if(expires!=null) expdate.settime(expdate.gettime() + ( expires * 1000 ));
document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.togmtstring()))
+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
+((secure == true) ? "; secure" : "");
}
function delcookie(name)
//删除cookie
{
var exp = new date();
exp.settime (exp.gettime() - 1);
var cval = getcookie (name);
document.cookie = name + "=" + cval + "; expires="+ exp.togmtstring();
}
function getcookie(name)
//获得cookie的原始值
{
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getcookieval (j);
i = document.cookie.indexof(" ", i) + 1;
if (i == 0) break;
}
return null;
}



自己写了个简陋的换肤功能,以后需要完善这个

$(function(){
//换皮
var skinpath = skinpath||"themes/blue/skins/";
var defcss = defcss||"themes/blue/skins/red/red.css";
var css1 = skinpath+getcookie("style");
if(getcookie("style")!=null){
jquery('#skin').attr('href',css1);
}else{
jquery('#skin').attr('href',defcss);
}
//换红
jquery('#red').click(function(){
var css = skinpath+"red/red.css";
jquery('#skin').attr('href',css);
setcookie("style","red/red.css");
});
//换蓝
jquery('#blue').click(function(){
var css = skinpath+"blue/blue.css";
jquery('#skin').attr('href',css);
setcookie("style","blue/blue.css");
});
})


页头:

<link id='skin'  rel="stylesheet" type="text/css" />

原文地址:http://www.35ui.cn/post/2011061249..html

标签javascript培训班js网页制作jquery

上一条: js存取删除cookie|js 存取cookie|js读写cookie
下一条: jquery 获取子元素|jQuery过滤器获取元素的方法

或许你还对下面的文章感兴趣