backbone.js history 里的 pushState 究竟是什么含义?

  • 3.4k

pushState support exists on a purely opt-in basis in Backbone. Older browsers that don't support pushState will continue to use hash-based URL fragments, and if a hash URL is visited by a pushState-capable browser, it will be transparently upgraded to the true URL. Note that using real URLs requires your web server to be able to correctly render those pages, so back-end changes are required as well. For example, if you have a route of /documents/100, your web server must be able to serve that page, if the browser visits that URL directly. For full search-engine crawlability, it's best to have the server generate the complete HTML for the page ... but if it's a web application, just rendering the same content you would have for the root URL, and filling in the rest with Backbone Views and JavaScript works fine.


阅读 5.4k
2 个回答

Backbone 的路由, 通过监听网址变化来起作用.. 这个题主先要了解.
还有要了解到, 其实有两种网址 /path /#path.. 这两个什么区别.

旧的浏览器不支持其中一种, 因此 Backbone 提供了 pushState 的选项.
关键词: HTML5 History API

自问自答一下吧,之前旧的浏览器只能通过hashchange来判断单页app地址的变化,而由于html5 history api的出现,允许通过/path 这样的模式来渲染页面,这种模式就叫做pushState,pushState设为true意味可以采用这种模式。在backbone里面,会判断浏览器对history api的支持,不支持的浏览器自动降格为hashchange的方式来切换页面。

  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进