1

A quick question here regarding forms. I've searched the web and can't seem to figure out why what I've implemented isn't working.

The idea is simple. I have a form inside a JSP page. The form has an 'onsubmit' property defined to open a different jsp with some parameters. Inside the form I have a few buttons, one of which calls a JavaScript function, which in turn submits the form (under some conditions).

Here's the code: JSP:

...
<form id='testForm' onsubmit="window.open('another.jsp')">
  <input type="button" onclick="callJsFunction()" />
  ..
</form>

JavaScript:

function callJsFunction() {
  if (launchNow == 1) {
    var form = document.getElementById("testForm");
    form.submit();
  }
}

If I add target="_blank" to the form definition, a new window does open, but NOT the jsp I want to open. Ultimately, I want the form to perform a servlet action (using the action attribute) and then open the new jsp. Any ideas???

Thanks!

Bob .
  • 521
  • 9
  • 18

3 Answers3

1

The solution to what I was looking for is found here: Javascript Post on Form Submit open a new window

Rather than setting target="_blank", I can set the target to the window I define and open. In my servlet, I redirect to the desired jsp, and it appears in the new pop-up window.

Community
  • 1
  • 1
Bob .
  • 521
  • 9
  • 18
0
<form id='testForm' action='another.jsp' target='_blank'>
HMarioD
  • 842
  • 10
  • 18
  • I don't want to tie up the action parameter because I also need to invoke a servlet to do some processing. I guess a better question is, can I redirect from a servlet to a new window? – Bob . Dec 27 '12 at 20:36
  • 1
    If `action."..."` is used for something else then where's the logic that decides on what action happens when the form is submitted? As far as I can tell, you need to intercept form submission with a submit handler, do whatever is needed, possibly including an ajax call, then `return false` unconditionally to suppress natural form submission. – Beetroot-Beetroot Dec 27 '12 at 20:49
0

I might be wrong but is this what you are looking for?

Please see the working demo at this link: http://fiddle.jshell.net/vf6AC/show/light/ (don't work in the jsfiddle)

<form action="http://google.com" id="testForm">
    <input type="submit" />
</form>​

<script type="text/javascript">
var testForm = document.getElementById("testForm");

testForm.onsubmit = function(e){
    window.open("http://stackoverflow.com");

    return true;
};​
</script>

See the jsfiddle here: http://jsfiddle.net/vf6AC/

TryingToImprove
  • 7,047
  • 4
  • 30
  • 39
  • Thanks, everyone. I found what I was looking for here: http://stackoverflow.com/questions/178964/javascript-post-on-form-submit-open-a-new-window – Bob . Dec 27 '12 at 22:50