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.
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.
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.
You can do this to your success action : window. history. pushState("object or string", "Title", "/new-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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With