如何将 document.cookie 替换为 Cookies.set(使用最新的 jquery cookie 插件)
How to replace document.cookie to Cookies.set (with latest jquery cookie plugin)
我正在尝试将 document.cookie 替换为 Cookies.set(使用最新的 jquery cookie 插件)但无法正常工作。插件 link here
<script>
function setCookie(name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=name + "=" + value;
}
function getCookie(name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==name)
{
return unescape(y);
}
}
}
</script>
这是等效的:
<script>
function setCookie(name,value,exdays) {
Cookies.set( name, value, {
expires: exdays,
// path: "" or
// path: "/"
// In version 2 the path is default to "/"
// In version <= 1 the path is default to "" (path of the current page)
})
}
function getCookie(name) {
return Cookies.get( name );
}
</script>
我正在尝试将 document.cookie 替换为 Cookies.set(使用最新的 jquery cookie 插件)但无法正常工作。插件 link here
<script>
function setCookie(name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=name + "=" + value;
}
function getCookie(name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==name)
{
return unescape(y);
}
}
}
</script>
这是等效的:
<script>
function setCookie(name,value,exdays) {
Cookies.set( name, value, {
expires: exdays,
// path: "" or
// path: "/"
// In version 2 the path is default to "/"
// In version <= 1 the path is default to "" (path of the current page)
})
}
function getCookie(name) {
return Cookies.get( name );
}
</script>