开发者

Possible to rewrite url clientside with javascript without reloading page

开发者 https://www.devze.com 2023-01-01 11:18 出处:网络
Is it possible to rewrite the URL in the URL-field on the client\'s browser? So when a person clicks on a link on my page something ajax happens (eg a tab shows up) i want the url to display the acti

Is it possible to rewrite the URL in the URL-field on the client's browser?

So when a person clicks on a link on my page something ajax happens (eg a tab shows up) i want the url to display the actio开发者_如何学JAVAn without refreshing the page.

Is this possible?


When everyone else answered this question, they were right, it wasn't possible to change the URL in javascript. With HTML5 and modern browsers, it now is using the HTML5 History API

diveintohtml5.info article about it. Here is another good blog post explaining the feature.

Check caniuse.com for browser compatability.

There are libraries such as History.js which wrap the API and pollyfill using hashbangs for old browsers.


You can change the hash/anchor part of the URL (after #). The rest of the URL is readonly.

location.hash = "#value";


URL rewriting is not possible, but it is very common to append action data with a hash. For example, Facebook uses it extensively:

http://www.facebook.com/#!/username
                        ^

Everything after the hash is just anchor data in the same page, which you can modify by script all day long.

Newer browsers also offer the onhashchanged event so you can react to the "URL" changing instead of polling it constantly.


The only possible solution is to append an anchor. Then you can use somwething like reallysimplehistory to allow the user to bookmark a page or use the back button within you ajaxified page.

I have done that with a tabbed page in the past, too.

0

精彩评论

暂无评论...
验证码 换一张
取 消