首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

Jquery 登陆页面 Cookie记住用户名密码 (可设定保留时间)

2014-01-05 
Jquery 登陆页面 Cookie记住用户名密码 (可设定保存时间)? ? _expires (typeof hours) string ?

Jquery 登陆页面 Cookie记住用户名密码 (可设定保存时间)

  • ? ? _expires = (typeof hours) == "string" ? "" : ";expires=" + expires.toUTCString();? ?
  • ? ? document.cookie = name + "=" + value + _expires + path;? ?
  • }? ?
  • //获取cookie值? ? 方法
  • function getCookieValue(name){? ?
  • ? ? var name = escape(name);? ?
  • ? ? //读cookie属性,这将返回文档的所有cookie? ?
  • ? ? var allcookies = document.cookie;? ?? ?? ??
  • ? ? //查找名为name的cookie的开始位置? ?
  • ? ? name += "=";? ?
  • ? ? var pos = allcookies.indexOf(name);? ?? ??
  • ? ? //如果找到了具有该名字的cookie,那么提取并使用它的值? ?
  • ? ? if (pos != -1){? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?//如果pos值为-1则说明搜索"version="失败? ?
  • ? ?? ???var start = pos + name.length;? ?? ?? ?? ?? ?? ?//cookie值开始的位置? ?
  • ? ?? ???var end = allcookies.indexOf(";",start);? ?? ???//从cookie值开始的位置起搜索第一个";"的位置,即cookie值结尾的位置? ?
  • ? ?? ???if (end == -1) end = allcookies.length;? ?? ???//如果end值为-1说明cookie列表里只有一个cookie? ?
  • ? ?? ???var value = allcookies.substring(start,end);??//提取cookie的值? ?
  • ? ?? ???return unescape(value);? ?? ?? ?? ?? ?? ?? ?? ?? ?//对它解码? ?? ?? ?
  • ? ?? ???}? ?? ?
  • ? ? else return "";? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?//搜索失败,返回空字符串? ?
  • }? ?
  • 复制代码
    2.还需要一个jquery-1.6.1.min.js ,这个大家应该都有 ,就不传了。


    3.页面
    1. //登录事件
    2. ? ?? ???function doLogin() {
    3. ? ?? ?? ?? ?? ? var userName = document.getElementById("username");
    4. ? ?? ?? ?? ?? ? var pwd = document.getElementById("password");
    5. ? ?? ?? ?? ?? ? if(userName.value.replace(/ /g,"")==""){
    6. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???alert("用户名不能为空!");
    7. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???userName.focus();
    8. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???return false;
    9. ? ?? ?? ?? ?? ?? ?? ?? ?}
    10. ? ?? ?? ?? ?? ? if(pwd.value==""){
    11. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???alert("密码不能为空!");
    12. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???pwd.focus();
    13. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???return false;
    14. ? ?? ?? ?? ?? ?? ?? ?? ?}
    15. ? ?? ?? ?? ?? ? setCookie('cookUser', userName.value, time, '/');//set 获取用户名和密码 传给cookie
    16. ? ?? ?? ?? ?? ? setCookie('cookPass', pwd.value, time, '/');
    17. ? ?? ?? ?? ?? ? document.login_form.submit();
    18. ? ?? ?? ?? ?? ?? ?? ?? ?
    19. ? ?? ?? ?? ?? ? }
    20. ? ?? ?? ?? ?? ? //设置Cookie保存时间
    21. ? ?? ?? ?? ?? ? var time = 0;
    22. ? ?? ?? ?? ?? ??
    23. ? ?? ?? ?? ?? ? $(document).ready(function(){
    24. ? ?? ?? ?? ?? ? //获取Cookie保存的用户名和密码
    25. ? ?? ?? ?? ?? ? var username = getCookieValue("cookUser");
    26. ? ?? ?? ?? ?? ? var password = getCookieValue("cookPass");
    27. ? ?? ?? ?? ?? ??
    28. ? ?? ?? ?? ?? ? if (username !='' && password !='' ) {
    29. ? ?? ?? ?? ?? ?? ?? ?? ?$("#username").val(username);
    30. ? ?? ?? ?? ?? ?? ?? ?? ?$("#password").val(password);
    31. ? ?? ?? ?? ?? ?? ?? ?? ?$("#rememberPW").attr("checked", true);
    32. ? ?? ?? ?? ?? ? }else?
    33. ? ?? ?? ?? ?? ?? ?? ?? ?$("#rememberPW").attr("checked", false);
    34. ? ?? ?? ?? ?? ??
    35. ? ?? ?? ?? ?? ? $("#rememberPW").click(function(){//记住密码
    36. ? ?? ?? ?? ?? ?? ?? ?? ?if($(this).attr("checked") == 'checked'){
    37. ? ?? ?? ?? ?? ?? ?? ?? ?time = 60 * 60 * 60;
    38. ? ?? ?? ?? ?? ?? ?? ?? ?}
    39. ? ?? ?? ?? ?? ?? ?? ?? ?});
    40. ? ?? ?? ?? ?? ? });
    复制代码
    Jquery 登陆页面 Cookie记住用户名密码http://bbs.it-home.org/thread-2179-1-1.html

  • 热点排行