Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: change the URL address without redirecting? [duplicate]

Possible Duplicate:
Modify Address Bar URL in AJAX App to Match Current State

How can I change the URL address without redirecting the page?

For instance, when I click on this link below:

<a href="http://mysite.com/projects/article-1" class="get-article">link</a> 

I will grab the URL from the link:

var path = object.attr('href'); 

If I do this below, the page will be redirected:

window.location = path; 

I want to do something like this site, when you click on the image, the ajax call will fetch the requested page and the URL address on the window will be changed too, so that it has a path for what you click.

like image 329
Run Avatar asked Jun 25 '11 14:06

Run


People also ask

How do I change URL without reloading?

There is no way to modify the URL in the browser without reloading the page. The URL represents what the last loaded page was. If you change it ( document. location ) then it will reload the page.

How do I use JavaScript to modify the URL without reloading the page?

the page using JavaScript? the page using JavaScript? Method 2: Adding a new state with pushState() Method: The pushState() method is used to add a new history entry with the properties passed as parameters. This will change the current URL to the new state given without reloading the page.

How can I change my URL in Ajax request?

You can do this to your success action : window. history. pushState("object or string", "Title", "/new-url");

How do I change my current URL?

Answer: Use the window. location. href Property location. href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc.


1 Answers

NOTE: history.pushState() is now supported - see other answers.

You cannot change the whole url without redirecting, what you can do instead is change the hash.

The hash is the part of the url that goes after the # symbol. That was initially intended to direct you (locally) to sections of your HTML document, but you can read and modify it through javascript to use it somewhat like a global variable.


If applied well, this technique is useful in two ways:

  1. the browser history will remember each different step you took (since the url+hash changed)
  2. you can have an address which links not only to a particular html document, but also gives your javascript a clue about what to do. That means you end up pointing to a state inside your web app.

To change the hash you can do:

document.location.hash = "show_picture"; 

To watch for hash changes you have to do something like:

window.onhashchange = function(){     var what_to_do = document.location.hash;         if (what_to_do=="#show_picture")         show_picture(); } 

Of course the hash is just a string, so you can do pretty much what you like with it. For example you can put a whole object there if you use JSON to stringify it.

There are very good JQuery libraries to do advanced things with that.

like image 154
david Avatar answered Sep 21 '22 16:09

david