简体   繁体   中英

Create URL upon JavaScript submit form

I currently have a form with some JavaScript functions and localstorage.

I'm trying to get that when a user types a value into a textbox, the search bar changes the URL from "mysite.com" to "mysite.com/%userinput%". Then that user can send that link to someone else and that person will then see what the original user saw.

This will change the URL after input.

As I understand from your question and comments, you don't want to load the URL, just change it, so try this fiddle : http://jsfiddle.net/GrP6U/2/show/

The code behind is:

JavaScript

var theForm = document.getElementById('theForm');
var theInput = document.getElementById('subj');

theForm.onsubmit = function(e) {
    var myurl = "http://jsfiddle.net/GrP6U/2/show/?input=" + encodeURIComponent(theInput.value);
window.history.pushState('', "Title", myurl);
    return false;
}

HTML

<form id="theForm">
    <input id='subj'/>
    <input type='submit'/>
</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