首页 > 解决方案 > 如何使用 cookie 使用提示登录

问题描述

嗨,我想创建一个使用w3 学校等 cookie 的提示。我还想添加一个我创建的功能。

     

                   <body onload="checkCookie();deny()">
        <script type="text/javascript">
        //This code has been copied from w3 schools link
        function setCookie(cname,cvalue,exdays){
        var date=new Date();
        date.setTime(date.getTime()+(exdays*24*60*60*1000));
        var expires="expires="+date.toGMTString();
        document.cookie=cname+"="+cvalue+";"+expires+";path=/";}
        function getCookie(cname){
        var name=cname +"=";
        var decodedCookie=decodeURIComponent(document.cookie);
        var ca=decodedCookie.split(';');
        for(var i = 0; i<ca.length; i++){
        var c=ca[i];
        while (c.charAt(0)==' '){
        c=c.substring(1);}
        if (c.indexOf(name)==0){
        return c.substring(name.length,c.length);}}
        return "";}
        function checkCookie(){
        var user=getCookie("username");
        if (user!=""){alert("Welcome again "+user);} 
        else{
        user=prompt("Please enter your name:");
        if (user!=""&& user!=null){
        setCookie("username",user,30);}}}
        //The below code I would like to integrate in the code above
        function deny(){
        var sign=prompt("Please enter your name?");
        sign=sign.toLowerCase()
        var hi="Hi! ",we=" we are processing.",yes="Access allowed.",no="Access denied."
        switch(sign){
        case "Bla","bla":
        alert(hi+"Bla"+we)
        alert(yes)
        break;
        case "":
        alert("You have not entered a name!")
        break;
        default:
        alert(hi+sign+we)
        alert(no+"Sorry we don't recognize you!")}}</script>
        </body>

小提琴的工作示例

你能告诉我如何将这两者结合起来,functions所以我得到这样的结果:

  1. 首先onload,我希望出现一个提示,其功能类似于小提琴中的第二个提示,其中有一个“?” 在最后。
  2. deny()我希望使用我创建的函数处理第二个提示中给出的输入。

3.仅当输入为“bla”时,才应将其保存为cookie,并应重定向到某个链接。4.如果输入不是“bla”,则应将其重定向到javascript:void(0);

标签: javascriptcookiescaseprompt

解决方案


推荐阅读