Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q575 How can I force Internet Explorer to open up and return the output of a cgi to a popup window using the onUnload() event handler?

irt.org | Knowledge Base | JavaScript | Window | Q575 [ previous next ]

Q575 How can I force Internet Explorer to open up and return the output of a cgi to a popup window using the onUnload() event handler?

This question was asked and eventually answered by Pierre Adriaans:

I have an onUnload handler place on a page. When the user leaves the page, I need to pop a new window with the URL of a counter CGI that will, in turn, return a 302 with where to go. This works great in netscape, but in Internet Explorer, 25% of the time the new window opens, but does not actually call the CGI. It simply displays in the location box the call it should be making, but that's it. If I press on "refresh", the call is made, the 302 is sent, and the browser goes where it's got to go, but this "I aint calling" thing is bugging me.

Here's the code I use:
<html>
<head>
<script language="JavaScript"><!--
function unload() {
     window.open('http://www.somewhere.com/program.cgi?name=value','windowName');
}
//--></script>

</head>

<body onUnload="unload()">
...
</body>
</html>
So, basically, a new window will pop up, and display "http://209.118.25.167/cgi-bin/eventcounter.dll?babylon-x=redirect" in the location box, and that's it.

How can I make sure Internet Explorer actually calls this CGI?

Well, I actually found the solution to this one by attacking it from a different angle. I figured that the problem was the "GET" itself. I felt that if I could perform a "POST", the problem would disappear. It did indeed. Here's how I did it:
<html>
<head>
<script language="JavaScript"><!--
function unload() {
    var windowHandle = window.open('blank.htm','windowName');
    windowHandle.document.writeln('<html><body>');
    windowHandle.document.writeln('<form name="the_form" action="http://www.somewhere.com/program.cgi?name=value" method="post"><\/form>');
    windowHandle.document.writeln("<\/body><\/html>");
    windowHandle.document.the_form.submit();
}
//--></script>

</head>

<body onUnload="unload()">
...
</body>
</html>
So, in effect, I open a window and create a document that contains one single form emulating the "GET" in a "POST", then I force the submit. I havent had a failed load since then. The actual code I use has a test that still uses the old "GET" method if you're using Netscape, of course.

Though this might work also:

<html>
<head>
<script language="JavaScript"><!--
function unload() {
    var windowHandle = window.open('blank.htm','windowName');
    windowHandle.document.writeln('<html><body>');
    windowHandle.document.writeln('<script>location.href="http://www.somewhere.com/program.cgi?name=value";</script>');
    windowHandle.document.writeln("<\/body><\/html>");
}
//--></script>

</head>

<body onUnload="unload()">
...
</body>
</html>

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.