gpt4 book ai didi

JavaScript:读取 cookie

转载 作者:行者123 更新时间:2023-12-02 19:35:47 27 4
gpt4 key购买 nike

我看到很多很多通过java脚本读取cookie的函数,但我只想在变量中使用它一次,我是JS新手。

这是我的代码

var TheNumber = (Math.random() + '') * 1000000000000000000;
document.cookie= "rand=" + TheNumber.toString() + ";path=/";
var AdServer = {
tile: 1,
mock: false,
ord: (Math.random() + "") * 1000000000000000000 + '?',

我想用 rand cookie 中的值替换 ord 部分。

您能否指导我执行以下操作:我需要一个函数吗?如果是的话我该把它放在哪里?我该怎么调用它?

最佳答案

我发现使用 JavaScript 写入/读取 cookie 的最简单(也是最灵活)的方法是使用带有 getter/setter 方法的全局对象。

document.cookie 上的 Mozilla 开发文档页面有一个详细记录的示例:https://developer.mozilla.org/en/DOM/document.cookie

如何/在哪里实例化然后引用该对象取决于程序的其余部分,但为了简单起见,假设我们只是在全局命名空间中并且不担心变量冲突等:

var docCookies = {
getItem: function (sKey) {
if (!sKey || !this.hasItem(sKey)) { return null; }
return unescape(document.cookie.replace(new RegExp("(?:^|.*;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1"));
},
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/.test(sKey)) { return; }
var sExpires = "";
if (vEnd) {
switch (typeof vEnd) {
case "number": sExpires = "; max-age=" + vEnd; break;
case "string": sExpires = "; expires=" + vEnd; break;
case "object": if (vEnd.hasOwnProperty("toGMTString")) { sExpires = "; expires=" + vEnd.toGMTString(); } break;
}
}
document.cookie = escape(sKey) + "=" + escape(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
},
hasItem: function (sKey) { return (new RegExp("(?:^|;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie); }
}

然后设置你的cookie:

docCookies.setItem('rand', (Math.random()* 1000000000000000000).toString());

并通过以下方式获取它:

docCookies.getItem('rand');

所以把它们放在一起:

var docCookies = {
getItem: function (sKey) {
if (!sKey || !this.hasItem(sKey)) { return null; }
return unescape(document.cookie.replace(new RegExp("(?:^|.*;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1"));
},
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/.test(sKey)) { return; }
var sExpires = "";
if (vEnd) {
switch (typeof vEnd) {
case "number": sExpires = "; max-age=" + vEnd; break;
case "string": sExpires = "; expires=" + vEnd; break;
case "object": if (vEnd.hasOwnProperty("toGMTString")) { sExpires = "; expires=" + vEnd.toGMTString(); } break;
}
}
document.cookie = escape(sKey) + "=" + escape(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
},
hasItem: function (sKey) { return (new RegExp("(?:^|;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie); }
}
//set our cookie
docCookies.setItem('rand', (Math.random()* 1000000000000000000).toString());

然后当您想要检索 cookie 值时,稍后/在代码中的其他位置:

var AdServer = {
tile: 1,
mock: false,
ord: docCookies.getItem('rand')
};

现在,如果您检查 AdSever.ord,它将等于您之前设置的 rand cookie 中的随机数。

console.log(AdServer.ord);

关于JavaScript:读取 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10939964/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com