JavaScript uses Document to record cookies, documentcookies

Source: Internet
Author: User

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 });


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.