Javascript Return False Not Working In Ie
Solution 1:
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)
<scripttype="text/javascript">var isvalidateNeeded = true;
functionvalidate(theForm) {
if (!isvalidateNeeded) returntrue; // allow submission
. // normal validation
.
.
returntrue; // allow submission
}
</script><formonsubmit="return validate(this)">
.
.
.
<inputtype="submit"name="subaction"value="Test"onclick="isvalidateNeeded=false" /><inputtype="submit"name="subaction"value="Check"onclick="isvalidateNeeded=false" /><inputtype="submit"name="subaction"value="Submit"onclick="isvalidateNeeded=true" /></form>
Solution 2:
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).
Solution 3:
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;
returntrue;
event.returnValue = false;
returnfalse;
Solution 4:
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:
- User clicks on anchor
- onClick fires validateUser()
- There is an error in validateUser() so Javascript crashes and stops all code execution.
- return false is never fired because Javascript has already stopped.
- 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.
<aclass="button"href="javascript:;"onclick="validateUser();return false;">Validate User</a>
Post a Comment for "Javascript Return False Not Working In Ie"