javascript - 刷新页面时通过 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
解决方案
推荐阅读
- java - 使用 th:object 保存所有对象
- arduino - ESP8266-01 如何使用电阻将 GPIO 接地,如下图所示
- angular - Angular 获得 30 多个结果
- javascript - 我如何在 swagger/nodejs 中检查不记名令牌
- node.js - 如何以编程方式运行和停止运行 Artillery.io?
- excel - 如何使用集合为用户表单中的下拉列表提供选项 - Excel VBA
- swift - 我们可以在 swift 包管理器中使用协调器模式吗?
- java - 2021-02-22T00:00:00 的日期模式格式
- python - TypeError: index_queryset() 得到了一个意外的关键字参数“使用”
- python - 在 AWS Batch 作业中发送参数的编程方式