views:

169

answers:

4

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?

miro

+8  A: 

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

CMS
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
@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!
aSeptik
+1  A: 

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
+2  A: 

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)
Iggy Kay
Damn!Thank you!
Miro
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...
CMS
@Miro Do you really only have Gecko 1.9.3/Firefox 4 users on your site? :)
bzlm
A: 

parent.document.location.href = "your data";

jeanck