getCookie IE问题

时间:2012-03-26 22:43:37

标签: javascript internet-explorer compatibility

对于我的生活,我无法弄清楚为什么以下getcookie代码在IE中不起作用。这正是我想要发生的事情,并且在所有其他浏览器(FireFox,Safari,Chrome等)上运行良好。

我一直得到'document.getElementByID(...)childNodes.0'为null或者不是对象......

请告知。

谢谢, 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')" />&nbsp;
        <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>

1 个答案:

答案 0 :(得分:1)

IE只在包含whiteSpaces时忽略textNodes,因此IE将不会在元素中找到任何childNodes。

你可以用这个:

try{document.getElementById("showname").childNodes[0].nodeValue = val_c[0];}
catch(e){document.getElementById("showname").appendChild(document.createTextNode(val_c[0]));}