Reputation: 2805
The crux of my problem is: How can I tell if a window launched via a form submit was actually launched?
I inherited a messy old web site that uses CGI & Perl on an Apache server. It was written decades ago and the code is really terrible even for Perl so I want to avoid changing too much as we have hundreds of reports used by hundreds of customers.
The initial problem was that when an 'Ok' button was pressed, it sometimes appeared that nothing happened because the process took a long time so a user would keep clicking it. This would spawn many new processes on the Unix box running Apache and cause problems. The way the 'programmer' designed the site, he put a name in the target attribute of the form tag. This USUALLY opens a new window (eventually) with that name.
<form name="PARAMETERS"
method="POST"
action="reportsrv.cgi"
onsubmit="watchChild('_REPORT1556723905', 'reportSubmit3', true);"
target="_REPORT1556723905">
Another programmer tried to fix this by disabling the OK button using java script and then having the newly opened window find its parent and then enable the button. This did not work when, in our hosted environment, sometimes the new window simply did not open. So our customers think something is happening when it is not (because the window did not open and the javascript to enable the button never ran).
I wrote a simple function that uses setTimer to look for the window and see if it is open BUT the only way to find it is to use the window.open() function.
function watchChild(targetName, thebuttonname, disable) {
setTimeout( function()
{
var child = window.open("", targetName); // opens new window if not found!
if (!child) {
child = window.frames[targetName];
}
var child = window.frames[targetName];
if (child) {
alert('child found');
if (disable) {
//disableButton(thebuttonname, disable);
}
watchChild(targetName, thebuttonname, false);
return;
}
// frames are empty
alert('child not found: ' + targetName + '; frame count = ' + window.frames.length);
//disableButton(thebuttonname, false);
}, 3000 );
Now my problem is that the only reliable way to find the window (if it exists) if window.open("", Name) but that opens a blank window if the window does not exist.
The logic of the child window is that nothing is rendered until the process is finished (which may take a long time) so I can't add javascript to it until then.
Can anyone see a way to tell if the window is open without opening another window? I could keep a reference to it if it wasn't opened by a form submit event.
Upvotes: 0
Views: 69
Reputation: 1074949
I suspect you could adapt this technique to your situation.
If not, though, another approach comes to mind:
submit
handler, have JavaScript code open the window proactively in response to the form submission, and disable the form submission.opener
to say when it has loaded.HTMLFormElement#submit
method, it won't trigger the submit
handler; some tools like jQuery will, so beware of that).
opener
to tell it the response has arrived (so opener
re-enables the button). Also put a timeout in the "loading" page that does the same (and shows a timeout error).Upvotes: 1