0
對於我的生活,我無法弄清楚爲什麼下面的getcookie代碼在IE中不起作用。這正是我想要發生的事情,並且在所有其他瀏覽器(FireFox,Safari,Chrome等)上運行良好。getCookie IE問題
我不斷收到 '的document.getElementById(...)childNodes.0' 爲空或不是對象...
請指教。
感謝, JG
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>Example using cookie</title>
<script type="text/javascript">
// function to set the cookie
function SetCookie() {
// variabiles that gets the form values
var ssqname = document.prefers.ssqname.value;
// Set the expiration time, 2 days
var twoDays = 2*24*60*60*1000
var expDate = new Date()
expDate.setTime(expDate.getTime()+twoDays);
// Create the cookies
document.cookie = "cookie1" + "=" + escape(ssqname) + "; expires=" + expDate.toGMTString() ;
}
// This function checks if exist the cookie with the name passed in the argument
// If that cookie exists gets and return its value
function checkCookie(c_name) {
if (document.cookie.length>0) {
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1) {
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) {
c_end=document.cookie.length;
}
return unescape(document.cookie.substring(c_start,c_end));
}
}
// If the cookie not exists
return "You have not yet added a preference";
}
// This function gets the cookie values from the checkCookie() and stores them in an array
// Dysplays it the page the cookie values
function getCookie() {
nr = getCookie.arguments.length
var val_c = new Array(nr)
var a = 0
for (var i=0; i<nr; i++) {
valoare=checkCookie(getCookie.arguments[i]);
if (valoare!=null && valoare!="") {
val_c[a] = valoare;
a++
}
}
// Dysplays the cookie values, at the indicated id
document.getElementById("showname").childNodes[0].nodeValue = val_c[0];
}
// This function deletes the cookies
function delCookies() {
nr_c = delCookies.arguments.length
var ThreeDays = 3*24*60*60*1000;
var expDate = new Date();
expDate.setTime (expDate.getTime() - ThreeDays);
for (var n=0; n<nr; n++) {
document.cookie = delCookies.arguments[n] + "=DataDel; expires=" + expDate.toGMTString();
}
}
</script>
</head>
<body>
<table border="1" bordercolor="#8888fe" width="580" cellpadding="2" cellspacing="0">
<tr><td>
<form name="prefers">
Your Name:
<input type="text" name="ssqname" size="20" maxlength="40" /><br /><br />
<input type="button" value="Start" name="buton" onclick="SetCookie()" />
</form>
</td><td>
<form name="prefers2">
<input type="button" value="Show Name" name="buton" onclick="getCookie('cookie1')" />
<input type="button" value="Delete cookie" name="Clear" onclick="delCookies('cookie1')" />
</form>
<body onLoad="getCookie('cookie1')">
<b>Your Name - </b><span id="showname"> </span><br />
</td></tr>
</table>
</body>
</html>
那麼,有沒有一個修復我的代碼? – jgrannis 2012-03-26 22:52:52
工作!謝謝! – jgrannis 2012-03-26 22:58:24