首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >getCookie IE问题

getCookie IE问题
EN

Stack Overflow用户
提问于 2012-03-27 06:43:37
回答 1查看 865关注 0票数 0

我真搞不懂为什么下面的getcookie代码不能在IE中工作。这正是我想要的,并且在所有其他浏览器(FireFox、Safari、Chrome等)上都工作得很好。

我一直收到‘document.getElementByID(...)object Nodes.0’为空或不是对象...

请给我建议。

谢谢,JG

代码语言:javascript
运行
复制
<!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>
EN

Stack Overflow用户

回答已采纳

发布于 2012-03-27 06:51:25

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

您可以使用以下命令:

代码语言:javascript
运行
复制
try{document.getElementById("showname").childNodes[0].nodeValue = val_c[0];}
catch(e){document.getElementById("showname").appendChild(document.createTextNode(val_c[0]));}
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9880973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档