简体   繁体   中英

Button only works on click, not on enter/return

I have a simple password protection set up on my website to keep out random visitors and give clients a sense of security.

I understand that the password can be easily pulled from the source code, but that isn't a problem.

When the password is entered, it only works if the button is clicked. When enter is hit (which most people default to), it just reloads the page with "?pass=password" at the end of the location. It would be ideal if the button could be clicked and/or enter could be pressed.

In head...

</script>
<!--- PASSWORD PROTECTION SCRIPT --->    
function TheLogin() {    
   var password = 'accessportfolio';    
   if (this.document.login.pass.value == password) {
      window.location.href="protected.html#portfolio";
   } else {
      location.href="incorrect.html";
   }
}    
<!--- End hiding --->
</script>

In Body...

<form name="login" style="margin: 0px">
<INPUT TYPE="text" NAME="pass" size="17"
       onKeyDown="if(event.keyCode==13) event.keyCode=9;" 
       style="width: 250px; margin: 5px;"><br>
<input type="button" value="CLICK TO ACCESS" 
       style="width : 150px; margin: 10px"
       onClick="TheLogin(this.form)">
</form>

The button should be a submit button and you should be using onsubmit of the form and not onclick of a button.

 function TheLogin() { var password = 'accessportfolio'; if (this.document.login.pass.value == password) { window.location.href = "protected.html#portfolio"; } else { location.href = "incorrect.html"; } } 
 <form name="login" style="margin: 0px" onsubmit="TheLogin(); return false;"> <INPUT TYPE="text" NAME="pass" size="17" style="width: 250px; margin: 5px;"> <br> <input type="submit" value="CLICK TO ACCESS" style="width : 150px; margin: 10px"> </form> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM