Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML input form and Javascript

I have a simple HTML input form with a button. Fist I added javascript so the form can be submitted by pressing enter key not only by clicking button. That worked perfectly. After that I added javascript so when certain keyword is entered the user gets redirected to some other URL. Here is where the problem came. The users get redirected normally when pressing button, but when I press enter it leads me to 404 page. Here is my code:

<script type="text/javascript">
<!--
function enter(e){
    if(e.keyCode == 13)
        Login();
}
//-->
</script>

 <script type="text/JavaScript">
 <!--
 function Login(){
 var keyword=document.getElementById("address").value;
 var done=0;
 keyword=keyword.toLowerCase();
 keyword=keyword.split(' ').join('');
 if (keyword=="example,example") { window.location="http://www.example.com"; done=1; }
 if (keyword=="example1") { window.location="http://www.example1.com"; done=1; }
 if (keyword=="example2") { window.location="http://www.example2"; done=1; }
 if (done==0) { codeAddress(); }
 }
 //-->
 </script> 

<form name="enterkeyword" action="none">
    <input name="keyword" id="address" type="text"  onkeypress="return enter(event);"/>
    <div class="buttons">
    <button type="button" onclick="Login()">Submit</button>
    </div>
    </form>

Any help?

like image 672
Dreadlord Avatar asked Sep 14 '26 23:09

Dreadlord


1 Answers

Change:

<script type="text/javascript">
<!--
function enter(e){
    if(e.keyCode == 13)
        Login();
}
//-->
</script>

to

<script type="text/javascript">
<!--
function enter(e){
    if(e.keyCode == 13)
    {
        Login();
        return false;
    }
}
//-->
</script>
like image 178
Niels Avatar answered Sep 17 '26 12:09

Niels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!