js携带参数实现页面跳转

js携带参数页面跳转

方法一:跳转路径携带参数

第一个页面:

location.replace("user.html?username=" + username + "&userPsd=" + userPsd);

跳转后的页面

//获取路径携带的参数
var username = (location.search).substring(((location.search).indexOf("=") + 1), (location.search).indexOf("&"));
//传递一个参数时,用下面的获取
 var password = (location.search).substring(((location.search).lastIndexOf("=") + 1), (location.search).length);

缺点:跳转后的路径后面携带参数和值

.../html/user.html?username=aaa&userPwd=111

方法二: sessionStorage传递

sessionStorage属性允许在浏览器中存储 key/value 对的数据,但sessionStorage 用于临时保存同一窗口(或标签页)的数据,在关闭窗口或标签页之后将会删除这些数据。

页面一:

//获取输入框的值
var username = document.getElementById("username").value;
var userPsd = document.getElementById("password").value;
//跳转路径
location.replace("user.html");
//设置sessionStorage:-----------主要代码-------------------------------
window.sessionStorage.setItem('username', username);           window.sessionStorage.setItem('password', userPsd);

可以在浏览器中查看:运行后浏览器右键-检查

页面二:

//在跳转后页面,通过getItem方法来获取
var username = window.sessionStorage.getItem('username');
var password = window.sessionStorage.getItem('password');

具体数据可根据实际要求更改

  • 11
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值