window.location.href Loads WHOLE URL When INCOMPLETE URL Input is Submitted by Form

30 views Asked by At

When text is entered into this form, it then loads a webpage with that typed text appended at the end of my domain name, to create the full page url. (Incidentally, it also disables the typed Enter key to submit, because I only want the clicked Submit button to submit the form.)

It works, but too well. Weirdly, I can type “ann” and be directed to mysite.com/annex. Worse yet, typing in “an” directs me to mysite.com/anabelle for example … it's defaulting to whichever existing page is first in alphabetical order (ana comes before ann). How do I limit this behavior, so users must type the ENTIRE word or else go to a 404 error?

<!-- Location Input Form ENTER Button Disabler. Prevents ENTER from submitting form. See also in body form: onkeypress="return noenter()" -->
<script type="text/javascript">
function noenter() {
  return !(window.event && window.event.keyCode == 13); }
</script>

<!-- Location Input Form Script, entered location must match url's "/folder" since it's appended to the domain name. For alts, add a redirect to HTACCESS -->
<script language="javascript" type="text/javascript">
    function noenter() {
       return !(window.event && window.event.keyCode == 13); }
    function buttonClick(){
      var url = document.getElementById("inputURL").value;
      window.location.href = "mysite.com/" + url;
      }
</script>

<!-- Location Input Form -->
<form>
<div class=formfieldcontainer>
<input class="formfield" onkeypress="return noenter()" type="text" id="inputURL" name="inputURL" value="">
</div>
<input class="submitbutton" type="button" value="Submit" onclick="buttonClick()">
</form>

I need users to have to type the ENTIRE word to access a page, not just the first few letters. Any ideas? Thank you!

0

There are 0 answers