当 url 哈希更改时刷新使用 window.open 打开的打开窗口

Jus*_* L. 1 javascript window.location window.open

我使用打开一个小的弹出参考窗口window.open(...)并为其命名。window.open当为该窗口调用后续 s 时,它会被正确地重用。

function openHelp(hash) {
    var helpWindow = window.open(location.protocol + "/help.aspx" + (hash ? "#" + hash : ""), "helpWindow", "width=750, height=600, resizable=1, scrollbars=1, location=0, directories=0, status=no, menubar=no, toolbar=no");
}
Run Code Online (Sandbox Code Playgroud)

它无法正常工作的一种情况是,有人在帮助页面 URL 上打开窗口,并且只有哈希值发生变化(即#jump-to-me)。只有在页面重新加载时,页面才能正确转到哈希。

有没有办法找到打开的窗口,检查 URL 是否与我们尝试打开的窗口匹配,并window.location.refresh()在哈希值更改时有条件地执行 a 操作?

Ger*_*tra 5

如果我做对了,这会让你开始。

var extraWindow;

function makeWindow(){
   extraWindow= window.open(/* .. */);
}

// this will reload the extra window that you just opened.
function reloadWindow(){
  if(extraWindow){
     extraWindow.location.reload();
  }
}

makeWindow();

// reload the window when the hash changes or possibly change the page url based on this.
window.addEventListener("hashchange", reloadWindow, false);
Run Code Online (Sandbox Code Playgroud)

我希望这能提供一个好的答案。