www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 11-22-2009, 11:13 AM
    fdanell fdanell is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 5
    self.location.href wont happen?

    Hi!
    I have a problem with a javascript and I am hopeing anyone have any suggestions. On one of my pages I have a simple login-form, its not ment to be secure or anything.

    Once my form was complete and functioning ok, I also wanted the "login" to happen when the enterkey was pressed (instead of having to press a button after typing the password). I cant redirect it to the page with self.location.href.

    This is my form:

    Code:
    <form>
    <br>
    <center>
    Password: <input type="password" name="password" onKeyPress="Loginb(this,event,this.form);" style="background:#bfbfbf;color:#212121;border-color:#212121;" onFocus="this.style.background = '#ffffff';" onBlur="this.style.background = '#bfbfbf';">
    <br>
    <input type="button" value="Login" onClick="Login(this.form);" style="background:#bfbfbf;color:#000000;border-color:#212121;" onMouseOver="this.style.color = '#404040';" onMouseOut="this.style.color = '#000000';" onFocus="this.style.color = '#404040';" onBlur="this.style.color = '#000000';">
    </center>
    </form>
    and this is the javascript:

    Code:
    function Login(form) {
    password = new Array("p1","p2","p3","p4");
    page = "b" + ".html";
    if (form.password.value == password[0] || form.password.value == password[1] || form.password.value == password[2] || form.password.value == password[3]) {
    self.location.href = page;
    }
    else {
    alert("Wrong password.\nTry again.");
    form.password.focus();
    }
    return true;
    }
    
    function Loginb(myfield,e,form)
    {
    password = new Array("p1","p2","p3","p4");
    page = "b" + ".html";
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;
    
    if (form.password.value == password[0] || form.password.value == password[1] || form.password.value == password[2] || form.password.value == password[3]) {
    	
    	if (keycode == 13)
    	{
    		alert("Login!");
    		self.location.href = page;
    	}
    }
    else
    {
    	if (keycode == 13){
    		alert("Wrong password.\nTry again.");
    		form.password.focus();
    	}
    } 
    
       return true;
    }
    One strange thing, the alert("Login!"); shows up indicating that I have pass that "if" statement, but it does not redirect to the page. To me this is a mystery but my knowledge in javascript is not great.

    Thnx
    /Danell
    Reply With Quote
      #2  
    Old 11-22-2009, 12:51 PM
    Fang's Avatar
    Fang Fang is offline
    Resistance is futile
     
    Join Date: Apr 2003
    Location: Netherlands
    Posts: 18,428
    Works as expected. Is that the complete document?
    __________________
    At least 98% of internet users' DNA is identical to that of chimpanzees
    Reply With Quote
      #3  
    Old 11-22-2009, 12:57 PM
    fdanell fdanell is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 5
    hmm, does it work even when you press enter efter typing password (eg. p1)?
    for me i got the alert but then nothing.
    Reply With Quote
      #4  
    Old 11-22-2009, 01:01 PM
    fdanell fdanell is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 5
    and no, its not the complete html document. it has a <html> and loads the javascript from a file in the header.

    It seems that when I am pressing enter after typing the password the page reloads.
    Reply With Quote
      #5  
    Old 11-22-2009, 01:10 PM
    fdanell fdanell is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 5
    i discovered something!

    the line:
    Code:
    if (keycode == 13)
    	{
    		alert("Login!");
    		self.location.href = page;
    	}
    if i put the alert AFTER instead of BEFORE the self.location.href like this:

    Code:
    if (keycode == 13)
    	{
    		self.location.href = page;
                    alert("Login!");
    	}
    Then it works, its redirecting me. BUT... I dont want to have an alert, and if I take it away its not redirecting me.

    This is... well... a complete mystery to me.
    Reply With Quote
      #6  
    Old 11-22-2009, 01:19 PM
    Fang's Avatar
    Fang Fang is offline
    Resistance is futile
     
    Join Date: Apr 2003
    Location: Netherlands
    Posts: 18,428
    pressing the Enter key initiates the submission of the form, his will prevent that
    Code:
    <form onsubmit="return false;">
    __________________
    At least 98% of internet users' DNA is identical to that of chimpanzees
    Reply With Quote
      #7  
    Old 11-22-2009, 01:34 PM
    fdanell fdanell is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 5
    perfect!
    thanks alot!
    I guess putting the alert after self.loca... made it to have time to move before the form was submitted! again, thank you!
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting 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 Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 11:03 AM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.