Jquery.cookie.js method to implement user login to save password function _jquery

Source: Internet
Author: User
Tags eval

The example in this article describes the Jquery.cookie.js method to implement user login to save password. Share to everyone for your reference, specific as follows:

Need to import JS has jquery.js and jquery.cookie.js

<script type= "Text/javascript" src= "jquery-1.5.2.js" ></script>
<script type= "Text/javascript" Src= "Jquery.cookie.js" ></script>

First try to get the value of the cookie when the page loads, and if the cookie has a value, fill in the input box with the value that is fetched, and select the check box that saves the password

JQuery (function () {
 //Get cookie value
 var username = $.cookie (' username ');
 var password = $.cookie (' password ');
 Fills the fetched value into the input box
 $ (' #uName '). val (username);
 $ (' #psw '). val (password); 
 if (username!= null && username!= ' && password!= null && password!= ') {//Select the Save secret check box
  $ (" #remember_password "). attr (' checked ', true);
 }
);

Determine whether the Save Password check box is selected at logon, if selected, create a cookie (the validity period can be determined by itself, the following cookie is valid for 7 days), or delete the cookie if it is not selected (because the password was saved last time, this logon option cancels saving the password, So you need to delete the cookie to make the cookie do not have a value the next time you log on.
Pay special attention to delete cookies, many articles on the Web using the method is $.cookie (' username ', null), but I use this method does not work, every time the cookie exists after the login, I try to use $.cookie (' username ', ' ' There will still be a problem, the program becomes unable to save the password.

The handler function for submitting the form is
Login ()
{
 var uName =$ (' #uName '). Val ();
 var PSW = $ (' #psw '). Val ();
 if ($ (' #remember_password '). attr (' checked ') = = True) {//Save password
  $.cookie (' username ', uName, {expires:7,path: '/'});
  $.cookie (' Password ', PSW, {expires:7,path: '/'});
 } else{//Delete Cookie
  $.cookie (' username ', ', {Expires:-1, Path: '/'});
  $.cookie (' Password ', ', {Expires:-1, Path: '/'});
 //....
 Actions for submitting a form
}

Save password check box for login page

<input type= "checkbox" Name= "Remember_password" id= "Remember_password"/> <span id= "Span_tip" style= "
margin-bottom:-2px;color:white;font-size:12px; " > Remember password
</span>

PS: here again for you to recommend a very useful JavaScript compression, formatting and encryption tools, very powerful (for the code to encrypt the friend may wish to try the JS encryption function here):

JavaScript compression/formatting/encryption tool:http://tools.jb51.net/code/jscompress

In addition, the above JS tool encryption using the Eval function encryption form, this site also provides the following for the Eval function encryption decryption tool, very powerful and practical!

JS eval method on-line encryption and decryption tool : Http://tools.jb51.net/password/evalencode

More interested readers of jquery-related content can view this site: "JQuery Cookie Tips Summary", "jquery table (table) Operation Tips Summary", "jquery drag-and-drop effects and tips summary", "jquery extension Techniques Summary", " jquery Common Classic Effects Summary "jquery animation and special effects usage Summary", "jquery selector usage Summary" and "jquery common Plug-ins and Usage summary"

I hope this article will help you with the jquery program design.

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.