2011-12-22 9 views
1

何か助けてもらえますか? Javascriptを使って、私はクッキーを設定して、ページを更新しています。私の現在壊れコード:クッキーを設定してもリフレッシュしない

var username=getCookie("username"); 
if (username!=null && username!="") 
{ 
    document.write("Welcome, " + username + "! <br/> <input type='button' value='Click to reset your name!' onclick='setCookie(\"username\",\"\",-1);' onmouseup='window.location.reload(true);'/>"); 
} 
else 
{ 
    username=prompt("Please enter your name:","Nothing is stored on the server."); 
    if (username!=null && username!="") 
    { 
     setCookie("username",username,365); 
    } 
    window.location.reload(true); 
} 

取得クッキー:

function getCookie(c_name) 
{ 
var i,x,y,ARRcookies=document.cookie.split(";"); 
for (i=0;i<ARRcookies.length;i++) 
    { 
    x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("=")); 
    y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1); 
    x=x.replace(/^\s+|\s+$/g,""); 
    if (x==c_name) 
    { 
    return unescape(y); 
    } 
    } 
} 

問題は、コードの最初の部分の他に、{}の部分です。前もって感謝します! :) 私のコードや書式が間違っているとお詫び申し上げます。 :(

+0

あなたのコードをフォーマットしてください。今後の対応方法については、投稿を編集してください。 – Kenaniah

+1

それは私ですか、あるいはクローンしただけですか? – Kenaniah

+0

私は自分のコードに使用していますが、私は自動的にリフレッシュしようとしています。この投稿の目的は何ですか? – Andrey

答えて

0

我々はgetCookieの定義を参照することができますしてください?クッキーが設定されていない場合、それは返さんか?

あなたはJSのエラーコンソールを見て?

コードは、以下の再フォーマットした。

var username=getCookie("username"); 
if (username!=null && username!="") 
{ 
    document.write("Welcome, " + username + "! <br/> 
        <input type='button' value='Click to reset your name!'    
         onclick='setCookie(\"username\",\"\",-1);' 
         onmouseup='window.location.reload(true);'/>"); 
} 
else 
{ 
    username=prompt("Please enter your name:","Nothing is stored on the server."); 
    if (username!=null && username!="") 
    { 
     setCookie("username",username,365); 
    } 
    window.location.reload(true); 
} 
// This last } is superfluous and may indicate issues with brackets 
} 
+0

メインの投稿にgetCookieを追加しました。 – Andrey

関連する問題