html 记住用户名,js使用cookie实现记住用户名功能示例

本文实例讲述了js使用cookie实现记住用户名功能。分享给大家供大家参考,具体如下:

www.jb51.net cookie记住用户名

//1、cookie的使用:document.cookie = 'key=value';可以同时设置多个

// document.cookie="username=longzhoufeng"

// document.cookie="age=03"

//2、cookie的过期时间:document.cookie = '名称=值;expires=' + 字符串格式的时间;

// var myDate=new Date()

// myDate.setDate(myDate.getDate()+3)

// document.cookie="mynameis="+encodeURI("longzhoufeng")+ ";expires="+myDate.toGMTString();

// document.cookie="age=30"

// console.log(decodeURI(document.cookie))

// 解码后输出结果如下:

//mynameis=longzhoufeng; age=30

//3、把上面的封装成一个函数,其中有三个参数是在变化的,key值,value值,T时间

function setCookie(key,value,t){

var myDate=new Date()

myDate.setDate(myDate.getDate()+t)

document.cookie=key+"="+value+ ";expires="+myDate.toGMTString();

}

function getCookie(key){

var arr1 = document.cookie.split('; ');

for (var i=0; i

var arr2 = arr1[i].split('=');

if ( arr2[0] == key ) {

return decodeURI(arr2[1]);

}

}

}

function removeCookie(key) {

setCookie(key, '', -1);

}

// setCookie("myName","longzhoufeng",1)

// console.log(getCookie("myName"))

// console.log(removeCookie("myName"))

// alert(typeof myDate)

// 必须将时间格式转换成字符形式

// alert(typeof myDate.toGMTString());

//4、内容最好编码存放,encodeURI

//alert( encodeURI('你好') );

//alert( decodeURI('%E4%BD%A0%E5%A5%BD') )

window.onload = function() {

var oUsername = document.getElementById('username');

var oLogin = document.getElementById('login');

var oDel = document.getElementById('del');

if ( getCookie('username') ) {

oUsername.value = getCookie('username');

}

oLogin.onclick = function() {

alert('登陆成功');

setCookie('username', oUsername.value, 5);

}

oDel.onclick = function() {

removeCookie('username');

oUsername.value = '';

}

}

希望本文所述对大家JavaScript程序设计有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Vue.js 应用程序实现记住密码功能需要使用一些技术,如 Cookie、localStorage 或 Vuex。这里提供了一个使用 Cookie 实现记住密码功能示例代码: 1. 在登录页的表单添加一个“记住密码”的勾选框: ```html <template> <div> <input type="text" v-model="username" placeholder="用户名"> <input type="password" v-model="password" placeholder="密码"> <label><input type="checkbox" v-model="remember">记住密码</label> <button @click="login">登录</button> </div> </template> ``` 2. 在登录方法判断是否勾选了“记住密码”,如果勾选了,将用户名和密码保存在 Cookie : ```js <script> import Cookies from 'js-cookie' export default { data() { return { username: '', password: '', remember: false } }, methods: { login() { // 发送登录请求,验证用户名和密码 // ... if (this.remember) { Cookies.set('username', this.username) Cookies.set('password', this.password) } } } } </script> ``` 3. 在应用程序的入口处读取 Cookie,如果存在 Cookie,则自动填充表单: ```js <script> import Cookies from 'js-cookie' export default { created() { const username = Cookies.get('username') const password = Cookies.get('password') if (username && password) { this.username = username this.password = password this.remember = true // 自动登录 this.login() } } } </script> ``` 在这个示例,我们使用js-cookie 库来读写 Cookie。如果您使用的是 localStorage 或 Vuex,可以使用相应的 API 来读写数据。并且需要注意,如何保护用户的敏感信息,比如使用加密技术来保护用户的密码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值