views:

192

answers:

4

Hi

I have an HTML page with a simple form.

When the user clicks "submit", I'd like a new window to open with the processed results of the form, AND to have the original page redirect somewhere else.

If I use a link with target="_blank", I can open the results window but not redirect the original page. If I use Javascript to try and open the new window and then redirect the current page, the opening of the new window gets blocked (at least by my Firefox's default popup blocker).

Is there any way to get both a new window and run some Javascript in the original page?

Thanks!

A: 

u can have function on parent page like

function gosomewhere()
{
location.href="somewhere.html";
}

and on the popup window use this function on onload event of body call this function

function parentRedirect()
{
window.opener.gosomewhere();
}
nik
do you mean the submit button's direct function will only be to open the new window?if so, how would the form be submitted? I will need the called function to also submit the form, wouldn't I?
Gj
no need to insert function for the form. Just place gosomewhere function on the page where form is. U would call this function from the pop-up page. just place function parentRedirect in the popup page and call it inside body tag with onLoad event. GOT IT
nik
i'm not sure you get it: how would the form in the original page get submitted according to you suggestion?
Gj
since the opener of the page ie parent can be accessed by window.opener from the popup ie child page.. I suppose You will find out by trying it
nik
A: 

Firstly, you'll need to set the "target" of the form you're submitting to "_blank", then use the onsubmit event of the form to change the current location as follows:

<form action="formResults.html" method="post" onsubmit="document.location.href='redirectPage.html'" name="MyForm" target="_blank" id="MyForm">
  <input type="submit" name="button" id="button" value="Submit" />
  <input type="text" name="textfield" id="textfield" />
</form>
Jimbo
this looks looks like the solution, except that since I have a simple data validation function in the onsubmit, this construct will not run the validation before opening the new window.any idea how I can do some basic JS validation on the field before the new window pops up?
Gj
The ONLY way to do this (validation, popup result and current location change) for ALL browsers (IE is easy, Firefox causes problems) is:In the body's onload event of the form action window i.e. formResults.html, call window.opener.document.location.href = 'redirectPage.html'That will cause the page from which the form was initially submitted to change location to 'redirectPage.html'
Jimbo
A: 

I had a similar problem with a PDF which was generated on the fly from a form. The form was first validated and then opened the pdf on the fly in a new window. as this PDf could be opened within the window it would not fire javascript code to the parent.

My solution was to place an setTimeout('gosomewhere()', 100); just before the 'return true;' in the validation script.

The timeout of 100 milliseconds should be sufficient for the browser to open the new window.

 
{
setTimeout('gosomewhere()', 100);
return true;
}
 

Hope this will be useful for all who encounter such problems.

j-p Gommers
A: 

You should be able to use something along the lines of

<script type="text/javascript">
function onSubmit() {
     if(valid()) {
         setTimeout(function() { document.location.href = 'blah' }, 100);
         return true;
     }
     return false
}
</script>

<form onsubmit="return onSubmit();" target="_blank">
...
Toby