views:

73

answers:

3

How do I change the URL with jQuery without reloading the page?

A: 

You can only change the hash part of an url without a page refresh through location.hash. Adding get parameters (?foo=bar) or a complete url change will always reload the page.

In HTML5 you get more options to change URL's, but right now (2010) it's not yet viable since crappy browser die out hard.

BGerrissen
Thats not strictly true any more. Check out the HTML5 history API: https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history#Adding_and_modifying_history_entries
DanSingerman
@DanSingerman Which doesn't work with IE7/8 and thus still not viable imo. I would like it to be though, but I won't base my awnsers on it just yet.
BGerrissen
-1: a fragmentidentifier (or hash) is a "real" part of the URL and can be changed from javascript. So, "You can"
Caspar Kleijne
@BGerrissen - I know. But when you give an absolute like you did, I think mentioning how technology is changing is a valid caveat. Remember this answer should be around for years.
DanSingerman
Changed my awnser accordingly, thanks for the comments.
BGerrissen
+3  A: 

Set location.hash

jQuery has nothing to do with it though, this is basic DOM 0.

David Dorward
This was just what i needed :)
Profeten
A: 

In general, leaving aside the new HTML history API, you can't.

It is possible to add data to the URL after the hash (using location.hash as David Dorward describes). However, if you want to do this to affect behaviour of the page, you will also need to read these changes via jQuery (or triggered by the same process that sets the hash.)

Outside of the History API you will not be able to add a parameter, and have the page 'just know about it'.

DanSingerman