gpt4 book ai didi

javascript - 如何将变量放入cookie中?

转载 作者:行者123 更新时间:2023-12-03 06:36:09 26 4
gpt4 key购买 nike

我想将变量的字符串放入 cookie 中。 (作为内容)当前 cookie 已设置,名称为用户名。 (这部分有效)但是变量的内容没有保存在 cookie 中。这就是我所拥有的。我看过另一个类似的问题,但答案不起作用。可能是因为我使用的是字符串

var sharedContent = sharedURL;
var sharedURL = "http://www.example.com/";
function getUrl() {
window.location.href= sharedURL;
createCookie();
}
function createCookie() {
document.cookie = "username="+sharedContent+"; expires=Wed, 1 Jan 2025 13:47:11 UTC; path=/"
}

正如您可能已经注意到的,我对使用 js 有点陌生。

HTML:

<link href='https://fonts.googleapis.com/css?family=Lato:300' rel='stylesheet' type='text/css'>

<a href="https://www.minds.com/newsfeed"><div class="button" onclick="getUrl(); "><img src="icon.png" width="16">Share</div></a>

最佳答案

如果您更改变量/函数声明位置并将 ; 更改为 ,

,它就可以正常工作
var sharedURL = "http://www.example.com/";
var sharedContent = sharedURL;
function createCookie() {
var cookies = "username="+sharedContent+", expires=Wed, 1 Jan 2025 13:47:11 UTC, path=/"
document.cookie = cookies;
}
function getUrl() {
window.location.href= sharedURL;
createCookie();
}
getUrl();

关于javascript - 如何将变量放入cookie中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38195925/

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