1

I have the following scenario like this :

I have a main window[main.html]. There are three links called Popup1, Popup2, Popup3. On clicking the link open Popup1 window, Popup2 window respectively.

<html>
<head>
  <title>Popup Focus Example</title>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
  <script>
      var openWin = [];
      $(function(){
          $('#elem').on('click', 'a', function(){
              var index = $(this).index();
              var page = 'pop'+(index+1)+'.html';
              var w = window.open(page, $(this).attr('title'), '_blank');
              openWin[index] = w;

          });
      });
  </script>
</head>
<body>
  <div id="elem">
    <a href="javascript:void(0);" title="Pop 1">Pop 1</a>
    <a href="javascript:void(0);" title="Pop 2">Pop 2</a>
    <a href="javascript:void(0);" title="Pop 3">Pop 3</a>
  </div>

</body>
</html>

Now the windows open are window 1 & window 2. I want to transfer the focus of the window 1 from window 2. is there anyway this can be done?

pop1.html

<html>
<head>
  <title>Popup 1 Example</title>
  <script type="text/javascript">
    function go() {
        if(window.opener.openWin) {
            var popup2 = window.opener.openWin[1];
            popup2.focus();
        }
    }
  </script>
</head>
<body>
  <div>
    popup 1 example
  </div>
  <div><a href="javascript:void(0);" onclick="go();">Go to Popup2</a></div>
</body>
</html>

pop2.html

<html>
<head>
  <title>Popup 2 Example</title>
</head>
<body>
  <div>
    popup 2 example
  </div>
</body>
</html>

Check main.html code in fiddle http://jsfiddle.net/tmf8cry8/2/

som
  • 4,650
  • 2
  • 21
  • 36
  • May this link help you http://stackoverflow.com/questions/11407153/open-a-window-behind-the-current-window-using-javascript-jquery – Sadikhasan Jan 19 '15 at 07:23

3 Answers3

0

You have written the code properly, please implement the two changes as listed below...

1) You need to push the window object in openWin Array

var openWin = [];
  $(function(){
      $('#elem').on('click', 'a', function(){
          var index = $(this).index();
          var page = 'pop'+(index+1)+'.html';
          var w = window.open(page, $(this).attr('title'), '_blank');
          //openWin[index] = w;
          openWin.push(w); //Push the window object

      });
  });

2) Access the window object using the array index to focus the window

function go() {
    //console.log(openWin);
    //if(window.opener.openWin) {
      //  var popup2 = window.opener.openWin[1];
        //popup2.focus();
    //}
    //Supposing that pop1, pop2 and pop3 are opened, you want to access pop2
    openWin[1].focus();

}

Hope it helps!

Hemant
  • 1,999
  • 2
  • 12
  • 8
0

You can use the method window.open

window.open("",window_name).focus();

Assuming your window is opened, you can transfer focus to the window using the above code.

Please use the following code in pop2.html and try [after opening all windows in a sequential order].

<html>
<head>
<title>Popup 2 Example</title>
<script language="javascript">
function openNew()
{
    window.open("","Pop 3").focus();
}
</script>
</head>
<body>
<div>
popup 2 example
</div>
<a href="javascript:void(0);" onclick="javascript:openNew();">
 Go to pop3</a>
</body>
</html>

As per you code, you are using title attribute to naming windows, hence Pop 3 is used to transfer focus to Pop 3 window.

Alternatively, you may check if the window instance is opened or not and then load the URL entirely or transfer focus

Hope it helps!!!

Hemant
  • 1,999
  • 2
  • 12
  • 8
0

Make go() function just call a function in the opener window, e.g.:

window.blur(); //focus out of here
window.opener.myParentWindowFunction( 2 );

That function (in the "parent" window) should call then a function in the target window that just "window.focus()".

funtion myParentWindowFunction( target ){
 window.blur();//focus out of here also
 openWin[target].callFocus();
}

And on every popup:

function callFocus(){ window.focus(); }

You can't make a window "send" the focus to another (security issues I guess), but you can make a window to "ask for it"... and depends on the browsers if it's going to get it (in chrome doesn't work, but if you add an alert() after the focus it will change the window. Funny, because if you just put the alert but not the focus it doesn't swap the windows...)

Code is just a mockup, but that's the concept. You have to use the opener window as relay comunicate to the desired window that it have to call the focus. Hope you don't need it really crossbrowser.

miguel-svq
  • 2,136
  • 9
  • 11