How would I have a JavaScript action that may have some effects on the current page but would also change the URL in the browser so if the user hits reload or bookmark the new URL is used?
It would also be nice if the back button would reload the original URL.
I am trying to record JavaScript state in the URL.
my code is:
and action:
and example
That's all :)
I was wondering if it will posible as long as the parent path in the page is same, only something new is appended to it.
So like let's say the user is at the page:
http://domain.com/site/page.html
Then the browser can let me dolocation.append = new.html
and the page becomes:http://domain.com/site/page.htmlnew.html
and the browser does not change it.Or just allow the person to change get parameter, so let's
location.get = me=1&page=1
.So original page becomes
http://domain.com/site/page.html?me=1&page=1
and it does not refresh.The problem with # is that the data is not cached (at least I don't think so) when hash is changed. So it is like each time a new page is being loaded, whereas back- and forward buttons in a non-Ajax page are able to cache data and do not spend time on re-loading the data.
From what I saw, the Yahoo history thing already loads all of the data at once. It does not seem to be doing any Ajax requests. So when a
div
is used to handle different method overtime, that data is not stored for each history state.What is working for me is -
history.replaceState()
function which is as follows -This will not reload page, you can make use of it with event of javascript
Facebook's photo gallery does this using a #hash in the URL. Here are some example URLs:
Before clicking 'next':
After clicking 'next':
Note the hash-bang (#!) immediately followed by the new URL.
Browser security settings prevent people from modifying the displayed url directly. You could imagine the phishing vulnerabilities that would cause.
Only reliable way to change the url without changing pages is to use an internal link or hash. e.g.: http://site.com/page.html becomes http://site.com/page.html#item1 . This technique is often used in hijax(AJAX + preserve history).
When doing this I'll often just use links for the actions with the hash as the href, then add click events with jquery that use the requested hash to determine and delegate the action.
I hope that sets you on the right path.
With HTML 5, use the
history.pushState
function. As an example:and a href:
If you want to change the URL without adding an entry to the back button list, use
history.replaceState
instead.