Javascript 如何使用javascript刷新另一个页面而不在新选项卡中打开同一页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27061451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:39:57  来源:igfitidea点击:

How to refresh another page using javascript without opening the same page in a new tab

javascriptjqueryreloadpage-refresh

提问by Nayana_Das

Is it possible to refresh a page from another page using Javascript or JQuery without opening the same page in a new tab.

是否可以使用 Javascript 或 JQuery 从另一个页面刷新页面,而无需在新选项卡中打开同一页面。

JS:

JS:

 var newtab = window.open('http://localhost:8081/app/home');
 newtab.document.location.reload(true);

I tried the above, but here, it will open a new tab, with the same page, which is already opened in the browser.

我尝试了上述操作,但在这里,它将打开一个新选项卡,该选项卡与浏览器中已打开的页面相同。

Please suggest a method.

请提出一种方法。

采纳答案by Nayana_Das

I got the idea from a previous Question, here they used window Object Reference to reload the popup window, but for me it wont work, because, the parent window and child window runs in 2 different ports. So using the same trick, what i did is :

我从上一个问题中得到了这个想法,在这里他们使用窗口对象引用来重新加载弹出窗口,但对我来说它不起作用,因为父窗口和子窗口在 2 个不同的端口中运行。所以使用同样的技巧,我所做的是:

HTML:

HTML:

<a onclick="openNewTab()">app2</a>

<a onclick="refreshExistingTab()">Refresh</a>

JS:

JS:

<script>

    var childWindow = "";
    var newTabUrl="http://localhost:8081/app/home";

    function openNewTab(){
        childWindow = window.open(newTabUrl);
    }

    function refreshExistingTab(){
        childWindow.location.href=newTabUrl;
    }

</script>

refreshExistingTab() this instend of refreshExistingTab

refreshExistingTab() 这就是 refreshExistingTab

回答by sagie

take a look at https://developer.mozilla.org/en-US/docs/Web/API/Window.openbasically if you do window.open and specify a window name it will overwrite that window with the url you provided. so if you open the page each time with same window name, it should overwrite it each time you do it again from that other page.

看看https://developer.mozilla.org/en-US/docs/Web/API/Window.open基本上如果你做 window.open 并指定一个窗口名称,它将用你提供的 url 覆盖该窗口。因此,如果您每次都使用相同的窗口名称打开该页面,则每次从该其他页面再次打开该页面时,它都应该覆盖它。