Hooking a synchronous event handler on a form submit button in JS

Posted by Xzhsh on Stack Overflow See other posts from Stack Overflow or by Xzhsh
Published on 2010-04-19T03:38:42Z Indexed on 2010/04/19 3:43 UTC
Read the original article Hit count: 226

Hi, I'm working on a security project in javascript (something I honestly have not used), and I'm having some trouble with EventListeners.

My code looks something like this:

function prevclick(evt) {
evt.preventDefault();
document.loginform.submitbtn.removeEventListener('click',prevclick,false);
var req = new XMLHttpRequest();
req.open("GET","testlog.php?submission=complete",false);
req.send();
document.loginform.submitbtn.click(); //tried this and loginform.submit()
}

document.loginform.submitbtn.addEventListener('click',prevclick,false);

But the problem is, the submit button doesn't submit the form on the first click (it does, however, send the http request on the first click), and on the second click of the submit button, it works as normal.

I think there is a problem with the synchronization, but I do need to have the request processed before forwarding the user to the next page.

Any ideas on this would be great. Thanks in advance.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about event-handling