|
|
Some basic JavaScript event problems (preventing default action)
|
|
|
|
Mac Elite
Join Date: Oct 2000
Status:
Offline
|
|
I'm working on a small personal project and am having trouble getting the browser to prevent the default action. In general this is my problem:
There is a password field and a submit button. When the user hits return in the text field or hits the submit button, the site communicates with the server via AJAX and either confirms or denies the user. It's not the most secure method, but the password system isn't too important.
I have this line in my JavaScript:
document.getElementById('loginform').onsubmit=logi n;
Which executes this function:
function login() {
makeRequest('login.php','name='+hovered+'&pass='+( getElementById('passfield').value),true);
return false;
}
The problem is that immediately after it makes the request, the page refreshes, ignoring my "return false". If I put something more innocuous, instead of makeRequest, like alert('Hello'), then the alert is shown and the return false takes effect: the page is never refreshed.
What is different about makeRequest that causes the browser to ignore my "return false"? I've tried different things, like changing the form element in my HTML to a div, and then changing <input type="submit"> to <input type="button">. But then I'm not sure how to make it so that when the user hits return in the text field, it activates the input button. I'm just learning JavaScript events right now and can't figure out how to read which key the user pressed (although I've looked).
Am I missing something fundamental? Is there another approach? If it's helpful, I can put together a mini example.
Thanks!
|
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Aug 2001
Location: Durango CO
Status:
Offline
|
|
did you try putting the return false first? or try putting the onsubmit and return false in the form tag or the submit tag? just ideas.
|
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status:
Offline
|
|
Sounds like there's an error in your makeRequest function. Is it supposed to be getElementById in the argument rather than document.getElementById?
|
Just who are Britain? What do they? Who is them? And why?
Formerly Black Book
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Mar 2002
Location: Clogland
Status:
Offline
|
|
Just as an aside, always validate before trying to debug that AJAX stuff.
|
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Dec 2006
Status:
Offline
|
|
Is the login.php page linked properly?
|
Enormous Image Removed by Management
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Oct 2000
Location: Oakland, CA
Status:
Offline
|
|
What is this on, a button or a div? (document.getElementById('loginform').onsubmit=log i n
The submit button can probably just be.. <input type="button" name="submit" onsubmit="login();" />
PS. sohailamir, that is a MAJOR signature. wow!
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Oct 2000
Status:
Offline
|
|
Thanks for the help everyone. Mania, you encouraged me to get back on the project and mess around with a few things; in doing that, I realized that I was calling "getElementById" instead of "document.getElementById" (thanks Chris). And yes, the login page was properly linked, sohailamir.
skalie, what's a good solution for JavaScript validation?
exca1ibur, the onsubmit function applies to the form, since the form can be submitted any number of ways (hitting return in a text field, or hitting the submit button). I could apply the event inline, but that would just be calling the event another way, and would also be more to add to my HTML.
It was just a typo this time... Cut me some slack, I'm still learning
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Mar 2002
Location: Clogland
Status:
Offline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|