[英]How can you make a separate page reload when a button is pressed?
I would like a page to be reloaded when the user press a button on the index. 我希望当用户按下索引上的按钮时重新加载页面。
Here what it looks like... 这里看起来像...
-index.php -index.php
<h1>Press the reload button to reload main.php</h1>
<a class="btn" id="reload">Reload</a>
-main.php -main.php
<h1>When the button is pressed on index this page should reload.</h1>
Basically there should be two devises one on index.html and the other on main.html. 基本上应该有两个设计,一个在index.html上,另一个在main.html上。 When the reload button is pressed the other device should reload.
按下重新加载按钮后,另一台设备应重新加载。
Sorry for the lack of code, I looked and couldn't find anything on this. 很抱歉缺少代码,我看了一下却找不到任何东西。
If both pages are running in the same browser (and both are not in private/ingonito mode) you can use local storage events. 如果两个页面都在同一浏览器中运行(并且两个页面都不处于私有/ ingonito模式),则可以使用本地存储事件。
document.getElementById('reload').addEventListener('click', function (evt) {
window.localStorage.setItem('window-event', JSON.stringify({reload: "main.php"}));
});
and in main.php: 并在main.php中:
window.addEventListener('storage', function (evt) {
if (evt.key === 'window-event') {
var msg = JSON.parse(evt.newValue);
if (msg.reload === 'main.php') {
window.location.reload();
}
}
});
Edit: web workers can also exchange messages between windows, but web workers are more complex to set up and maintain and may fail when dozens of windows are open. 编辑:Web Workers也可以在窗口之间交换消息,但是Web Workers的设置和维护更为复杂,并且在打开多个窗口时可能会失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.