1

I have this simple javascript function which calls other 2 functions:

<script type="text/javascript" language="javascript">

    function mainfunction() {
        function1()
        function2()
    }

    function function1() {
        if (//some test) {
            alert('test function1');
            return false;
        }
    }

   function function2() {
        if (//some test) {
            alert('test function2');
            return false;
        }
    }

</script>

i call mainfunction() like this:

<form id="form1" runat="server" onSubmit="return mainfunction()">

or like this:

<asp:Button ID="btntest" runat="server" Text="test button" OnClientClick="return mainfunction()" />

btntest is a button that just calls some class which redirects to another page.


The problem

  • if i directly call any of the 2 functions function1() or function2() then the button will not get executed, which is what i want. but...:
    When i call mainfunction() then alert in each function works but return false; doesn't seem to work because the button unfortunately gets executed.

why is that? how can i call the 2 functions and let their return false; work?

3 Answers 3

1

Your mainfunction is not returning anything. Try this:

function mainfunction() {
    return function1() && function2();
}

As mentioned in the comments, to have your mainfunction working properly, you have to alter your other functions to always return either true or false:

function function1() {
    if (//some test) {
        alert('test function1');
        return false;
    }
    return true;
}

EDIT A funny hack to have both functions being executed and still return true only when both functions are true:

function mainfunction() {
    return (function1() + function2()) === 2;
}
Sign up to request clarification or add additional context in comments.

12 Comments

function1 returns undefined or false, not true or false. undefined is not truthy, so function2 will never be called.
@icktoofay: Okay, I should have said that you have to extend your functions by returning true. I will fix that. Thanks for the hint.
Thank you Amberlamps and icktoofay. I tried the solution: i changed mainfunction and the other 2 functions as mentioned but now unfortunately only function1 is called.
thank you guys, now function2 is called regardless if function1 fails or not. but this made me lost a bit, I don't really understand the purpose of the hack.
also i notice that it won't work unless i call mainfunction like this: return mainfunction()
|
0

mainfunction doesn't return anything, you aren't using the return values from either function1 or function2

To prevent a form from submitting, you need the handler itself to return false, for example:

function mainfunction() {
    function1();
    function2();

    return false; // prevent form submission
}

Returning undefined (what you're doing right now in the example in the question) is not sufficient.

1 Comment

Thank you AD7six, but the result is not always false. However thank you as I see what you mean, mainfunction didn't return anything.
0

Try this:

<asp:Button ID="btntest" runat="server" Text="test button" OnClientClick="mainfunction(); return false;" />

This will works as well:

<asp:Button ID="btntest" runat="server" Text="test button" OnClientClick="return mainfunction();" />

but you will need to change mainfunction:

function mainfunction() {
    function1();
    function2();
    return false;
}

1 Comment

Thank you for your answer, it made me realize that i must also use return when calling mainfunction(). however mainfunction() does not always return false.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.