JS实现页面传值

1,cookie

cookie的话可以加载Jquery和JQuery.cookie 模块

temp.html页面

<body>
  <button onclick="jump()">jump</button>
  <script src="/js/jquery.min.js"></script>
  <script src="/js/jquery.cookie.min.js"></script>
  <script>
    var userName = 'ReSword';
    var userPassword = '111111';
    var test = "xxx";
    function jump () {
      //   path: '/' 表示在整个域名下都可由使用
      //   cookie的使用必须建立在服务上
      $.cookie('userName', userName, { path: '/' });
      $.cookie('userPassword', userPassword, { path: '/' });
      window.location.href = './test.html';
    }
  </script>
</body>

test.html页面

<body>
  <script src="/js/jquery.min.js"></script>
  <script src="/js/jquery.cookie.min.js"></script>
  <script>
    console.log($.cookie('userName'), $.cookie('userPassword'));
  </script>
</body>

2,参数

temp.html页面代码

<body>
  <button onclick="jump()">jump</button>
  <script>
    var uerName = 'ReSword';
    var userPassword = '111111';
    function jump () {
      window.location.href = `./test.html?userName=${uerName}&userPassword=${userPassword}`;
    }
  </script>
</body>

test.html页面代码

<body>
  <script>
    var url = location.href;
    var results = decodeURI(url.substring(url.indexOf('?')+1,url.length)).match(/\w+/g);
    var obj = {};
    results.forEach((val, index) => {
      if (index % 2) obj[results[index - 1]] = val;
    });
    console.log(obj);
  </script>
</body>

3.localStorage

temp.html页面

<body>
  <button onclick="jump()">jump</button>
  <script>
    var userName = 'ReSword';
    var userPassword = '111111';
    var myStorage = localStorage;
    function jump () {
      myStorage.setItem('userName', userName);
      myStorage.setItem('userPassword', userPassword);
      window.location.href = './test.html';
    }
  </script>
</body>

test.html页面

<body>
  <script>
    var myStorage = localStorage;
    console.log(myStorage.getItem('userName'), myStorage.getItem('userPassword'));
  </script>
</body>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值