views:

68

answers:

2

I got this code in my submit form

<form id="myform" action='hello.php' method='GET'>
    <input type="button" name="newWin" onclick="frmSubmitSameWindows();">
    <input type="button" name="SameWin" onclick="frmSubmitNewWindows();">
<form>

Then use some js functions in the head;

function frmSubmitSameWindows() {
form.target = '';
form.submit();
}


function frmSubmitNewWindows() {
form.target = '_blank';
form.submit();
}

What is the pro and cons when we use javascript event function such as frmSubmitSameWin() and frmSubmitNewWin() in our form ? as far as i concern, this is the best solution when we need a way to submit things. Is there other preference ? the better way then the way i got now ?

+2  A: 

Use onsubmit event on the form element. and use submit inputs instead buttons. this way you can have a single function to determine what you wanna do.

In addition form target = 'blank' is not recommend, as it's deprecated, and it's not supported in HTML 4.01 Strict / XHTML 1.0 Strict DTD.

Mendy
+1  A: 

What are the pros and cons using javascript in our form?

Pro: Offers seas of possibilities to improve user experience (dynamics, faster, no flash of content, etc).
Con: You should code it unobtrusively. Your example is not. It fails when the client has JS disabled.

I've developed lots of websites since ages, but I can't seem to remember a valid business reason to have two submit buttons, one to submit the form in the same window and other to submit it into a new window. I'd suggest to rethink this approach and end up with one submit button. The other flaws in the code are already mentioned by Mendy.

BalusC
maybe this is another WTF question ;D but is there any way to make it not fails when the client has JS disabled ? what i have to code to make it better ?
justjoe
Either just use one button, or use two forms. The last is not really user friendly if you have to duplicate input fields as well.
BalusC