最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Open child window and redirect parent window - Stack Overflow

programmeradmin1浏览0评论

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 :(((

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 :(((

Share Improve this question edited May 2, 2015 at 6:28 phpnewbie2015 asked May 1, 2015 at 21:19 phpnewbie2015phpnewbie2015 3762 gold badges5 silver badges17 bronze badges 1
  • I don't know much JavaScript, but shouldn't the setTimeout line e inside the function definition rather than after it? – dg99 Commented May 1, 2015 at 22:07
Add a ment  | 

2 Answers 2

Reset to default 1

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.

My javasrcipt isnt too hot, but does this not work ?

 <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>
发布评论

评论列表(0)

  1. 暂无评论