【问题标题】:getCookie IE issuegetCookie IE 问题
【发布时间】:2012-03-26 22:43:37
【问题描述】:

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

我不断收到“document.getElementByID(...)childNodes.0”为空或不是对象...

请指教。

谢谢, 杰哥

<!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>

【问题讨论】:

    标签: javascript internet-explorer compatibility


    【解决方案1】:

    当文本节点只包含空格时,IE 会忽略它们,因此 IE 不会在元素内找到任何子节点。

    你可以使用这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 2010-10-14
      • 2011-10-12
      相关资源
      最近更新 更多