使用Cookie通过Javascript设置用户名和密码 [英] Using Cookie to set username and password via Javascript
问题描述
我使用checkCookie函数来查看cookie中是否存在用户名和密码。我知道使用cookie不是一种安全的方法,但我只是试用它,所以请放宽。
首次执行时,它将提示用户输入其用户名和密码,并通过使用 setCookie();
并在重新执行时将其存储在document.cookie中代码,它只会询问用户名,并将检查cookie来检索相应的密码。下面是我的checkCookiefunction JS: -
function checkCookie(){
pre>
var username = prompt :,);
var password;
if(flag == 0){// flag = 0显示未设置cookie
password =提示(请输入您的密码:,);
username + =!;
username + = password;
alert(Username =+ username);
setCookie(username,username,365); // save input in cookie
} else //如果cookie已经设置了一些用户名+密码
{
var n = getCookie(username,username); //这从Cookie中检索密码
if(n){
alert(FLAG = 1用户的密码是+ n);
} else
alert(FLAG = 1用户密码不存在)
}
} // checkCookie函数结束
现在我对上述代码有这些问题。
1)变量,它的值是0,但是每当用户名存储在Cookie中,标志值应该变成1.在C中,我们可以通过声明一个静态变量标志,但是如何在JS中实现这一点?
2)加上我的完整的Javascript是允许我获得对应于用户名的密码,我如何使用它自动填充密码字段让我们说gmail.com网页密码字段? >
如果我使用getElementByID(password)。innerHTML = password
,它会自动填充文档的密码字段吗?
3)当我在JSFiddle.net上运行代码时,用户名cookie被保存。但是当我刷新JSFiddle.net,cookie值被重置!如何在页面重新载入时使我的Cookie值持久化(修复)?
期待一些好的建议。感谢您阅读
解决方案我将跳过讲座,给您您要求的内容:
document.getElementById(password)。value = password;
工作(在jsfiddle中测试)
I am using a checkCookie function to see whether a username and password exists in cookie.I know using cookie is not a secure method but I am only using it on trial basis, so please be lenient. On first execution, it will prompt the user for its username and password and will store it in document.cookie by using
setCookie();
and on re-executing code, it will only ask the username and will check cookie to retrieve the corresponding password. Below is my checkCookiefunction JS:-function checkCookie() { var username = prompt("Please enter your name:", ""); var password; if (flag == 0) { //flag=0 shows cookie is not set password = prompt("Please enter your password:", ""); username += "!"; username += password; alert("Username = " + username); setCookie("username", username, 365); //save input in cookie } else //if cookie is already set with some username+password { var n = getCookie("username", username); //this retrieves the password from Cookie if (n) { alert("FLAG= 1 The user's password is " + n); } else alert("FLAG= 1 User password doesnot exist ") } } //checkCookie function end
Now I have these questions regarding the above code.
1) I am trying to use a flag variable, whose value is initally 0, but whenever a username is stored in Cookie, flag value should become 1. In C, we can do this by declaring a static variable flag, but how to achieve this in JS ?
2) Plus my complete Javascript is allowing me to get password corresponding to a username, how can I use it to auto-fill password field on lets say gmail.com webpage password field?
If i usegetElementByID("password").innerHTML = password
, will it auto-fill the document's password field ?3) When I run the code on JSFiddle.net, the username cookie is saved. But when I refresh the JSFiddle.net, cookie values are reset! How can I make my cookie value persistent(fixed) on page reload?
Looking forward to some good suggestions. Thanks for reading
解决方案I'll skip the lecture and give you what you asked for:
document.getElementById("password").value = password;
seems to work (tested in jsfiddle)
这篇关于使用Cookie通过Javascript设置用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!