Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return focus to the parent window using javascript?

Tags:

javascript

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?

like image 943
user874329 Avatar asked Aug 02 '11 09:08

user874329


People also ask

How do I pass a value from child window to parent window?

From a child window or a small window once opened, we can transfer any user entered value to main or parent window by using JavaScript. You can see the demo of this here. Here the parent window is known as opener. So the value we enter in a child window we can pass to main by using opener.

How do you find the parent element in a child window?

You just need to prefix “ window. opener. ” and write the same code that you will write in the parent window's HTML page to access its elements.

What is window focus?

focus() Makes a request to bring the window to the front. It may fail due to user settings and the window isn't guaranteed to be frontmost before this method returns.

What is window opener?

opener. The Window interface's opener property returns a reference to the window that opened the window, either with open() , or by navigating a link with a target attribute. In other words, if window A opens window B , B. opener returns A .


1 Answers

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.

like image 58
Juan Posadas Avatar answered Sep 19 '22 16:09

Juan Posadas