1

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?

Matt
  • 22,721
  • 17
  • 71
  • 112
Miro
  • 108
  • 1
  • 5
  • 1
    why do you want to change the browser location address? If this is so that the user/Google can link into any page then just use regular HTTP requests. By using 100% AJAX you are creating a "single page site". There's pro's and cons to both regular HTTP and AJAX... I've always found a good combination of both of them is the best result. – scunliffe May 29 '10 at 23:59

3 Answers3

7

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

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • The idea is good, but that would make search engine (google, yahoo, etc...) indexing useless, because they would consider it the same page. And the url I'm using is not considering the hash. http://site.com/products/detail/1 ( this is good) http://site.com#/products/detail/1 ( this is not good ) Does anyone think this is possible? – Miro May 29 '10 at 23:34
  • @Miro - hummm... first of all you can "of course" make the first request by using AJAX, second, i think you should look to other things before worry about your url in orther to make your site SEO friendly, third you have 2 options, or you stop code by now or start learn something by now! – Luca Filosofi May 29 '10 at 23:46
7

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)
Doctor Kicks
  • 494
  • 2
  • 4
  • 1
    Miro: Just note that *fake* urls won't be indexed by search engines either, and if the user makes a bookmark or simply refreshes the page, he will get a 404, unless you implement a sort of url-rewriting technique at the server-side... Also, you will need to wait until `history.replaceState` is supported by mature browsers, right now is only available on Firefox 3.7 pre alpha and Chrome 5 Beta AFIK... – Christian C. Salvadó May 30 '10 at 03:24
  • 1
    @Miro Do you really only have Gecko 1.9.3/Firefox 4 users on your site? :) – bzlm Oct 22 '10 at 13:39
1

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

Geek Num 88
  • 5,264
  • 2
  • 22
  • 35