0

I try to implement the following code, this page will display when survey is completely submited and direct to this thankyou page, but I want this window close once it is being redirected.

<head>
 <script>
    function load()
    {
      window.close();
    }           
 </script>   

   <!--[if IE]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
  </head>
  <body onload="load()">
<p id="thankyou">Thank you!</p>
   </body>

when I use google chrome to open it, it won't close, can anyone help me with it, I want it close automatically once the page is load, thanks in advance!

aefxx
  • 24,835
  • 6
  • 45
  • 55
smith
  • 5,341
  • 8
  • 31
  • 38
  • 3
    You want the page to close as soon as it opens? – Jared Farrish Dec 03 '11 at 13:36
  • 1
    Why don't you show a simple **light box**, instead of showing an entire window or tab? – Saeed Neamati Dec 03 '11 at 13:39
  • @JaredFarrish yes, even better if can wait for 5 seconds and then colse, thanks – smith Dec 03 '11 at 13:44
  • You can change your approach as Saeed said, or can this help you http://stackoverflow.com/questions/2032640/problem-with-window-close-and-chrome ? – Irvin Dominin Dec 03 '11 at 13:46
  • Light box means you open a simple **overlaid** `div` element. Have you seen sites where on your entrance they ask something by darkening the entire screen, and showing you a simple box at the center? That is called light box. – Saeed Neamati Dec 03 '11 at 13:58
  • @saeed, thanks, because I am using header(Location:http://www.example.com) to redirect in my php file and I think it wonld be better if i know how to open a new window or light box with that header function, is there a way to do that? – smith Dec 03 '11 at 14:03

4 Answers4

1

Apply as below code for onUnload event

function closeWindows() {
    document.forms[0].unloadFlag.value='true';
    document.forms[0].logoff.value='user';
    document.forms[0].submit();

}
1

From MDN on window.close:

This method is only allowed to be called for windows that were opened by a script using the window.open method. If the window was not opened by a script, the following error appears in the JavaScript Console: Scripts may not close windows that were not opened by script.

AshleysBrain
  • 22,335
  • 15
  • 88
  • 124
0

Try this code -

function closeWindow() { 
window.open('', '_self', '');
window.close();
}
Rohit Suthar
  • 3,528
  • 1
  • 42
  • 48
0

You can create a function that delegates opening and closing like this: http://jsfiddle.net/yyE5z/1/.

var w = window.open("<popup>");

function doClose() {
    w.close();
}

Then use setTimeout to close after 5 seconds:

setTimeout(function() {
    opener.doClose();
}, 5000);
pimvdb
  • 151,816
  • 78
  • 307
  • 352