Search code examples
javascriptasp.netinternet-explorer-10internet-explorer-11

'return' statement outside of function in IE 10 and IE 11


I have a following asp control

<asp:Button ID="btnSave" onclick="return validate();"
                            runat="server" Text="submit" CssClass="next" />

and below is the js function Validate

function Validate()
{if (some condition){return true} else return false;  } 

this code is working fine in all the browsers other than IE10 and IE11 the function is always returning a value. But when I debugged in IE11 or in 10, I am getting issue " 'return' statement outside of function" after function executes.

What could be the actual cause?

well, I am editing this question as I just came to know that the issue is something else. I was using complete statement 'return Validate()' in Add Watch of IE to see the real value and was getting the error 'return' statement outside of function". When I tried same with only 'Validate()' I got 'true' as its value. Function is returning proper value true or false based on condition, but the actual issue I am getting in IE10 or IE11 is that even after getting true as return value of Validate function my form is not submitted to server. Any Idea ?


Solution

  • In your case no need to have the else part in you code.

    function Validate() {
         if (some condition) {
            return true;
         } 
         return false;  
    } 
    

    Also Terminate the line return true by adding the ;.

    Or

    The shorthand way is

     function Validate() { 
         return ((some condition) ? true : false); 
     } 
    

    Thanks to @pholpar for the suggestion.