Cookies is html and javascript

Cookies is html and javascript

本文关键字:javascript and html is Cookies      更新时间:2023-09-26

当用户登录时,我会检查他的cookie是否已经保存

如果是:欢迎用户

如果没有:为他设置一个cookie

在InternetExplorer而不是GoogleChrome中运行时,什么都没发生我的html代码很清楚,但经过大量调试后,我认为问题出在getCookie()中

<html>
<head>
<script>
function setCookie(c_name,value){
document.cookie=c_name+ "=" +escape(value);
}

function getCookie(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));
    }
}
return ""; 
}


function checkCookie(){
username=getCookie('username');
password=getCookie('password');
if (username!="")
{ alert('Welcome again '+username+'!'); }
else  
{  
username=prompt('Please enter your name:',"");  
if (username!="")    
{setCookie('username',username);} 
}
}

</script>
</head>

<body>
<form>
    <h1 align="center">Login Page</h1>
    username:<br/>
    <input type="text" name="username"/> *  <br />  
    password: <br/>
    <input type="password" name="password"/> *<br />
    <input type="button" value="Log in" onclick="checkCookie()"/>
</form>
</body>

</html>

似乎是"!="-->更改为!==的问题在函数中:

function checkCookie() {
    username = getCookie('username');
    password = getCookie('password');
    if (username !== "") {
        alert('Welcome again ' + username + '!');
    } else {
        username = prompt('Please enter your name:', "");
        if (username !== "") {
            setCookie('username', username);
        }
    }
}

工作小提琴