Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I redirect a url that I am pointed to after clicking submit?

There is a html form I have which submits to a Servlet once Submit button is clicked. After the submission the Servlet redirects to a fixed url which is

http://siteforServlets.com/Servlet/Servlet

That site then displays an xml report shown below:

Sorry I cannot post Images not enough rep so I uploaded direct link here: http://img836.imageshack.us/img836/3343/sitea.png

I dont want my users getting confused seeing an xml I prefer it going blank page or redirecting to a different url...

How can I redirect the url to hide this xml report url?

Mark, I tried what you said but it redirects before the form even loads. Here is a quick jsfiddle I made to demonstrate your suggestion: jsfiddle.net/ujsEG/12/

like image 645
Bulvak Avatar asked Dec 09 '25 14:12

Bulvak


1 Answers

You could try making the form target a hidden iframe and then set the onload attribute of the iframe with a javascript call to window.location.href.

<script type="text/javascript">
var okToRedirect = false;
function redirect() {
    if (okToRedirect) {
        window.location.href = 'path/to/new/location';
    }
}
</script>

<form action="...some action..." target="MyIframe" onsubmit="okToRedirect = true;">
...
</form>

<iframe name="MyIframe" style="display: none;" onload="parent.redirect()"></iframe>

When the form posts, the results will display inside the hidden iframe. Once the iframe finishes loading, the onload attribute will fire the javascript to redirect the user.

Update

I updated my answer because of your comment. I added a javascript method which the iframe can call. There is now a global variable which is set to false initially. This will prevent the first load of the page from redirecting. When the form posts, the onsubmit event on the form will set the global variable okToRedirect to true, the iframe will reload and will call the redirect() method. This time around, the redirect will occur.

like image 104
Mark Bouchard Avatar answered Dec 12 '25 04:12

Mark Bouchard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!