Send a refresh request to another page opened in the browser

五迷三道 提交于 2019-11-29 13:01:53

JavaScript's window.open() returns reference to the instance of opened window. You may use it to set up onunload event handler, like this:

var hWndB = window.open('somepage.php'),
    hWndA = window.self;

hWndB.onunload = function(){ hWndA.location.reload(); }
lexmihaylov

You can use the onunload event and window.opener. For example.

window.addEventListener('unload', function() {
   window.opener.location.reload();
}, false);

Or you could use jquery so you wouldn't have any crossbrowser compatibility problems:

$(window).on('unload', function() {
   window.opener.location.reload();
});

Also there is another event called onbeforeunload that you might want to take a look at.

EDIT

For older browser you can use the hack that @CORRUPT has provided in the comments: window.open() returns undefined or null on 2nd call

Variant "A seeing B closed":

//winb is global variable
winb=window.open(blah blah); //Existing: A opens B

window.setTimeout(checkWinB,1000);

function checkWinB() {
  if (winb.closed()) refreshMySelf();
  else window.setTimeout(checkWinB,1000);
}

Variant "B kicks A":

opener.rerfreshMySelf();
window.close() //Existing: B closes

Both variants need the refreshMySelf() function to refresh the page. How to do this depends on the page mechanism, but a starting point for a non-AJAX page is

function refreshMySelf() {
  location.reload();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!