Subject: login script: user can't hit "return" for login
Posted By: dmerrill Post Date: 12/23/2005 2:41:51 PM
Hi, I am very new to js and am using a simple login script I found at www.laughland.biz. I needed this script to do 2 things it did't:

setfocus on the username field

allow the usesr to hit return after entering their pw and be logged in.

I got the first one, but can't for the life of me get the second.
Here's the code block I'm assuming this function would be in:

function BuildPanel() {
document.write('<form name="logon"><table align="center" border="0"><tr><td align="center">');
document.write('<small><font face="comic sans ms">Username:</font></small></td>');
document.write('<td><small><font face="comic sans ms"><input type="text" name="username" size="20"></font></small></td></tr>');
document.write('<tr><td align="right"><small><font face="comic sans ms">Password:</font></small></td>');
document.write('<td><small><font face="comic sans ms"><input type="password" name="password" size="20"></font></small></td></tr>');
if(imgSubmit == ""){
 document.write('<tr><td align="center" colspan="2"><p><input type="button" name="Logon" value="Logon" onClick="login(username.value,password.value)" onKeyPress="login(username.value,password.value)">');
} else {
 document.write('<tr><td align="center" colspan="2"><p><input type="image" src="'+imgSubmit+'" name="Logon" onclick="login(username.value,password.value)">');
}
if(imgReset == ""){
 document.write('<input type="reset" value="Reset" name="Reset">');
} else {
 document.write('<input type="image" src="'+imgReset+'" name="Reset" onclick="logon.reset();">');
}
document.write('</p></td></tr></table></form>');
}

function setfocus() {
    document.logon.username.focus();
}

K. Dawn Merrill
Reply By: adarsh83 Reply Date: 12/24/2005 1:49:11 PM
Hi Dawn,

Try doing this in your javascript:

if(imgSubmit == ""){
 document.write('<tr><td align="center" colspan="2"><p><input type="submit" name="Logon" value="Logon" onClick="login(username.value,password.value)" onKeyPress="login(username.value,password.value)">');
}

Note the type of the input. A submit button will automatically get fired when you
hit the enter button.

________
Adarsh
Reply By: dmerrill Reply Date: 12/29/2005 10:46:09 AM
HI Adarsh, and thanks for the information. I tried this & while it DOES allow the user to simply hit enter, it just returns to the same page & resets it to blank fields instead of going on to the next page. What am I doing wrong here? The only value I changed was the input type="submit"

Thanks, Dawn

K. Dawn Merrill
Reply By: panacea Reply Date: 1/16/2006 4:21:47 AM
Dawn,

Now that it's a submit button, when the user presses enter the browser goes to the "action" URL defined in your <form> element.  To prevent this from happening (and to just run the login() javascript), change the following code

onClick="login(username.value,password.value)"

to this:

onClick="login(username.value,password.value); return false"

This should abort the default submit action and let login() set the window location to the URL you're expecting.

Let me know how it works!

Jon Emerson
Computer Scientist
Adobe Systems, Inc.
http://www.jonemerson.net/
Reply By: dmerrill Reply Date: 1/16/2006 4:55:33 PM
Jon, that's perfect! Thanks so much. Now I have one more question: I lose my setfocus if the user hits reset. Any ideas?

Thanks again, Dawn

K. Dawn Merrill
Reply By: panacea Reply Date: 1/16/2006 6:07:22 PM
What form element do you want focused after reset is clicked?

Jon Emerson
Adobe Systems, Inc.
http://www.jonemerson.net/
Reply By: dmerrill Reply Date: 1/17/2006 9:40:48 AM
I would like it to go back to "username" after reset is clicked. Thanks for your help!

Dawn

K. Dawn Merrill
Reply By: panacea Reply Date: 1/17/2006 11:15:25 AM
Hi Dawn,

I would do something like this:

<html>
<body>
<form action="test.html" method="get" name="logon">
<input type="text" name="username">
<input type="submit">
<input type="image" src="http://media.wiley.com/product_data/coverImage80/28/07645590/0764559028.jpg" 
  onclick="document.logon.reset(); document.logon.username.focus(); return false">
</form>
</body>
</html>


You might want to move the onclick() code into its own function -- but this shows the basic idea.

Jon Emerson
Adobe Systems, Inc.
http://www.jonemerson.net/
Reply By: dmerrill Reply Date: 1/17/2006 5:27:06 PM
Thanks Jon, I'll give that a try. Today is Friday for me, but I'll probably try it from home over my "weekend". I'll let you know how it works!

Thanks Again, Dawn

K. Dawn Merrill
Reply By: rkn Reply Date: 7/12/2006 4:28:21 AM
Hi
I'm using the exact same login script, but I am new to this and I can't get make the setfocus work.

Besides the
function setfocus() {
    document.logon.username.focus();
}

do I need to add anything to the html, etc.?




Reply By: panacea Reply Date: 7/12/2006 4:44:01 AM
Rkn,

You need to run setfocus() when the page loads. For example, <body onLoad="setfocus()">.

Jon Emerson
http://www.jonemerson.net/
Reply By: rkn Reply Date: 7/12/2006 7:09:24 AM
Nice & simple,
thank you Jon.

Rasmus

Reply By: gomezsean Reply Date: 7/13/2006 5:05:28 PM
quote:
Originally posted by dmerrill

I would like it to go back to "username" after reset is clicked. Thanks for your help!

Dawn

K. Dawn Merrill



Hello,

I am using the exact same script and all the questions I had were answered by you guys. I was trying to figure out how to set focus on the username box, how to use the Enter key to logon, and how to re-set focus to the username box after the Reset button was clicked. I figured out the last one by trial and error. This is what I did and it works:

Here what the code looked like before:

if(imgReset == ""){
 document.write('<input type="reset" value="Reset" name="Reset">');
} else {
 document.write('<input type="image" src="'+imgReset+'" name="Reset" onclick="logon.reset();">');

Here's what did and it works so far:

if(imgReset == ""){
 document.write('<input type="reset" value="Reset" name="Reset" onclick="document.logon.username.focus();">');
} else {
 document.write('<input type="image" src="'+imgReset+'" name="Reset" onclick="logon.reset();">');

Basically I just added the setFocus() function code to the end of that first part. I'm a bigtime rookie so I don't know if what I did is considered normal, but it seems to work and since you guys helped me out I wanted to at least attempt to help you out. Thanks again. I really appreciate it.


Reply By: panacea Reply Date: 7/14/2006 7:25:06 PM
The code onclick="logon.reset();" looks perfect, and it's the same thing I would do.  If you wanted more complicated logic for clearing the form, I would put the focus code into a function -- but since you're just doing a one-liner, putting the code directly into the event is the best design.

Jon Emerson
http://www.jonemerson.net/

Go to topic 47046

Return to index page 231
Return to index page 230
Return to index page 229
Return to index page 228
Return to index page 227
Return to index page 226
Return to index page 225
Return to index page 224
Return to index page 223
Return to index page 222