is it possible to change the location url without the browser refreshing the page ?
imagine I want the user to change some values on some fields and as a consequence of this
to update is url (following the question mark)
so http://www.mysite.com/mypage?level=1&fav_fruit=apple&fav_food=pasta&…
the idea is that if it changes the ‘favourite food’ i might change the url only
http://www.mysite.com/mypage?level=1&fav_fruit=apple&fav_food=rice&…
but I don’t want to refresh the page.
why ? so he/she can continue to use the page and when he/she is tired he can bookmark it the way it left it (given a good name to it)
any idea ?
I will do this with javascript.
It’s not possible to update the part after the question mark without refreshing the page, but you can update the URL hash (so it looks like http://domain.com/file.php#fav_fruit=apple). I don’t know how to do it in plain old JavaScript but I have done this using jQuery and the jQuery.address plugin.
You can then set the textbox to update the URL every time the value is changed by doing something like the following:
The good thing with this plugin is that you can attach a function so that you can detect when the URL is changed (i.e. when the bookmark is reloaded):
By default, the plugin will create a history action everytime a value is update (so you can press back/forward). You wouldn’t want this so you’d have to have this before you set any values:
Update: Here’s what it looks like in plain old JavaScript:
It’s not pretty, but it works.
changeis what you set your form elements to call and it will set the URL hash with the required bits. When you want to set the values in the form fields (for example, when you load the page), you call thereadHashto get an object with all the parameters/values in thelocation.hashfield.Example of it working at http://jsfiddle.net/ENHFN/ (although you can’t see the location hash change, it uses the variable). I kept getting errors when I tried to separate it out.