Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Some basic JavaScript event problems (preventing default action)

Some basic JavaScript event problems (preventing default action)
Thread Tools
Synotic
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Jan 10, 2007, 08:58 PM
 
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!
     
mania
Mac Enthusiast
Join Date: Aug 2001
Location: Durango CO
Status: Offline
Reply With Quote
Jan 13, 2007, 10:46 PM
 
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.
The Bitcastle
graphic design, web development, hosting
     
Chris O'Brien
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status: Offline
Reply With Quote
Jan 15, 2007, 02:48 PM
 
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
     
skalie
Mac Elite
Join Date: Mar 2002
Location: Clogland
Status: Offline
Reply With Quote
Jan 16, 2007, 03:49 AM
 
Just as an aside, always validate before trying to debug that AJAX stuff.
     
sohailamir
Fresh-Faced Recruit
Join Date: Dec 2006
Status: Offline
Reply With Quote
Jan 16, 2007, 10:17 AM
 
Is the login.php page linked properly?
Enormous Image Removed by Management
     
exca1ibur
Mac Elite
Join Date: Oct 2000
Location: Oakland, CA
Status: Offline
Reply With Quote
Jan 16, 2007, 12:21 PM
 
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!
     
Synotic  (op)
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Jan 17, 2007, 01:19 PM
 
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
     
skalie
Mac Elite
Join Date: Mar 2002
Location: Clogland
Status: Offline
Reply With Quote
Jan 17, 2007, 02:11 PM
 
Originally Posted by Synotic View Post
skalie, what's a good solution for JavaScript validation?
The W3C Markup Validation Service
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 06:17 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,