0

I am having a form in which a button is there on click of that button i am calling a javascript function in which i am displaying like 3.. 2.. 1 kind of effect then i am returning true.

But after showing 3 only return true is getting executed and the form is getting submitted.

How to stop form submitting before running the script.

Edit:

function jsFun() 
{ 
    timerCount(3); 
    //Calling the Timer Function. 
} 

var t; 

function timerCount(cDown) 
{ 
    if (cDown == 0) 
    { 
        clearTimeout(t); 
        return true; 
    } 

    $('#<%= mainContainer.ClientID %>').html(cDown); 
    cDown = cDown - 1; 
    t = setTimeout('timerCount(' + cDown + ')', 1000); 
} 

<asp:Button ID="btnStarts" runat="server" Text="Start" OnClientClick="return jsFun();" OnClick="btn_click" />
4
  • 1
    Can you post your code please? Commented Aug 17, 2009 at 7:41
  • Are you using an asp.net button control? Commented Aug 17, 2009 at 7:50
  • function jsFun() { timerCount(3); //Calling the Timer Function. } var t; function timerCount(cDown) { if (cDown == 0) { clearTimeout(t); return true; } $('#<%= mainContainer.ClientID %>').html(cDown); cDown = cDown - 1; t = setTimeout('timerCount(' + cDown + ')', 1000); } <asp:Button ID="btnStarts" runat="server" Text="Start" OnClientClick="return jsFun();" OnClick="btn_click" /> Commented Aug 17, 2009 at 7:54
  • Post these codes as an edit to your question. Commented Aug 17, 2009 at 7:56

2 Answers 2

1

The form will be submitted if the 'submit' button returns true.

So then the answer is return false, and then do your 'countdown' (because it's async, probably), and manually submit the form manually via 'form.submit();'

Sign up to request clarification or add additional context in comments.

3 Comments

actually i want to call the event handler for the button click after the execution of javascript function. How to do that ?
Just call it? Post your code and maybe we can help futher. But when you hit '1', just call the submit().
As far as i know the cleanest way to do this is with a web service , i.e. when the javascript is finished executing call the web service which will execute what ever .net code you need
0

I've modified the code slightly not using ASP.NET like so:

function jsFun() {
    timerCount(3);
}

var t;

function timerCount(cDown) {
    if (cDown == 0) {
        clearTimeout(t);
        return true;
    }

    document.getElementById('testbutton').value = cDown;

    cDown = cDown - 1;
    t = setTimeout('timerCount(' + cDown + ')', 1000);
}

Calling it like this:

<input id="testbutton" name="testbutton" type="button" value="Test me!" onclick="return jsFun();" />

Which works exactly as advertised! I would say there's a problem with this line:

$('#<%= mainContainer.ClientID %>').html(cDown);

Hope it helps.

3 Comments

then how to submit the form and run the event handler for the button control.
I would say document.myForm.submit(); right after clearTimeout(t);?
stackoverflow.com/questions/1286770/…, this link will show you how to call a server side event or method using JS.

Your Answer

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