Question

I am developing a system which is 100% ajax, except of course, the first request.

I am interested in changing the address in document.location, using javascript. But I don't want the browser to load the page on that "new" location.

Does anyone know how I can do this?

Was it helpful?

Solution

To rewrite the entire location and not just the "hash" part, browser history API can be used, although currently it only seems to be supported in Gecko 1.9.3/Firefox 4.

history.replaceState({}, document.title, url)

OTHER TIPS

Changing the entire URL without navigating is not possible, just imagine the security issues that it could generate.

You can change only the location.hash, which is the part of the URL that follows the # symbol:

location.hash = "foo";

Your url will change to http://someurl.com/#foo

You can use the same method that Gmail uses. Append an anchor to the end of the url, the browser should not reload the page but you can read the information in document.location.href and act on it. This also will keep the functionality of the back button intact (providing your javascript supports it)

for example

first page is http://www.mypage.com/index.php you click to the next "page" using <a href="#page2">link</a> and it changes to http://www.mypage.com/index.php#page2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top