我正在尝试读取 3 个 cookie,然后打印它们的值,但由于某种原因,页面会无限期地加载,cookie 会被打印,但页面会继续加载。我不太精通javascript,所以不知道为什么会这样。我在下面发布我的代码可能会有所帮助。顺便说一句,所有 3 个 cookie 都在那里并且具有正确的值,我使用 php 脚本进行了检查,该脚本可以很好地读取它们的值。
<html>
<head>
<script>
var userRPM = true;
var userRWebcam = false;
var userEntryExitSFX = true;
var userNewMessageSFX = true;
var userSFX = true;
/*
* read cookie
*
*/
function readCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++)
{
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
/*
* get cookie
*
*/
var gotCookie1 = readCookie('myTextStyle');
var gotCookie2 = readCookie('myOptions');
function getCookie()
{
if(gotCookie1)
{
gotCookie = decodeURI(gotCookie1).split("|");
mBold = gotCookie[0];
mItalic = gotCookie[1];
mUnderline = gotCookie[2];
textColor = gotCookie[3];
textSize = gotCookie[4];
textFamily = gotCookie[5];
}
if(gotCookie2)
{
gotCookie = decodeURI(gotCookie2).split("|");
userRPM = gotCookie[0];
userRWebcam = gotCookie[1];
userEntryExitSFX = gotCookie[2];
userNewMessageSFX = gotCookie[3];
userSFX = gotCookie[4];
}
document.write('<p>colour: ' + textColor + '<br>textFamily: ' + textFamily + '<br>textSize: ' + textSize + '</p>');
}
</script>
</head>
<body>
<button onclick="getCookie()">Print cookies</button>
</body>
</html>