如何解决如何使用cookie使用提示登录
您好,我想创建一个使用 w3 schools 等 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
结合起来,以便得到如下结果:
- 第一个
onload
我希望出现一个提示,其功能类似于 fiddle 中的第二个提示,其中带有“?”最后。 - 我希望使用我创建的
deny()
函数处理第二个提示中给出的输入。
3.仅当输入为“bla”时,才应将其保存为 cookie 并重定向到某个链接。
4.如果输入不是“bla”,则应将其重定向到 javascript:void(0);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。