javascript - 如何使用 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
所以我得到这样的结果:
- 首先
onload
,我希望出现一个提示,其功能类似于小提琴中的第二个提示,其中有一个“?” 在最后。 deny()
我希望使用我创建的函数处理第二个提示中给出的输入。
3.仅当输入为“bla”时,才应将其保存为cookie,并应重定向到某个链接。4.如果输入不是“bla”,则应将其重定向到javascript:void(0);
解决方案
推荐阅读
- azure - Azure VM - 注册表更改阻止启动机器
- javascript - 如何模拟 AWS sqs 调用以进行单元测试
- arduino - IMU 设备 - 如何在设备垂直时通过计算欧拉角获得水平旋转角
- java - 如何将 JPA 默认获取更改为 LAZY
- javascript - ployly.js 时间序列图表上的 yaxis 自定义标签有什么解决方案吗?
- android - 应用在 Play 商店上线时,有 0 台设备支持 Android 应用
- sql - 需要公式先前值的公式 (SQL Server 2017)
- php - 如何将单个字符串变量或数组绑定到 mysqli 插入语句
- html - 如何以简单的方式使用来自 QRCode-Monkey 的 API 代码?
- c++ - SFML 静态链接问题:sfml-system-2.dll not found