Possible to rewrite url clientside with javascript without reloading page

前端 未结 4 501
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-31 06:16

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)

相关标签:
4条回答
  • 2020-12-31 06:52

    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.

    0 讨论(0)
  • 2020-12-31 07:07

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

    location.hash = "#value";
    
    0 讨论(0)
  • 2020-12-31 07:09

    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.

    0 讨论(0)
  • 2020-12-31 07:11

    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 讨论(0)
提交回复
热议问题