2012-03-26 7 views
0

私の人生の間、私は次のgetcookieコードがIEでうまくいかない理由を理解できません。それはまさに私が起こりたいもので、他のすべてのブラウザ(FireFox、Safari、Chromeなど)でうまく動作します。getCookie IEの問題

私は 'の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

彼らは唯一の空白が含まれている場合、IEは、要素内の任意ののchildNodesを見つけることができませんので、IEは、textNodesを無視します。

あなたはこれを使用することがあります。

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

だから私のコードの修正がありますか? – jgrannis

+0

これがうまくいった!ありがとうございました! – jgrannis