admin管理员组

文章数量:1287593

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?

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?

Share Improve this question edited Jul 3, 2013 at 16:11 Matt 23.8k18 gold badges74 silver badges116 bronze badges asked May 29, 2010 at 23:19 MiroMiro 1081 silver badge5 bronze badges 1
  • 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 bination of both of them is the best result. – scunliffe Commented May 29, 2010 at 23:59
Add a ment  | 

3 Answers 3

Reset to default 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)

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./#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./index.php you click to the next "page" using <a href="#page2">link</a> and it changes to http://www.mypage./index.php#page2

本文标签: javascriptRewrite documentlocation without loadingStack Overflow