18

This doesn't work as to return the focus to the parent window in Firefox 4 and 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?

Aleksi Yrttiaho
  • 8,266
  • 29
  • 36
user874329
  • 181
  • 1
  • 1
  • 3

6 Answers6

19

You need to give your parent window a name.

Parent.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

<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.

rtorres
  • 2,601
  • 3
  • 27
  • 33
Juan Posadas
  • 199
  • 1
  • 3
  • 4
    Just keeping you honest. That's not JavaScript, that's jQuery. The asker may not be using it. – Itumac Oct 11 '13 at 03:21
  • 3
    The parent window name is stored in `window.opener.name`, so the goBack variable could be rewritten to use it (i.e., `var goBack = window.open('', window.opener.name)`). Here's more info on the [window.opener](http://www.w3schools.com/jsref/prop_win_opener.asp) property. – rtorres Apr 24 '16 at 23:46
  • @Claudix Your comment sounds like this doesn't work ever since, but it is actually working (Chrome 51). – Parziphal Jun 13 '16 at 13:23
  • @renocor Yep, I probably misused the preposition... :P "on" is probably better :P – Claudi Jun 13 '16 at 14:17
  • Works in chrome 72 even without the focus method. – Josué Zatarain Feb 13 '19 at 22:16
  • Nope, that doesn't work if the user focuses on a 3rd tab and comes back to the 2nd one. – benweet May 24 '19 at 16:44
6

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).

Arthur van Dijk
  • 121
  • 2
  • 2
2

Here is how I solved this.

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) => {
  window.name = ParentWindowName;
  window.ChildWindowName = window.open('/child', id);
};

// here you can change the focus back to the new window.
export const focusParentWindow = () => {
  window.open('', ParentWindowName).focus();
};
mix3d
  • 4,122
  • 2
  • 25
  • 47
Elias
  • 79
  • 1
  • 4
2

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

myWindow.close()
Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99
  • I know `window.opener` - it is the equivalent of `window` in the OP (as it is run in the parent window). But I'd thought JS sandboxing wouldn't allow a focus, as it is effectively allowing the JS to control tab/window switching on the client machine. – Adam Hopkinson Aug 02 '11 at 10:19
  • I might be lucking out :) I have no issues with window.opener (up to IE8/FFX3.5) from windows that I explitely open (may be a different store with link href=_blank/_top). – Brian Aug 02 '11 at 10:28
1

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);
  }
}
Gonzalo
  • 1,781
  • 15
  • 29
-7

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

Brian
  • 2,772
  • 15
  • 12