Home  >  Article  >  Web Front-end  >  JavaScript cookie basic application (method of recording user name)

JavaScript cookie basic application (method of recording user name)

亚连
亚连Original
2018-05-19 15:49:241362browse

This article mainly introduces the method of recording user names in the basic application of javascript cookies, involving simple applications of javascript based on cookies for data storage. Friends in need can refer to it

The examples in this article describe the basics of javascript cookies The application's method of recording usernames. Share it with everyone for your reference, the details are as follows:

There is an article about the basics of cookies, which encapsulates cookie.js. Below we use an example to apply this js.

The most common one is to remember the user name. After the user logs in once, the user's account and password are recorded through cookies, so that the next time the page is opened, the user does not need to enter the account and password again. Attached code:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
  <meta charset="UTF-8">
  <title>cookie的应用——记住用户名</title>
</head>
<body>
  <form action="#" id="myform">
    <label for="username">用户名:</label><input type="text" name="username" id="username" />
    <label for="userpass">密码:</label><input type="password" name="userpass" id="userpass" />
    <input type="submit" value="登录" />
    <a href="javascript:;">清除记录</a>
  </form>
</body>
</html>
<script type="text/javascript" src="cookie.js"></script>
<script type="text/javascript">
window.onload = function(){
  var oForm = document.getElementById(&#39;myform&#39;);
  var oTxt1 = document.getElementById(&#39;username&#39;);
  var oTxt2 = document.getElementById(&#39;userpass&#39;);
  var oClear = document.getElementsByTagName(&#39;a&#39;)[0];
  oTxt1.value = getCookie(&#39;username&#39;);
  oTxt2.value = getCookie(&#39;userpass&#39;);
  oForm.onsubmit = function(){
    setCookie(&#39;username&#39;,oTxt1.value,30);
    setCookie(&#39;userpass&#39;,oTxt2.value,30);
  }
  oClear.onclick = function(){
    removeCookie(&#39;username&#39;);
    removeCookie(&#39;userpass&#39;);
    oTxt1.value = &#39;&#39;;
    oTxt2.value = &#39;&#39;;
  }
}
</script>

PS: Post the cookie.js in the previous article here for everyone to view:

function setCookie(name,value,hours){
 var d = new Date();
 d.setTime(d.getTime() + hours * 3600 * 1000);
 document.cookie = name + &#39;=&#39; + value + &#39;; expires=&#39; + d.toGMTString();
}
function getCookie(name){
 var arr = document.cookie.split(&#39;; &#39;);
 for(var i = 0; i < arr.length; i++){
  var temp = arr[i].split(&#39;=&#39;);
  if(temp[0] == name){
   return temp[1];
  }
 }
 return &#39;&#39;;
}
function removeCookie(name){
 var d = new Date();
 d.setTime(d.getTime() - 10000);
 document.cookie = name + &#39;=1; expires=&#39; + d.toGMTString();
}

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Detailed explanation of JavaScript cookie and simple example application (picture and text tutorial)

Understanding and understanding of JavaScript cookie Use

javascript cookieUsage (concept, setting, reading and deletion)

##

The above is the detailed content of JavaScript cookie basic application (method of recording user name). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn