Javascript return false not working in ie

Hi all

I have the 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 validation fails.

This works in FF, but not in IE8.

IE8 presents the form even after validation is complete.

+1
source share
4 answers

Firstly, one form that can perform several actions is a bad idea that says ...

Do not use submit buttons, not:

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

Now you only need to worry about the form submission behavior (when the user presses enter in the field).

+1
source

Never assume that you can undo the submit button, instead set some javascript variable or hidden field in the form and use onsubmit. Take my word for it. Ask onsubmit to view the variable set by the various submit buttons

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

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

Finally, in IE, when you encounter an error, the default action is to submit the form. Perhaps you may well have other errors elsewhere, and confirmation will return an error that is considered true (0 evaluates to false, most of the rest 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> 
+3
source

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

 event.returnValue = true; return true; event.returnValue = false; return false; 
+1
source

Please note that if there is an error or error in validateUser () or deleteUser (), then "return false" will NOT stop the binding action, and your browser will try to refer to the "href" sub-action. 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 never starts because Javascript is already stopped.
  • the browser is trying to go to the href attribute

If you rely on a third-party JavaScript API (I used the code provided by Recyclebank to pop up a pop-up), and the third-party API does an update that breaks JavaScript, then you will have problems.

The following will stop communication under normal conditions and error conditions.

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

Source: https://habr.com/ru/post/887321/


All Articles