2012-02-01 3 views
0

자바 스크립트 쿠키를 연구 중입니다. 처음 사용자 입력 자신의 번호가 365자바 스크립트 쿠키 처리

  • 사용자의 방문을 다시 내 사이트 그/그녀 돈에 쿠키 이름, 값 및 만료 날짜를 저장할 때 기본적으로 내가 원하는

    1. 입니다 쿠키가 아직 살아 있거나 브라우저에서 아직 삭제하지 않은 상태에서 더 이상 전화 번호를 입력하지 않으면 그 사람이 내 홈페이지로 리디렉션됩니다.

    여기에 지금까지 자바 스크립트에 내 코드입니다 :

    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 
    
    어쩌면

    내 코딩 스타일이 잘못되었습니다. 내 코드를 다시 쓸 수 있습니다. 나는 아직도 자바 스크립트에 새로운 사람이다. 당신에게

  • 답변

    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'속성을 읽을 수 없습니다. 이는 사용자가 아직 전화 번호를 입력하지 않고 브라우저에 쿠키를 저장 한 경우에만 발생합니다. –