JavaScript uses Document to record cookies, documentcookies
Cookies sometimes help a lot for our users. For example, for websites that are not particularly important, such as the company's test platform, you need to manually enter the user name and password for each login.
Complicated. Therefore, in order to introduce other frameworks less, you can directly use js to write a code that records the user name and password on the login page. You do not need to display the password on the front-end.
Remember the user name and password directly after the first login.
<Script language = "javascript"> function login () {// TODO code remberPwd ();}
Function remberPwd () {var date = new Date (); var expiresDays = 10; // set date to date after 10 days. setTime (date. getTime () + expiresDays * 24*3600*1000); // set the cookie userName and password to expire the document after 10 days. cookie = "userName = admin; expires =" + date. toGMTString (); document. cookie = "password = 12345; expires =" + date. toGMTString ();}
The Value obtained based on the Cookie key is similar to the Map in Java.
Function getCookie (name) {var cookieValue = null; // return the cookie value.
// Cookie is a string separated by semicolons
Var cookieArray = document. cookie; // obtain the cookie string if (cookieArray! = Null & cookieArray! = '') {Var cookies = cookieArray. split (';'); // cut all the obtained cookies into an array for (var I = 0; I <cookies. length; I ++) {var cookie = cookies [I]; // obtain the cookie array var nt = cookie. substring (0, name. length + 1); if (nt. indexOf (name )! =-1) {// if this cookie exists, take out the cookie value cookieValue = cookie. substring (name. length + 2, cookie. length); break }}return cookieValue ;}
// Because the platform has jquery, $ (function () {<span style = "white-space: pre "> </span> var name = getCookie (" userName "); <span style =" white-space: pre "> </span> if (name = null) return; <span style = "white-space: pre"> </span> var pwd = getCookie ("password"); <span style = "white-space: pre "> </span> if (pwd = null) return; <span style =" white-space: pre "> </span> // code for TODO form submission });