Home  >  Article  >  Web Front-end  >  How to implement jQuery encrypted cookies

How to implement jQuery encrypted cookies

小云云
小云云Original
2018-01-17 15:01:441661browse

This article mainly shares with you the implementation method of jquery encrypted password to cookie. It is very good and has reference value. Friends who need it can refer to it. I hope it can help everyone better master cookies.

Without further ado, I will post the code directly for you. The specific code is as follows:


 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
  <table>
    <tr>
      <th>账号:</th>
      <td><input type="text" id="username" /></td>
    </tr>
    <tr>
      <th>密码:</th>
      <td><input type="text" id="password" /></td>
    </tr>
    <tr>
      <td><button onclick="setCookie();">保存</button></td>
      <td><button onclick="getCookie();">读取</button></td>
    </tr>
  </table>
</body>
<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery.cookie/jquery.cookie.js"></script>
<script type="text/javascript" src="https://raw.githubusercontent.com/yckart/jquery.base64.js/master/jquery.base64.js"></script>
<script type="text/javascript">
  function setCookie() { //设置cookie 
    var loginCode = $("#username").val(); //获取用户名信息 
    var pwd = $("#password").val(); //获取登陆密码信息 
    $.cookie("username", loginCode);//调用jquery.cookie.js中的方法设置cookie中的用户名 
    $.cookie("pwd", $.base64.encode(pwd));//调用jquery.cookie.js中的方法设置cookie中的登陆密码,并使用base64(jquery.base64.js)进行加密 
  }
  function getCookie() { //获取cookie 
    var loginCode = $.cookie("username"); //获取cookie中的用户名 
    var pwd = $.cookie("pwd"); //获取cookie中的登陆密码 
    if (loginCode) {//用户名存在的话把用户名填充到用户名文本框 
      $("#username").val(loginCode);
    }
    if (pwd) {//密码存在的话把密码填充到密码文本框 
      $("#password").val($.base64.decode(pwd));
    }
  }
</script>
</html>

Related recommendations:

Encrypted cookie class

VUE front-end cookie simple operation example sharing

Solution to the problem of cookie loss in Ajax cross-domain access_AJAX related

jQuery combined with jQuery.cookie.js plug-in to implement skin changing function example

jQuery's Cookie usage method

The above is the detailed content of How to implement jQuery encrypted cookies. 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