简体   繁体   中英

javascript Redirect user to directory based on form input

someone give me a part of the answer Redirect user to directory based on form input

<form onsubmit="location.href='http://www.example.org/' + document.getElementById('myInput').value; return false;">
<input type="text" id="myInput" />
<input type="submit" />
</form>

the code implement like this in the browser :

http://www.example.com/contact

my issue is :

this address become not working with me without adding extension .html

like this

http://www.example.com/contact.html

i read about solution in the .htaccess

but I'm looking if we can implement this too in java script

so the use just Enter the word contact as the example before and java script do the other ?

regrads

As others have mentioned in the comments, the way to get 100% of what you're asking for is to add a rule to the .htaccess (assuming Apache) that makes the server add the file extension.

The closest I think you could do purely on the client side is to simply add the .html yourself, maybe like this:

<form onsubmit="handleFormSubmit();">
<input type="text" id="myInput" />
<input type="submit" />
</form>

And then somewhere in your javascript:

function handleFormSubmit() {
    var destinationUrl = 'http://www.example.org/' + document.getElementById('myInput').value + ".html";

    window.location = destinationUrl;
}

The biggest problem I see with what you're wanting to do, though, is that you're relying on the user to know what the names of the available pages are, unless you have something more going on on the server side.

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