我試圖根據cookie是否設置在現有網頁上編寫一段文本。 Cookie部分工作正常,但document.write導致文本出現在新的空白頁面上。document.write in html/php doc寫入新頁面
<script type="text/javascript">
function getCookie(c_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==c_name)
{
return unescape(y);
}
}
}
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 checkCookie()
{
var username=getCookie("username");
if (username!=null && username!="")
{
alert("Welcome again " + username);
}
else
{
username=prompt("Please enter your name:","");
document.write ("hello");
if (username!=null && username!="")
{
setCookie("username",username,7);
}
}
}
</script>
<body onload="checkCookie()">
</body>
我想我需要使用z-index莫名其妙,但似乎無法讓它工作。如果有人能解釋我如何讓我的'document.write'行出現在頁面上已存在的東西的頂部,我將非常感激。所有這些都發生在joomla網站的一個頁面上,代碼位於其中一個模塊的php文件中。謝謝大家。
Rob。
該行爲是預期的。請參閱:https://developer.mozilla.org/en/document.write#Notes解決方案:不要使用'document.write'。 –