2012-02-01 7 views
0

Javascript Cookieを学習しています。初めてのユーザ入力その数はそれが365Javascript Cookieの処理

  • ユーザーの訪問再び私のサイトに彼/彼女ドンへのクッキーの名前、値、および有効期限を節約する際に基本的に私が欲しいもの

    1. ですクッキーがまだ生きているかブラウザでまだ削除されていない限り、彼の番号をもう一度入力する必要があります、彼/彼女は私のホームページにリダイレクトされます。ここで

    は、これまでにJavaScriptで私のコードです:

    function checkCookie() { 
        var mob_tel=getCookie("mob_tel"); 
        if (mob_tel!=null && mob_tel!="") { 
         alert("Welcome again " + mob_tel); 
        } else { 
         set_name(""); 
        } 
    } 
    
    function set_name(form) { 
        var mob_tel = form.mobtel.value 
        if (mob_tel != "") { 
         if (confirm("Are you sure you want this saved as your number?")) { 
          setCookie ("mob_tel", mob_tel, 365); 
          //window.history.go(0); 
         } 
        } else alert("Geez, at least enter something, entering nothing will cause an error."); 
    } 
    

    私のhtml体

    <body onload="checkCookie()"> 
        <form> 
          Enter Mobile Number: <input type="text" name="mobtel"><br> 
          <input type="submit" value="Save Cookie" onclick="set_name(this.form)"> 
        </form> 
    </body> 
    

    それすべての作品が、私の放火犯で:

    Uncaught TypeError: Cannot read property 'value' of undefined 
    set_name 
    checkCookie 
    (anonymous function) 
    onload 
    

    たぶん私はコーディングスタイルは間違っています。私は自分のコードを書き直すために開いています。私はまだjavascriptに新しいです。

  • 答えて

    0

    ここで作業バージョンです。

    これは私がやったことです:
    1 - getCookie()setCookie()は定義されていません。私はそれらを補うためにW3Schools関数を使用しました。
    2 - いくつかのスクリプトエラー(セミコロンなどがありません)がありましたが、それらを修正しました。
    3 - set_name()関数の場合、電話番号を保持している入力にアクセスするためにDOMクエリに変更しました。

    <script> 
    function checkCookie() { 
        var mob_tel=getCookie("mob_tel"); 
        if (mob_tel!=null && mob_tel!="") { 
         alert("Welcome again " + mob_tel); 
        } else { 
         set_name(""); 
        } 
    } 
    
    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); 
         } 
        } 
    } 
    
    function setCookie(c_name,value,exdays) { 
        var exdate=new Date(); 
        exdate.setDate(exdate.getDate() + exdays); 
        var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()); 
        document.cookie=c_name + "=" + c_value; 
    } 
    
    function set_name(form) { 
        var mob_tel = document.getElementById('mobtel').value; 
        if (mob_tel != "") { 
         if (confirm("Are you sure you want this saved as your number?")) { 
          setCookie ("mob_tel", mob_tel, 365); 
          //window.history.go(0); 
         } 
        } else alert("Geez, at least enter something, entering nothing will cause an error."); 
    } 
    </script> 
    <body onload="checkCookie()"> 
        <form> 
          Enter Mobile Number: <input type="text" name="mobtel" id="mobtel"><br> 
          <input type="submit" value="Save Cookie" onclick="set_name(this.form)"> 
        </form> 
    </body> 
    
    1

    あなたに名前を追加形成:

     
    
    <form name="your_form_name_here"> 
        Enter Mobile Number: <input type="text" name="mobtel" value=""><br> 
        <input type="submit" value="Save Cookie" onclick="set_name(this.form)"> 
    </form> 
     
    
    +0

    Uncaught TypeError:未定義のプロパティ 'value'を読み取ることができません。これは、ユーザが自分の番号を入力せずにブラウザにクッキーを保存した場合にのみ発生します。 –