首页 > 解决方案 > 刷新页面时通过 URL 传递值

问题描述

当我按下页面的刷新按钮时,我想通过 URL 传递一个值。

当我访问我的项目的第二个 HTML 页面时,我通过 URL 传递了一个值,并且我从第二个页面获取没有问题。问题是当我刷新第二页时,我无法通过 URL 发送该值并再次访问它。

//this is the code from my first page
function setup() {

   var tst = createButton('test');
   tst.position(400, 400);
   tst.mousePressed(function () {
       var val1 = 'dadadad';
       var queryString = '?para1=' + val1;
       window.location.href = 'page2.html' + queryString;
   })
   createCanvas(300, 300);
}

//this is the code from my second page to get the value from URL
function setup() {

  var  key = decodeURIComponent(window.location.search);
   var index = key.indexOf('=');
   key = key.substring(index + 1);
   console.log(key);
   createCanvas(200, 200);

}
//this is the code to pass the value through URL when the page is refreshed
window.onbeforeunload = function (e) {
   var e = e || window.event;
   if (e) {
       var val1 = 'dadadad';
       var queryString = '?para1=' + val1;
       window.location.href = 'page2.html' + queryString;


   }

   // // For Safari
   // return window.location.href = window.location.href.replace('page2.html?para1=ddadadad');;
};

我想在刷新页面时通过 URL 传递一个值,并在刷新完成后从该 URL 访问该值。enter code here

标签: javascriptnode.jssocket.io

解决方案


推荐阅读