Javascript 如何使用javascript将焦点返回到父窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6910278/
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-23 23:43:27  来源:igfitidea点击:

How to return focus to the parent window using javascript?

javascript

提问by user874329

This doesn't work as to return the focus to the parent window in Firefox 4 and 5

这不能在 Firefox 4 和 5 中将焦点返回到父窗口

<html>
<head>
<script type="text/javascript">
  function openWin()
  {
     myWindow=window.open('','','width=200,height=100');
     myWindow.document.write("<p>The new window.</p>");
     myWindow.blur();
     window.focus();
  }
</script>
</head>
<body>

  <input type="button" value="Open window" onclick="openWin()" />

</body>
</html> 

How can I return focus to the parent window using javascript?

如何使用javascript将焦点返回到父窗口?

回答by Juan Posadas

You need to give your parent window a name.

您需要为您的父窗口命名。

Parent.html

父.html

<a id="link" href="#">Click to child.html </a>
<script type="text/javascript">
    $(document).ready(function () {
        window.name = "parent";
        $('#link').click(function (event){ 
            event.preventDefault();
            window.open("child.html");
        });
    });
</script>

Child.html

儿童.html

<a id="link" href="#">Return to Parent </a>
<script type="text/javascript">
    $(document).ready(function () {
        $('#link').click(function(event) {
            event.preventDefault();
            var goBack = window.open('', 'parent');
            goBack.focus();
        });
    });
</script>

Now, whenever you click the link in child.html, the parent window will be focused.

现在,无论何时单击 child.html 中的链接,父窗口都将被聚焦。

回答by Arthur van Dijk

Sorry for necromancing a very old question, but I ran into the same problem and couldn't find any solution. I eventually solved this by using the following script:

很抱歉对一个非常古老的问题进行了死机处理,但我遇到了同样的问题并且找不到任何解决方案。我最终通过使用以下脚本解决了这个问题:

e = window;
while (e.frameElement !== null) {e = e.parent;}
e.parent.focus();

Apparently, calling just window.focus();isn't enough. You have to call the Window's parent's focus method by using window.parent.focus();You can't access any other properties from JS from it though, it will give a cross-source error. This script will also work if your script was fired from a frame within a page, assuming the frame and main page share the same source (domain).

显然,仅仅打电话window.focus();是不够的。您必须使用 Window 的父级的 focus 方法调用window.parent.focus();您无法从 JS 访问任何其他属性,但它会产生跨源错误。如果您的脚本是从页面内的框架触发的,则此脚本也将起作用,假设框架和主页共享相同的源(域)。

回答by Adam Hopkinson

I don't think you can return focus, not without closing the child window:

我不认为你可以返回焦点,而不是关闭子窗口:

myWindow.close()

回答by Jason Foust

I tried to use the above example and it didn't work for me. I had to use a recursive loop to find the opener until the window didn't have an opener (bad design, I know, but our app has several levels of child windows). Here is the code example. I hope it helps:

我尝试使用上面的示例,但它对我不起作用。我不得不使用递归循环来查找打开器,直到窗口没有打开器(我知道设计不好,但我们的应用程序有多个级别的子窗口)。这是代码示例。我希望它有帮助:

/* This is for the button functionality.  
The button element first comes in.  
This element is then used to get the document that has the element.  
The document window opener is then passed to the focusMain method */
function setFocusOnMain(el){
  var doc = el.ownerDocument;
  var win = doc.defaultView || doc.parentWindow;
  focusMain(win.opener.document);
}

/* This is a recursive method that checks for a parent window of the current document.  
If the document has no window opener, focus on this element because this is the Main.  
If the document has a window opener, pass the window opener's document to focusMain. */
function focusMain(doc) {
  var win = doc.defaultView || doc.parentWindow;
  if (win.opener == null) {
    doc.focus();
  } else {
    focusMain(win.opener.document);
  }
}

回答by Elias

Here is how I solved this. It is in TypeScript but you can easy compile it JS:

这是我解决这个问题的方法。它在 TypeScript 中,但您可以轻松编译它 JS:

const ParentWindowName = 'ParentWindowName';

// here set the parent window name so that you can switch back to it later and open the new window/tab
export const openChildWindow = (id: string) => {
  (window as any).name = ParentWindowName;
  (window as any).ChildWindowName = window.open(`/child`, id);
};

// here you can change the focus back to the new window.
export const focusParentWindow = () => {
  (window as any).open('', ParentWindowName).focus();
};

回答by Brian

If this is a Window you opened yourself you can use opener.focus();

如果这是您自己打开的窗口,则可以使用opener.focus();