Open child window and redirect parent window

phpnewbie2015 picture phpnewbie2015 · May 1, 2015 · Viewed 8.8k times · Source

I have a JavaScript that opens a new window when a button is clicked. It works fine. But, I want to re-direct the Parent Window (i.e. the current page) once the new window is opened.

Here is my script :

<script type="text/javascript">
function OpenWindow() {
    window.open('/My_Folder/file.php',
                'newwindow',
                config='height=670,width=1400,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,directories=no,status=no');
}
</script>

And, the button:

<input type="button" value="Submit" onclick="OpenWindow()">

So, in order to be able to re-direct the Parent Window, after the new window has popped up, I added a simple line to my JS function :

<script type="text/javascript">
function OpenWindow() {
    window.open('/My_Folder/new_file.php', 'newwindow', 
                config='height=670,width=1400,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,directories=no, status=no');
}

setTimeout(1000, '/My_Folder/PHP/file_2.php');
</script>

Meaning, after the new window has popped up, the Parent Window should re-direct in 1 second.

But, for some reason, it's not working.

Online searches have only given me solutions for situations involving the reverse : re-direct the Parent Window after CLOSING the child-window.

I want to re-direct the Parent Window after opening a new (child) window.

UPDATE

So far, I've tried these 3 ways :

   (a)  <script type="text/javascript">
   function OpenWindow()    {
   window.open ('/My_Folder/new_file.php', 'newwindow', 
   config='height=670, width=1400,   
   toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, 
   directories=no, status=no');
   window.top.location.href = '/My_Folder/PHP/file_2.php'
   }
   </script>


 (b)  <script type="text/javascript">
 function OpenWindow() { window.open('/My_Folder/file.php','newwindow',         
 config='height=670,width=1400,toolbar=no,menubar=no,scrollbars=no,
 resizable=no, location=no,directories=no,status=no');
 settimeout( function(){document.location='/My_Folder/PHP/file_2.php'}, 1000    
 );
 }
 </script>


 (c) <script>
     function OpenWindow()    {
    window.open ('/My_Folder/new_file.php', 'newwindow', 'height=670, 
    width=1400, toolbar=no, menubar=no, scrollbars=no, resizable=no, 
     location=no, directories=no, status=no');

        window.location = '/My_Folder/PHP/file_2.php';
     }
     </script>

Of all these, none are working.

But (b) is the best solution so far, because, at least, it opens the New (Child) Window, as it should. But, it still does not re-direct the Parent Window :(((

Answer

serverSentinel picture serverSentinel · May 2, 2015

Try this:

<script type="text/javascript">
function OpenWindow() { window.open('/My_Folder/file.php','newwindow',
             config='height=670,width=1400,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,directories=no,status=no');
settimeout( function(){document.location.href='/My_Folder/PHP/file_2.php'}, 1000 );
}
</script>

Moves the settimeout into the function and uses it properly, and tells the window to change to a new location.