2

I'm having issues trying to put a simple password protect script in my HTML (I know its not secure, I don't need high level protection) but the redirect function doesn't work.

I'm having trouble with the input:image Onclick action it doesn't redirect to the proper page when the correct password is entered. If I use a input:button then it works but I want to use a input:image. What am I doing wrong? Any help is appreciated.

Here is the Javascript:

<script language="javascript">
<!--//
/*PW Script*/
function pasuser(form) {
if (form.pass.value=="fred") {              
window.location="portfolio.html";
} 
else { 
	alert("Please enter the correct Password. Or request one under contacts");
}
}

//-->
</script>

Here is my form to call the Javascript:

 <form name="login">
            <fieldset>	
            <div class="LC_field2">
           	
           	    <span class="ghosttext">
				<input type="password" name="pass" placeholder="Password"> 
              </span>
            </div>    
			<div class="LC_field3">    
                <input type="image" border="0" src="images/go_button.png" width="52" height="25" alt="Go" onClick="pasuser(this.form);" />
             </fieldset>
			</form>

3
  • Notice, that clicking on input type="image" submits the form. For some reason form submitting overrides setting the location. (Related question.) Also you shouldn't hardcode passwords to the page. Commented Dec 16, 2014 at 19:41
  • The simplest solution would be to remove the useless form. Commented Dec 16, 2014 at 19:58
  • @Teemu Thanks for your suggestion. I know its not secure but the page it goes to doesn't have anything valuable. I just need a light secure layer feature Commented Dec 17, 2014 at 23:52

3 Answers 3

2

You just have to return false on your onclick event to prevent the default browser behavior for input elements.

onClick="pasuser(document.forms.login); return false;"
Sign up to request clarification or add additional context in comments.

Comments

1

First of all you should do like this.

And then,

<script language="javascript">
<!--//
/*PW Script*/
function pasuser(form) {
    var pass = document.getElementsByName("pass")[0].value;
   if (pass == "fred") {              
    var your_url            = "portfolio.html";
    window.location.href       = your_url;
   } 
  else
  { 
    alert("Please enter the correct Password. Or request one under contacts");
  }
}

//-->
</script>

Comments

0

Change this:

<input type="image" border="0" src="images/go_button.png" width="52" height="25" alt="Go" onClick="pasuser(this.form);" />

to this:

<img border="0" src="images/go_button.png" width="52" height="25" alt="Go" onClick="pasuser(document.forms.login);" />

1 Comment

No really, an <img/> tag with a click event is not an input field and will not trigger form submission.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.