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.
This is achieved through URL rewriting, not through URL obfuscating, which can't be done.
Another way to do this, as has been mentioned is by changing the hashtag, with
window.location.hash = "/2131/"
You can't do what you ask (and the linked site does not do exactly that either).
You can, however, modify the part of the url after the #
sign, which is called the fragment, like this:
window.location.hash = 'something';
Fragments do not get sent to the server (so, for example, Google itself cannot tell the difference between http://www.google.com/
and http://www.google.com/#something
), but they can be read by Javascript on your page. In turn, this Javascript can decide to perform a different AJAX request based on the value of the fragment, which is how the site you linked to probably does it.
No, because that would open up the floodgates for phishing. The only part of the URI you can change is the fragment (everything after the #
). You can do so by setting window.location.hash
.
You cannot really change the whole URL in the location bar without redirecting (think of the security issues!).
However you can change the hash
part (whats after the #
) and read that: location.hash
ps. prevent the default onclick
redirect of a link by something like:
$("#link").bind("click",function(e){
doRedirectFunction();
e.preventDefault();
})
That site makes use of the "fragment" part of a url: the stuff after the "#". This is not sent to the server by the browser as part of the GET request, but can be used to store page state. So yes you can change the fragment without causing a page refresh or reload. When the page loads, your javascript reads this fragment and updates the page content appropriately, fetching data from the server via ajax requests as required. To read the fragment in js:
var fragment = location.hash;
but note that this value will include the "#" character at the beginning. To set the fragment:
location.hash = "your_state_data";
See here - http://my.opera.com/community/forums/topic.dml?id=1319992&t=1331393279&page=1#comment11751402
Essentially:
history.pushState('data', '', 'http://your-domain/path');
You can manipulate the history object to make this work.
It only works on the same domain, but since you're satisfied with using the hash tag approach, that shouldn't matter.
Obviously would need to be cross-browser tested, but since that was posted on the Opera forum I'm safe to assume it would work in Opera, and I just tested it in Chrome and it worked fine.
Source: Stackoverflow.com