Home > Article > Web Front-end > JavaScript cookie basic application (method of recording user name)
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('myform'); var oTxt1 = document.getElementById('username'); var oTxt2 = document.getElementById('userpass'); var oClear = document.getElementsByTagName('a')[0]; oTxt1.value = getCookie('username'); oTxt2.value = getCookie('userpass'); oForm.onsubmit = function(){ setCookie('username',oTxt1.value,30); setCookie('userpass',oTxt2.value,30); } oClear.onclick = function(){ removeCookie('username'); removeCookie('userpass'); oTxt1.value = ''; oTxt2.value = ''; } } </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 + '=' + value + '; expires=' + d.toGMTString(); } function getCookie(name){ var arr = document.cookie.split('; '); for(var i = 0; i < arr.length; i++){ var temp = arr[i].split('='); if(temp[0] == name){ return temp[1]; } } return ''; } function removeCookie(name){ var d = new Date(); d.setTime(d.getTime() - 10000); document.cookie = name + '=1; expires=' + 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!