刷新页面时通过URL传递值

时间:2019-04-04 16:37:56

标签: javascript node.js socket.io

当我按下页面的刷新按钮时,我想通过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

0 个答案:

没有答案