当按下按钮时,如何重新加载单独的页面?

问题描述:

I would like a page to be reloaded when the user press a button on the index.

Here what it looks like...

-index.php

<h1>Press the reload button to reload main.php</h1>
<a class="btn" id="reload">Reload</a>

-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. 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.

我希望当用户按下索引上的按钮时重新加载页面。 p>

这里看起来像...... p>

-index.php p>

 &lt; h1&gt;按下重新加载按钮 重新加载main.php&lt; / h1&gt; 
&lt; a class =“btn”id =“reload”&gt;重新载入&lt; / a&gt; 
  code>  pre> 
 
 

-main。 php p>

 &lt; h1&gt;当在索引上按下该按钮时,该页面应重新加载。&lt; / h1&gt; 
  code>  pre> 
 
  

基本上应该有两个设计在index.html上,另一个在main.html上。 当按下重新加载按钮时,另一个设备应该重新加载。 p>

对于缺少代码,我看起来并且找不到任何内容。 p> div >

If both pages are running in the same browser (and both are not in private/ingonito mode) you can use local storage events.

document.getElementById('reload').addEventListener('click', function (evt) {
   window.localStorage.setItem('window-event', JSON.stringify({reload: "main.php"})); 
});

and in 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.