I use AJAX in my gallery and I need to change URL in address bar. Is it possible and how? Thanks for replies.
Asked
Active
Viewed 1,249 times
0
-
2possible duplicate of [Change the URL in the browser without loading the new page using JavaScript](http://stackoverflow.com/questions/136458/change-the-url-in-the-browser-without-loading-the-new-page-using-javascript) – Rob W Apr 11 '12 at 18:55
-
@RobW Depending on whether or not Filip wants to load the page, and/or whether or not the URL is just a hash change. – Phrogz Apr 11 '12 at 18:57
-
@Phrogz AJAX + "changing URL in the adress bar" ringes a bell at me: The OP wants to change the URL without unloading the page. – Rob W Apr 11 '12 at 18:58
-
@RobW I suspect you're correct, but I'll wait to hit "Close" until we find out :) – Phrogz Apr 11 '12 at 18:58
2 Answers
2
Change the URL and possibly go to a new location (if the URL is not just a "#foo" hash):
location.href = 'new url';
If you want to 'lie' to the user and have the location change without actually going to the page:
history.pushState({}, "page 2", "bar.html");
For more on this latter example, read: Manipulating the Browser History.

Phrogz
- 296,393
- 112
- 651
- 745
0
You can set the the part of the url after the # value, called the location.hash. (Otherwise you'll be navigated to the new url, which you don't want I suppose).
you can set the hash part with an anchor (href="#abc") or with programmatically setting document.location.hash.

Peter Aron Zentai
- 11,482
- 5
- 41
- 71