开发者

Javascript return false not working in ie

HI All

I have following lines in my JSP.

<s:submit name="submit"  onclick开发者_运维问答="return validateUser();" action="saveUser"  theme="simple" value="Save" />

The java script method validateUser(), validates the user and returns true or false. The form should not be submitted when the validation fails.

This is working in the FF but not in the IE8.

IE8 submits the form even after validation fails.


Never assume you can cancel the submit button, instead set some javascript variable or hidden field on the form and use onsubmit. Take my word for it. Have the onsubmit look at the variable set by the different submit buttons

Never use javascript: (javascript colon) unless you are in IE and have a VBScript as the first script on the page. In all other cases javascript is default.

Never use such atrocities as <a href="javascript:something()" instead of <a href="#" onclick="return something()

Lastly, in IE, when you have an error occurring, the default action is to submit the form. You may very well have other errors completely elsewhere and have the validate return the error, which is seen as true (0 evaluates to false, most anything else is true)

<script type="text/javascript">
var isvalidateNeeded = true;
function validate(theForm) {
  if (!isvalidateNeeded) return true; // allow submission
. // normal validation
.
.
  return true; // allow submission
}
</script>
<form onsubmit="return validate(this)">
.
.
.
<input type="submit" name="subaction" value="Test" onclick="isvalidateNeeded=false" />
<input type="submit" name="subaction" value="Check" onclick="isvalidateNeeded=false" />
<input type="submit" name="subaction" value="Submit" onclick="isvalidateNeeded=true" />
</form>


First, a single form that can perform multiple actions is a bad idea, that said...

Do not use submit buttons, instead:

<button type="button" onclick="javascript:validateUser();">Save user</button>
<button type="button" onclick="javascript:deleteUser();">Delete user</button>

Now you only have to worry about default submit behaviour of a form (when user presses enter in a field).


Only thing you need to change is in your validateUser() function. IE is looking for the return value on the event, so you need to specify this:

event.returnValue = true;
return true;

event.returnValue = false;
return false;


Please note that if there is a bug or error in validateUser() or deleteUser() then "return false" will NOT stop the anchor action and your browser will try to link to the href "subaction". Here is the logic:

  1. User clicks on anchor
  2. onClick fires validateUser()
  3. There is an error in validateUser() so Javascript crashes and stops all code execution.
  4. return false is never fired because Javascript has already stopped.
  5. browser tries to go to href attribute

If you are relying on a third party JavaScript API (I was using code supplied by Recyclebank to spawn a popup), and the third party API makes an update that breaks the JavaScript, then you'll have problems.

The following will stop the link under normal conditions and error conditions.

<a class="button" href="javascript:;" onclick="validateUser();return false;">Validate User</a>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜