简体   繁体   English

按下按钮后如何重新加载页面?

[英]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.

相关问题 按下后退按钮时如何使Firefox重新加载页面? - How do I make Firefox reload page when back button is pressed? 为什么按下按钮时页面会重新加载 - Why does the page reload when the button is pressed 在HTML上按下按钮后,如何重新创建重新加载问题 - How can I recreate the reload issue when a button is pressed on HTML 如何制作一个油脂猴子脚本,当按下某个键时,该脚本单击页面上的一个按钮? - How to make a greasemonkey script that clicks a button on page when a key is pressed? 按下后退按钮时不要重新加载页面 - AngularJS - Don't reload page when back button is pressed - AngularJS 在 Angular 中按下后退按钮时硬重新加载页面 - Hard reload a page when back button is pressed in Angular 按下按钮时如何模拟按下的不同按钮? - How can I simulate a different button pressed when a button is pressed? 按下按钮时如何使JavaScript工作? - How to make JavaScript work when button is pressed? 按下按钮时如何不刷新页面 - How to not refresh a page when a button is pressed 如何在按下第二个按钮时隐藏按钮,以及当我点击页面的空白区域时如何使按钮显示? - How to hide button when second is pressed and how to make it appear when i click on empty area of page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM