js 记住 用户名和密码实现也是看的一篇文章。纯js当然我们可以混着jquery使用

html

<!--网站登录跳转 -->
<form name="jumpFrm" id="jumpFrm" action="" method="POST" target="_blank">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="69" height="26" align="right">用户名:</td>
<td><input type="text" name="userName" id="userName" class="text_input2" style="width:126px; height:15px;"/></td>
</tr>
<tr>
<td height="26" align="right">密码:</td>
<input type="hidden" name="domainAccount" value="whir">
<td><input type="password" name="userPassword" id="userPassword" class="text_input2" style="width:50px; height:15px;"/>

记住密码:
<input type="checkbox" name="rmbPassword" id="rmbPassword"/>
</td>
</tr>
<tr>
<td height="26" align="right">选择系统:</td>
<td>

<select name="jmpLocation" style="width:126px; height:20px;">
<option value="1" selected="selected">综合办公平台</option>
<option value="2">内网门户后台</option>
<option value="3">邮件系统</option>
</select>

</td>
</tr>
<tr>
<td height="28" align="right" valign="bottom></td>
<td valign="bottom">
&nbsp;
<input type="button" name="Submit22" value="登 录" class="btn_2" onclick="jmpLogin();"/>
&nbsp;
<input type="reset" name="Submit2" value="重 置" class="btn_2"/>
</td>
</tr>
</table>

<input type="hidden" name="Password" id="password" value=""/>
<input type="hidden" name="LoginName" id="LoginName" />

</form>

js

<!--网站登录跳转 -->
<mce:script type="text/javascript"><!--
function jmpLogin(){
//跳转
document.jumpFrm.action="";
var userName=document.getElementById("userName").value;
var userPassword=document.getElementById("userPassword").value;
if(userName==""){
alert("用户名不能为空,请填写!");
return;
}else if(userPassword==""){
alert("密码不能为空,请输入!");
return;
}

var jmpLocation=document.getElementById("jmpLocation").value;
//document.all.memberid.checked==true
if(document.all.rmbPassword.checked){
//alert("begin to rmb password!!!");
setCookie("userName",userName,24,"/");
setCookie("userPassword",userPassword,24,"/");
//alert("OK!COOKIE");
}

if(jmpLocation==1){
document.jumpFrm.action="http://10.41.7.41:7001/defaultroot/LogonAction.do";
jumpFrm.submit();
}
if(jmpLocation==2){
document.jumpFrm.action="http://10.41.7.40:8123/defaultroot/LogonAction.do";
jumpFrm.submit();
}
if(jmpLocation==3){
// http://mail.hnsl.gov.cn/remote.php?LoginName=PMAIL_USER&Password=PMAIL_PASS
document.getElementById("LoginName").value=document.getElementById("userName").value;
document.getElementById("password").value=document.getElementById("userPassword").value;
document.jumpFrm.action="http://mail.hnsl.gov.cn/remote.php";
jumpFrm.submit();

}
}

//获取cookie信息
function getRememberInfo(){
// alert("---获取cookie信息---");

try{
var userName="";
var userPassword="";
userName=getCookieValue("userName");
userPassword=getCookieValue("userPassword");
document.getElementById("userName").value=userName;
document.getElementById("userPassword").value=userPassword;
}catch(err){
alert("NO RMB PASSWORD!");
}
}

//新建cookie。
//hours为空字符串时,cookie的生存期至浏览器会话结束。hours为数字0时,建立的是一个失效的cookie,这个cookie会覆盖已经建立过的同名、同path的cookie(如果这个cookie存在)。
function setCookie(name,value,hours,path){
var name = escape(name);
var value = escape(value);
var expires = new Date();
expires.setTime(expires.getTime() + hours*3600000);
path = path == "" ? "" : ";path=" + path;
_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 ""; //搜索失败,返回空字符串
}
//删除cookie
function deleteCookie(name,path){
var name = escape(name);
var expires = new Date(0);
path = path == "" ? "" : ";path=" + path;
document.cookie = name + "="+ ";expires=" + expires.toUTCString() + path;
}

// --></mce:script>

看完应该基本了解了cookie的使用,其实就是document.cookie进行存入,删除时候其实就是赋值为“”,我们可以结合上面的方法自己写一个js文件每次导入引用就好

我贴上我写的js,用到哪写到哪就好,如下:

/**
*Cookie plugin
*Copyrighte (c) 2015 Dylan Zhang
*Created on 24th April 2015
*/

var Dcookie = {
    setCookie: function (name, value, hours, path) {
        var name = escape(name);
        var value = escape(value);
        var expires = new Date();
        expires.setTime(expires.getTime() + hours * 3600000);
        path = path == "" ? "" : ";path=" + path;
        _expires = (typeof hours) == "string" ? "" : ";expires=" + expires.toUTCString();
        document.cookie = name + "=" + value + _expires + path;
    },
    getCookie: function (name) {
        var name = escape(name);
        var allCookies = document.cookie;
        name += "=";
        var pos = allCookies.indexOf(name);
        if (pos != -1) {
            var start = pos + name.length;
            var end = allCookies.indexOf(";", start);
            if (end == -1) end = allCookies.length;
            var value = allCookies.substring(start, end);
            return unescape(value);
        }
        else return "";
    },
    delCookie: function (name, path) {
        var name = escape(name);
        var expires = new Date(0);
        path = path == "" ? "" : ";path=" + path;
        document.cookie = name + "=" + ";expires=" + expires.toUTCString() + path;
    }
}
时间: 2024-08-29 07:53:02

js 记住 用户名和密码实现也是看的一篇文章。纯js当然我们可以混着jquery使用的相关文章

通过jquery.cookie.js实现记住用户名、密码登录功能

<!doctype html>   <html xmlns="http://www.w3.org/1999/xhtml">   <head>   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   <title>无标题文档</title>   <script src="

Android:SharedPreferences 记住用户名和密码

参考:http://blog.csdn.net/liuyiming_/article/details/7704923 SharedPreferences介绍: SharedPreferences是Android平台上一个轻量级的存储类,主要是保存一些常用的配置参数,它是采用xml文件存放数据的,文件存放在"/data/data<package name>/shared_prefs"目录下. SharedPreferences的用法: 由于SharedPreferences是

ios中DEBUG中记住用户名和密码

- (void)viewDidLoad { [super viewDidLoad]; #ifdef DEBUG // 设置测试使用的用户名和密码 self.nameText.text = @“xiaoshuai"; self.pwdText.text = @"123"; [self textChanged]; #endif } ios中DEBUG中记住用户名和密码,布布扣,bubuko.com

【转】ASP.NET Cookies简单应用 记住用户名和密码

不要试图给Password类型的TextBox赋值! 在asp.net中,不要试图给Password类型的TextBox控件赋值! 无论是在设计或是运行时,都不可以的.猜测的原因是,password类型的TextBox控件从根本上,没有Text属性的Set方法,只有Get !!同样,html中的Input控件,如果设置为ruanat="server",password类型的Input控件也是一样.无论是在设计或是运行时,都不容许设置它的值. 解决办法:txtPassword.Attri

防止浏览器记住用户名及密码

如何设置能禁止浏览器自动保存表单信息,比如用户名,密码? 现在很多浏览器都有自动填写功能,我在input上使用了autocomplete="off",但在有的浏览器上还是被记住了用户名跟密码,请问有没有更有效及简便的方法来防止浏览器记住用户名及密码? 1.针对浏览器记住密码 1).首先大部分浏览器都是根据表单域的type="password"来判断密码域的,所以针对这种情况可以采取“动态设置密码域”的方法: <input type="text&quo

Tortoisegit 记住用户名和密码

Tortoisegit 记住用户名和密码方法: [Windows系统] 当你配置好git后,在 C:\Documents and Settings\Administrator\ 目录下有一个  .gitconfig  的文件,里面会有你先前配好的name 和email,只需在下面加一行 [credential] helper = store 下次再输入用户名 和密码 时,git就会记住,从而在 C:\Documents and Settings\Administrator\  目录下形成一个  

Android 实现记住用户名和密码的功能

Android 实现记住用户名和密码的功能 结果演示: 源代码下载地址: https://github.com/GXS1225/Android-----.git 分析 (1)判断是否输入了账号和密码 if(name.trim().equals("")){ Toast.makeText(this, "请您输入用户名!", Toast.LENGTH_SHORT).show(); return; } if(pswd.trim().equals(""))

OpenVPN 如何记住用户名和密码

最近在使用OpenVPN,但是没有记住用户名和密码功能,太坑人,研究一下发现是可以的. 1. 在OpenVPN安装目录下\OpenVPN\config文件夹中找到vpnserver.ovpn文件. 2. 在文件最后一行加入auth-user-pass pass.txt保存. 3. 在同目录下创建pass.txt文件. 4. 文件中录入用户名和密码,用户名和密码独占一行.

jquery.cookie.js实现cookie记住用户名和密码

记得导入 <script src="jquery.js" type="text/javascript"></script> <script src="jquery.cookie.js" type="text/javascript"></script> 先看表单里面的内容,有一个checkbox复选框 <form class="form-signin"&g