本文介绍下jquery 记住登录信息的方法,引入jquery.cookie.js文件,实现记住登录信息,有需要的朋友参考下。

首先,导入jquery.cookie.js

$(function(){
//初始化页面时验证是否记住了密码
if ($.cookie("autologin") == "true") {
$("#autologin").attr("checked", true);
$("#account").val($.cookie("account"));
$("#pwd").val($.cookie("pwd"));
}
}); //保存用户信息
function saveUserInfo() {
  var xuanzhong = document.getElementById("autologin").checked;
  if (xuanzhong) {
    var account = $("#account").val();
    var pwd = $("#pwd").val();
    $.cookie("autologin", "true", { expires: 7 }); // 存储一个带7天期限的 cookie
    $.cookie("account", account, { expires: 7 }); // 存储一个带7天期限的 cookie
  $.cookie("pwd", pwd, { expires: 7 }); // 存储一个带7天期限的 cookie
  }
  else {
    $.cookie("autologin", "false", { expires: -1 });
    $.cookie("account", '', { expires: -1 });
    $.cookie("pwd", '', { expires: -1 });
  }
}

jquery.cookie.js

/**
* Cookie plugin
*
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/ /**
* Create a cookie with the given name and value and other optional parameters.
*
* @example $.cookie('the_cookie', 'the_value');
* @desc Set the value of a cookie.
* @example $.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});
* @desc Create a cookie with all available options.
* @example $.cookie('the_cookie', 'the_value');
* @desc Create a session cookie.
* @example $.cookie('the_cookie', null);
* @desc Delete a cookie by passing null as value.
*
* @param String name The name of the cookie.
* @param String value The value of the cookie.
* @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
* If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
* If set to null or omitted, the cookie will be a session cookie and will not be retained
* when the the browser exits.
* @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
* @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
* @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
* require a secure protocol (like HTTPS).
* @type undefined
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/ /**
* Get the value of a cookie with the given name.
*
* @example $.cookie('the_cookie');
* @desc Get the value of a cookie.
*
* @param String name The name of the cookie.
* @return The value of the cookie.
* @type String
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
}
var path = options.path ? '; path=' + options.path : '';
var domain = options.domain ? '; domain=' + options.domain : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else { // only name given, get cookie
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};

jQuery实例-记住登录信息的更多相关文章

  1. 模拟淘宝登录和购物车功能:使用cookie记录登录名,下次登录时能够记得上次的登录名,使用cookie模拟购物车功能,使用session记住登录信息并验证是否登录,防止利用url打开网站,并实现退出登录功能

    Login <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...

  2. 工作任务:模拟淘宝登录和购物车功能:使用cookie记录登录名,下次登录时能够记得上次的登录名,使用cookie模拟购物车功能,使用session记住登录信息并验证是否登录,防止利用url打开网站,并实现退出登录功能

    登入界面<% Cookie[] cks =request.getCookies(); String str=null; for(Cookie ck:cks) { if(ck.getName(). ...

  3. Asp.Net MVC记住用户登录信息 下次登录无需输入密码

    有的时候做网站,就需要记住用户登录信息,下次再登录网站时,不用重复输入用户名和密码,原理是浏览器的cookie把状态给记住了! 那么具体是怎么实现的呢?下面博主将一部分代码贴出来,想要完整版的Demo ...

  4. 使用 jQuery Ajax 异步登录,并验证用户输入信息(maven)

    使用 jQuery Ajax 异步登录,并验证用户输入信息(maven) 本篇内容: (1)上一篇是使用同步的请求实现登录,并由 Servlet 决定登陆后下一步做哪些事情,本篇使用 jQuery A ...

  5. net mvc中实现记录用户登录信息(记住登录效果)

    现记录用户登录信息(记住登录效果) 本文讲述了使用cookies实现网站记住登录效果,效果如下: 主要实现方法,当用户选择记住登录时建立cookies保存用户名和用户密码,当用户登录不选择记住登录时, ...

  6. laravel登录后其他页面拿不到登录信息

    登录本来是用表单的,我自作聪明的使用ajax提交 public function login(Request $request){ $data = $request->input(); $dat ...

  7. Axure实例演示—登录界面

         实例演示 ——登录界面                                                                                    ...

  8. SpringBoot开发十一-显示登录信息

    需求介绍-显示登录信息 我们需要在每个页面的头部都要把登录用户的头像显示出来,另外在详细信息里面你需要显示用户的名字,除此之外如果登录了,我们显示首页 信息 头像 三个功能的链接,否则显示首页 登录两 ...

  9. .NET跨平台之旅:ASP.NET Core从传统ASP.NET的Cookie中读取用户登录信息

    在解决了asp.net core中访问memcached缓存的问题后,我们开始大踏步地向.net core进军——将更多站点向asp.net core迁移,在迁移涉及获取用户登录信息的站点时,我们遇到 ...

随机推荐

  1. 一个利用window.name实现的windowStorage

    //key:value|key:value var windowStorage = { _inited: false, _data: {}, init: function(str) { var tmp ...

  2. Chapter 5

    1. 2模块导入 3.包导入

  3. MySql计数器,如网站点击数,如何实现高性能高并发的计数器功能

    MySql计数器,如网站点击数,如何实现高性能高并发的计数器功能 Clicks: Date: -- :: Power By 李轩Lane TagMysql计数器高性能 现在有很多的项目,对计数器的实现 ...

  4. c#实现串口操作 SerialPort

    命名空间:using System.IO.Ports;该类提供了同步 I/O 和事件驱动的 I/O.对管脚和中断状态的访问以及对串行驱动程序属性的访问. 操作类声明: SerialPort sp = ...

  5. test1

    test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1 ...

  6. Visual Studio 项目中添加include, lib, dll库文件(*.h,*.lib,*.dll)

    应用程序使用外部库时需要进行加载,两种库的加载本质上都是一样:提供功能和功能的定义.vs2005 c++ 项目设置外部库方法如下: 1. 添加编译所需要(依赖)的 lib 文件     在“项目-&g ...

  7. ==和equals的区别

    == :是判断两个变量或实例是不是指向同一个内存空间equals :是判断两个变量或实例所指向的内存空间的值是不是相同 结论:欲比较栈中数据是否相等,请用= =:欲比较堆中数据是否相等,请用equal ...

  8. BZOJ 1008: [HNOI2008]越狱 快速幂

    1008: [HNOI2008]越狱 Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生 ...

  9. java如何追加写入txt文件

    java中,对文件进行追加内容操作的三种方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import java.io.BufferedWriter; import  ...

  10. MongoDB (三) MongoDB 安装

    MongoDB安装在Windows上 在 Windows上,首先要安装 MongoDB下载最新发布的MongoDB: http://www.mongodb.org/downloads 确保得到正确的版 ...