JavaScript Cookie生成器
我的目标是:JavaScript Cookie生成器,javascript,html,cookies,web,Javascript,Html,Cookies,Web,我的目标是: <!DOCTYPE html> <html> <body> <script> function changerScript() { var x = document.getElementById('changethis'); var y = document.getElementById('textinputter').value; var z = '");'; if(y == "") { alert('Please enter y
<!DOCTYPE html>
<html>
<body>
<script>
function changerScript()
{
var x = document.getElementById('changethis');
var y = document.getElementById('textinputter').value;
var z = '");';
if(y == "")
{
alert('Please enter your JavaScript code, then click the Submit button.');
}
else
{
x.innerHTML = ('javascript:void(document.cookie="' + y + z);
alert('JavaScript String Successfully Generated!');
}
}
</script>
<div align="center">
<input type="text" id="textinputter" />
<button type="button" onClick="changerScript()">Submit</button>
</div>
<br /><hr />
<div align="center">
<p id="changethis"></p>
</div>
</body>
</html>
在上面的例子中,我如何添加多个cookie,因为
javascript:void(document.cookie="Cookie=cookiedata; Cookie2=cookiedata2;");
不起作用
首先,我想粘贴Cookie数据(
Cookie1=cookie1data;Cookie2=cookie2data;Cookie3=cookie3data;
)让JavaScript为我生成代码,这样我就可以将代码粘贴到Chrome的URL栏中,并使用生成的JavaScript字符串设置cookie。document.cookie
一次只接受一个值。除此之外,document.cookie还接受三个参数:cookie名称、值和过期日期
您可以从location.search
获取URL参数,然后可以重用以下方法来设置cookie。只需使用for循环结构来遍历所有参数,并为每个参数设置cookie
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
有关更多信息,请参阅。感谢您的回复,我将如何将此实现到我想用上面的脚本执行的操作中?
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}