gpt4 book ai didi

Javascript cookie 来记住输入值

转载 作者:行者123 更新时间:2023-11-28 19:59:02 24 4
gpt4 key购买 nike

我需要为我的学习制作一个带有cookie的网站,但我就是无法让它工作。我试图到处寻找解决方案,但找不到我需要的东西,因为我的老师想要他教我的简单代码。我需要制作一个 cookie 来记住您的用户名,这样当您下次登录时(不需要识别密码)您会收到消息“欢迎再次‘用户’”。它必须仅使用简单的 Javascript 来完成!

这是我的 JavaScript 文件(cookies.js):

function setCookie(cname,cvalue,exdays)
{
var d = new Date();
d.setTime(d.getTime()+(exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname+"="+cvalue+"; "+expires;
}

function getCookie(cname)
{
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++)
{
var c = ca[i].trim();
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 = document.getElementById("username").value;
if (user!="" && user!=null)
{
setCookie("username",user,30);
}
}
}

这是我的登录页面(inloggen.html):

<!DOCTYPE html>
<html>
<head>
<title>Inloggen</title>
<meta charset="iso-8859-1">
<meta name="keywords" content="Javascript">
<meta name="author" content="Arjan Oskam">
<meta name="description" content="Javascript">
<link rel="Stylesheet" href="css/style.css">
<script type="text/javascript">
alert("Deze webpagina maakt gebruik van cookies");
</script>
<script type="text/javascript" src="javascript/cookies.js"></script>
</head>
<body>
<script>
function checkCookie()
{
var user = getCookie("username");
if (user != "")
{
alert("Welcome again " + user);
}
else
{
user = document.getElementById("username").value;
if (user != "" && user != null)
{
setCookie("username", user, 30);
}
}
}
</script>
<div id="wrapper">
<div id="head">
<div><img src="images/logo.gif" id="logo" alt="logo"></div><!-- #Logo -->
<div id="banner"><img src="images/banner.jpg" alt="banner"></div>


</div><!-- #Head -->
<div id="menu">
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="id-ontwikkelaar.html">ID-Ontwikkelaar</a></li>
<li><a href="quiz.html">Quiz</a></li>
<li><a href="formulier.html">Registreren</a></li>
<li><a href="inloggen.html">Inloggen</a></li>
</ul>
</nav>
</div><!-- Menu -->
<div id="inhoud">
<br>
<br>
<h2>Inloggen</h2>
<div id="tekst">
<form class="uitlijnen" id="form" onsubmit="setCookie();
getCookie();
checkCookie();">
<br>
Gebruikersnaam: <input type="text" name="username" id="username" placeholder="Typ hier uw username" required><br><br>
Wachtwoord: <input type="password" name="wachtwoord" placeholder="Typ hier uw wachtwoord" required><br><br>
<input type="submit" value="inloggen" class="button">
</form>
</div><!-- #Tekst -->
</div><!-- #Inhoud -->
</div><!-- #Wrapper -->
</body>
</html>

希望你能帮助我!

最佳答案

这里,代码略有更新,您没有将参数传递给 setCookie 函数

<!DOCTYPE html>
<html>
<head>
<title>Inloggen</title>
<meta charset="iso-8859-1">
<meta name="keywords" content="Javascript">
<meta name="author" content="Arjan Oskam">
<meta name="description" content="Javascript">
<link rel="Stylesheet" href="css/style.css">
<script language="javascript">
function setCookie()
{
checkCookie();
var cname = "username";
var cvalue = document.getElementById("username").value;
var exdays = 7;
var d = new Date();
d.setTime(d.getTime()+(exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname+"="+cvalue+"; "+expires;
}

function getCookie(cname)
{
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++)
{
var c = ca[i].trim();
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);
}
}
</script>
</head>
<body>
<div id="wrapper">
<div id="head">
<div><img src="images/logo.gif" id="logo" alt="logo"></div>
<!-- #Logo -->
<div id="banner"><img src="images/banner.jpg" alt="banner"></div>
</div>
<!-- #Head -->
<div id="menu">
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="id-ontwikkelaar.html">ID-Ontwikkelaar</a></li>
<li><a href="quiz.html">Quiz</a></li>
<li><a href="formulier.html">Registreren</a></li>
<li><a href="inloggen.html">Inloggen</a></li>
</ul>
</nav>
</div>
<!-- Menu -->
<div id="inhoud"> <br>
<br>
<h2>Inloggen</h2>
<div id="tekst">
<form class="uitlijnen" id="form" onsubmit="setCookie();" method="post">
<br>
Gebruikersnaam:
<input type="text" name="username" id="username" placeholder="Typ hier uw username" required>
<br>
<br>
Wachtwoord:
<input type="password" name="wachtwoord" placeholder="Typ hier uw wachtwoord" required>
<br>
<br>
<input type="submit" value="inloggen" class="button">
</form>
</div>
<!-- #Tekst -->
</div>
<!-- #Inhoud -->
</div>
<!-- #Wrapper -->
</body>
</html>

关于Javascript cookie 来记住输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22009910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com