Javascript 单击按钮时重新加载另一页

Javascript 单击按钮时重新加载另一页,javascript,php,html,Javascript,Php,Html,我正在处理一个文件myfile.php。在此文件中,我有以下代码: <button type='submit' name='refresh' onclick='refresh()'></button> <script> function refresh(){ setTimeout(window.location.href = "summary.php",1000); } </script> 单击“刷新”按钮时,我

我正在处理一个文件myfile.php。在此文件中,我有以下代码:

<button type='submit' name='refresh' onclick='refresh()'></button>
<script>
    function refresh(){ 
        setTimeout(window.location.href = "summary.php",1000);
    }
</script>

单击“刷新”按钮时,我的窗口位置将更新,浏览器将加载summary.php文件。但是,我想继续使用myfile.php并在第二个选项卡中刷新文件summary.php。

您必须首先使用打开第二个选项卡并保存对它的引用,以便稍后单击按钮时可以访问它以重新加载。这是从JavaScript直接访问/控制单独选项卡的唯一方法。请注意,由于存在问题,您只能对同一域上的页面执行此操作。如果要刷新其他域上的页面,最好在选项卡上设置location.href

下面是一个非常简单的示例,说明了使用您的代码时可能会出现的情况:

<button type="button" name="refresh" onclick="refresh()">Refresh</button>
<script>
    let secondWindow = window.open('summary.php');
    function refresh() { 
        secondWindow.location.reload(true); // Use true to always force reload from the server
    }
</script>
如果您不想依赖JavaScript打开第二个选项卡,您可以将它作为一个替代选项。单击按钮时,您可以从第一页发送一条刷新消息,并在第二页看到其中一条消息时自动刷新该消息。

您可以尝试以下方法:

<script type="text/javascript">
    $(document).ready(function() {
        if(performance.navigation.type == 2){
 window.location.replace("www.google.com");
}

 </script>

使用window.opensummary.php;它是怎么打开的?window.open?使用summary.php的第二个参数已打开。我想在button presswindow上刷新这个文件。打开'summary.php';控制summary.php,但我想保留我的旧文件,即myfile。php@Imran如果你用一个你如何使用这段代码的例子来更新你的问题,我可以试着看看问题是什么。函数refresh{var secondWindow=summary.php';secondWindow.location.reloadtrue;}@Imran您必须在refresh函数之外分配变量。请看我更新的答案。每当我按下刷新按钮时,此代码都会打开summary.php。@如果您使用的代码正确,则不会打开。请用您正在使用的确切代码更新您的原始问题,而不仅仅是其中的一部分。
<script type="text/javascript">
    $(document).ready(function() {
        if(performance.navigation.type == 2){
 window.location.replace("www.google.com");
}

 </script>