By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,188 Members | 834 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,188 IT Pros & Developers. It's quick & easy.

Login page cursor not pointing to login box, how can I do that?

100+
P: 365
This is the Java script I am using fo rlogin page, but cursor not pointing to login box, any idea how can I point cursor to login box when this page loaded?. here admin login take to control page and user login take to status page, if I give direct path without goto login, both pages display, can I add a check if user didn't login, then redirect to login page for both control.htm and status.htm, let me know. thanks in advance.



Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2. <!--//
  3. /*This Script allows people to enter by using a form that asks for a
  4. UserID and Password*/
  5. function pasuser(form) {
  6. if (form.id.value=="admin") { 
  7. if (form.pass.value=="admin") {              
  8. location="control.htm" 
  9. } else {
  10. alert("Invalid Password")
  11. }
  12. } else if (form.id.value=="user") {
  13. if (form.pass.value=="user") {
  14. location="status.htm" 
  15. } else {
  16. alert("Invalid Password")
  17. }
  18.  
  19. else {  alert("Invalid UserID")
  20. }
  21. }
  22. //-->
  23. </script>
  24.  
  25. <html>
  26.  
  27. <head>
  28. <title>LOGIN PAGE</title>
  29. </head>
  30.  
  31. <body>
  32.  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  
  33.  <br>
  34. <div id="hedeer">
  35. </div> &nbsp;  &nbsp;  &nbsp; 
  36.  
  37. <center>
  38. <table bgcolor="white" cellpadding="12" border="1">
  39. <tr><td colspan="2"><center><h1><i><b>Login
  40. Area</b></i></h1></center></td></tr>
  41. <tr><td><h1><i><b>UserID:</b></i></h1></td><td><form name="login"><input
  42. name="id" type="text"></td></tr>
  43. <tr><td><h1><i><b>Password:</b></i></h1></td><td><input name="pass"
  44. type="password"></td></tr>
  45. <tr><td><center><input type="button" value="Login"
  46. onClick="pasuser(this.form)"></center></td><td><center><br><input
  47. type="Reset"></form></td></tr></table></center> 
  48.  
  49. &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; 
  50. <br>
  51. <br>
  52. <br>
  53. <tr> <CENTER>
  54.         <td align="center" width="100%" colspan="2">
  55.  
  56.             <img border="0" src="footer.gif" width="800" height="25"></td> </CENTER>
  57.     </tr>
  58.  
  59. </body>
  60. </html>
  61.  
Oct 21 '09 #1

✓ answered by gits

give your input node an id and then call a function in the body's onload that will focus the node:

Expand|Select|Wrap|Line Numbers
  1. function setFocus( yourInputNodeId ) {
  2.     var n = document.getElementById( yourInputNodeId );
  3.     n.focus();
  4. }
  5.  
and the call should look like:

Expand|Select|Wrap|Line Numbers
  1. <body onload="setFocus( 'theNodeId_youHaveAdded' );">
kind regards

Share this Question
Share on Google+
21 Replies


gits
Expert Mod 5K+
P: 5,320
give your input node an id and then call a function in the body's onload that will focus the node:

Expand|Select|Wrap|Line Numbers
  1. function setFocus( yourInputNodeId ) {
  2.     var n = document.getElementById( yourInputNodeId );
  3.     n.focus();
  4. }
  5.  
and the call should look like:

Expand|Select|Wrap|Line Numbers
  1. <body onload="setFocus( 'theNodeId_youHaveAdded' );">
kind regards
Oct 22 '09 #2

100+
P: 365
Thanks you verymuch, appreciated.
Oct 22 '09 #3

100+
P: 365
I added the function part of javascript, then I added function call in body, but still I cannot see cursor.
Oct 22 '09 #4

gits
Expert Mod 5K+
P: 5,320
did you add the id too? ... show the code you are using ... so that we might see what the problem could be.

kind regards
Oct 22 '09 #5

100+
P: 365
This is the code, I added function in javascript, then I am calling that function form body. Here my id is "UserID", which is the user id box, I need the curosr to be there.



Expand|Select|Wrap|Line Numbers
  1. <script language="javascript"> 
  2. <!--// 
  3. /*This Script allows people to enter by using a form that asks for a 
  4. UserID and Password*/ 
  5. function pasuser(form) { 
  6. if (form.id.value=="admin") {  
  7. if (form.pass.value=="admin") {               
  8. location="control.htm"  
  9. } else { 
  10. alert("Invalid Password") 
  11. } else if (form.id.value=="user") { 
  12. if (form.pass.value=="user") { 
  13. location="status.htm"  
  14. } else { 
  15. alert("Invalid Password") 
  16. }  
  17. else {  alert("Invalid UserID") 
  18. //--> 
  19.  
  20. function setFocus(NodeId) {
  21.  
  22.         var n = document.getElementById(NodeId);
  23.         n.focus();
  24. }
  25. </script> 
  26.  
  27. <html> 
  28.  
  29. <head> 
  30. <title>LOGIN PAGE</title> 
  31. </head> 
  32.  
  33.  <body onload="setFocus('UserID');">
  34.  
  35. <body> 
  36.  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;   
  37.  <br> 
  38. <div id="header"> 
  39. </div> &nbsp;  &nbsp;  &nbsp;  
  40.  
  41. <center> 
  42. <table bgcolor="white" cellpadding="12" border="1"> 
  43. <tr><td colspan="2"><center><h1><i><b>Login 
  44. Area</b></i></h1></center></td></tr> 
  45. <tr><td><h1><i><b>UserID:</b></i></h1></td><td><form name="login"><input 
  46. name="id" type="text"></td></tr> 
  47. <tr><td><h1><i><b>Password:</b></i></h1></td><td><input name="pass" 
  48. type="password"></td></tr> 
  49. <tr><td><center><input type="button" value="Login" 
  50. onClick="pasuser(this.form)"></center></td><td><center><br><input 
  51. type="Reset"></form></td></tr></table></center>  
  52.  
  53. &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  
  54. <br> 
  55. <br> 
  56. <br> 
  57. <tr> <CENTER> 
  58.         <td align="center" width="100%" colspan="2"> 
  59.  
  60.             <img border="0" src="footer.gif" width="800" height="25"></td> </CENTER> 
  61.     </tr> 
  62.  
  63. </body> 
  64. </html>
Oct 22 '09 #6

Dormilich
Expert Mod 5K+
P: 8,639
did you really use that? it should be giving an error (<script> tags are supposed to be in the <head>/<body> not outside <html>)
Oct 22 '09 #7

100+
P: 365
Yes, I loaded here, no error, the script gets called when user do "onclick" for verification. you can see line 54 (onClick="pasuser(this.form)").
Oct 22 '09 #8

gits
Expert Mod 5K+
P: 5,320
as far as i could see ... this:

Expand|Select|Wrap|Line Numbers
  1. <input name="id" type="text">
  2.  
should be that:

Expand|Select|Wrap|Line Numbers
  1. <input name="whatever" id="UserID" type="text"/>
kind regards
Oct 22 '09 #9

100+
P: 365
Yahooooo. It works now. cursor goes to login box by default with the above change.

But when I view "page source", I can see complete javascript, also it shows both passwords for admin and user, mainly I want admin knows both, where as user shouldnot know admin password, is there anyway I shouldnot show passwords with "view" source of this login page?.
Oct 22 '09 #10

Dormilich
Expert Mod 5K+
P: 8,639
@tvnaidu
that’s the problem with passwords in Javascript, you cannot hide them.
Oct 22 '09 #11

100+
P: 365
after I enter password, if I press "enter" button by default it should login automatically, but it is not logging in until I click on "login" button, is there anyway can I force to login when I press enter from keyboard after entering password?.
Oct 22 '09 #12

Dormilich
Expert Mod 5K+
P: 8,639
I’m not aware of all the possibilities (and maybe there is some default setting possible) but you can intercept the enter key press (there’s some event for keyboard actions somewhere…)
Oct 22 '09 #13

100+
P: 365
Is there anyway password display can scramble in view source?. or any other mechanism I can use not to show passwords in source code, I using this on small microcontroller based product to control and view status, I can store password in some files and do some comare
Oct 22 '09 #14

100+
P: 365
After I made the change to get cursor like below, curosr points, but my check fails when I click on login, it says "Invalid user id", if I change back, it passs but cursor not pinting.

now:

Expand|Select|Wrap|Line Numbers
  1. <input name="whatever" id="UserID" type="text"/> 
previous:

Expand|Select|Wrap|Line Numbers
  1. <input name="id" type="text">
Oct 22 '09 #15

Dormilich
Expert Mod 5K+
P: 8,639
@tvnaidu
nope.

that’s why password validation usually is made server side.
Oct 22 '09 #16

gits
Expert Mod 5K+
P: 5,320
your check relies on an input named 'id' ... so you shouldn't have changed the name of the input-node ... rechange it to 'id' ...
Oct 22 '09 #17

100+
P: 365
Appreciated. If I change back like below, as soon as I enter id and password and click on "login", check fails and it says "invalid user id", see this print coems from line 19 in the above 68 line program.

Expand|Select|Wrap|Line Numbers
  1. <input name="whatever" id="UserID" type="text"/>
Oct 23 '09 #18

gits
Expert Mod 5K+
P: 5,320
what did you do? i just said that you would need to rechange the 'name' attribute to:

Expand|Select|Wrap|Line Numbers
  1. <input name="id" id="UserID" type="text"/>
the focus-method uses the id to identify the node while your check-method uses the name. you need to understand the code a bit ... it is not of great help to you when i would just give you copy and paste code ... since that leads to confusion as you see :)

kind regards

PS: btw. please use [code] tags when posting source-code in your posts.
Oct 23 '09 #19

100+
P: 365
Thank you for your help, appreciated, I got it now. It is my mistake.
Oct 23 '09 #20

gits
Expert Mod 5K+
P: 5,320
no problem :) ... just post back to the forum in case you have more questions

kind regards
Oct 24 '09 #21

100+
P: 365
Gits.

Actually I posted another one, it prints half-way, remaining half not printing.
Oct 24 '09 #22

Post your reply

Sign in to post your reply or Sign up for a free account.