天天看點

js實作登入時記住使用者名

在頁面中如何擷取cookie值呢?

如果是jsp的話,可以通過servlet的對象request 擷取cookie,可以

參考:http://hw1287789687.iteye.com/blog/2050040

如果要求登入頁面是html呢?html頁面中如何擷取cookie呢?

直接上代碼了

頁面:logininput.html

代碼:

js實作登入時記住使用者名

<!doctype html public "-//w3c//dtd html 4.01 transitional//en">  

<html><head>  

<meta http-equiv="content-type" content="text/html; charset=utf-8">  

<!-- base href="http://localhost:8080/shop_goods/" -->  

<title>user login</title>  

<meta http-equiv="pragma" content="no-cache">  

<meta http-equiv="cache-control" content="no-cache">  

<meta http-equiv="expires" content="0">  

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  

<meta http-equiv="description" content="this is my page">  

<script language="javascript" src="/shop_goods/js/common_util.js" type="text/javascript"></script>  

<!-- 

    <link rel="stylesheet" type="text/css" href="styles.css"> 

    -->  

<style type="text/css">  

.errormessage li {  

    list-style-type: none;  

    margin-left: 0  

}  

</style>  

</head>  

<body>  

<script type="text/javascript">  

//擷取cookie的值  

function getcookie(cookiekey){  

    var cookies = document.cookie ? document.cookie.split('; ') : [];  

    for (var i = 0, l = cookies.length; i < l; i++) {  

        var parts = cookies[i].split('=');  

        if(parts.length>1){  

            if(parts[0]==cookiekey){  

                //username1=;  

                return parts[1];  

            }  

        }  

    }  

    return '';  

var username1='';  

window.onload=function(){  

    //cookie的key是'useremail'  

    username1=getcookie('useremail');  

    //alert("username1:"+username1);  

    var issave222=com.whuang.hsj.$$one("issave");  

    if(username1){  

        if(username1!='' && username1!=null &&username1!=undefined){  

            com.whuang.hsj.$$one("user.username").value=username1;  

            issave222.checked=true;  

        }else{  

            issave222.checked=false;  

    }else{  

        issave222.checked=false;  

</script>  

    this is login page.  

    <br>  

    <a href="/shop_goods/">index</a>  

        <a href="/shop_goods/user/registeruser.jsp">register user</a>  

    <font color="red"></font>  

    <font style="font-weight: bold" color="red"> </font>  

        <form action="/shop_goods/user/login" method="post">  

            <table>  

                <tbody><tr>  

                    <td>username:</td>  

                    <td><input name="user.username" id="user_username" type="text">  

                    </td>  

                </tr>  

                <tr>  

                    <td>password:</td>  

                    <td><input name="user.password" id="user_password" type="text">  

                <tr> <td colspan="2"> <input name="issave" value="save" type="checkbox"> 儲存使用者名</td></tr>  

                    <td colspan="2"><input id="" value="login" type="submit">  

</td>  

            </tbody></table>  

</form></body></html>