简体   繁体   English

Hashtags(片段标识符)VS Javascript History API

[英]Hashtags (Fragment identifiers) VS Javascript History API

Which one is better? 哪一个更好? Using fragment identifiers... 使用片段标识符......

http://www.alinkthatdoesnotwork.com/#!/dir1/dir2/file.html

...or the new Javascript History API? ...或新的Javascript History API?

https://github.com/examplethatdoesnotwork/project/src/script.js

Or should I use both? 或者我应该同时使用两者? (via fallback) (通过后备)

Aspects: 方面:

  • Compatibility/Support 兼容性/支持
  • Speed 速度
  • Convenience 方便

Hashtags are a means to categorise content on Twitter, you mean fragment identifiers. Hashtags是一种在Twitter上对内容进行分类的方法,您的意思是片段标识符。

Using fragment identifiers to indicate what content to load via Ajax is a terrible idea. 使用片段标识符来指示通过Ajax加载的内容是一个糟糕的主意。 They are a hack that is fragile, search engine unfriendly (except with more hacks on both sides) and depends on JavaScript . 他们是一个脆弱的黑客,搜索引擎不友好(除了双方更多的黑客)并依赖于JavaScript

The history API is a robust system that is actually designed to do that job. 历史API是一个强大的系统,实际上是为完成这项工作而设计的。 The only problem with it is browser support , but (unlike the fragment identifier approach) it gracefully degrades to real URIs that will be passed directly to your server (which is what Github does ). 唯一的问题是浏览器支持 ,但(与片段标识符方法不同)它优雅地降级为将直接传递给您的服务器的真实URI(这是Github所做的 )。

Even Twitter appear to be about to switch to the history API . 甚至Twitter似乎即将切换到历史API

The history API is vastly preferable, so long as normal links work normally in browsers that do not support it. 历史API非常可取,只要普通链接在不支持它的浏览器中正常工作即可。

You could use a library such as History.js to enable it in these browsers. 您可以使用诸如History.js之类的库在这些浏览器中启用它。

More information here: It's About The Hashbangs , Side Effects of Hash-Bang URLs . 更多信息: 这是关于HashbangsHash-Bang URL的副作用

In a nutshell, URLs are important. 简而言之,URL非常重要。 URLs are forever, and cool URLs do not change, and finally: Once you hashbang, you can't go back. 网址是永远的,酷网址不会改变,最后:一旦你做好了,你就不能回去了。

The new history methods are incredibly useful for AJAX navigation. 新的历史方法对AJAX导航非常有用。 For example, pushState or replaceState allow you to update the browser's address bar so the user sees a clean URL instead of an ugly hashtagged thing. 例如,pushState或replaceState允许您更新浏览器的地址栏,以便用户看到一个干净的URL而不是一个丑陋的hashtagged事物。

However, as I'm sure you're aware, support for the new API is still limited. 但是,我相信您已经知道,对新API的支持仍然有限。 location.hash is much more widely supported at this point, meaning that you'd have to write a hash fallback for browsers that can't take advantage of the newer window.history stuff. 此时,location.hash得到了更广泛的支持,这意味着你必须为那些无法利用更新的window.history东西的浏览器编写一个哈希回退。

I think the question is what is support. 我认为问题是什么是支持。 You can't go solely with History API at the moment because it is not supported by IE. 您目前不能单独使用History API,因为IE不支持它。 You'll need a fallback solution like GitHub does. 你需要一个像GitHub那样的后备解决方案。

You've listed "compatibility" as your first criterion. 您已将“兼容性”列为第一个标准。 Since the history API isn't yet supported by all major vendors (I'm looking at you, Microsoft), not even in their most-recent releases (IE9 doesn't have it), that pretty much means you have to use the hash. 由于历史API 尚未得到所有主要供应商的支持 (我看着你,微软),甚至在他们最近的版本中都没有(IE9没有它),这几乎意味着你必须使用哈希值。 (Which is too bad, but there we are.) And it's not just Microsoft, lots of mobile devices are using one- or two-versions back of their mobile browser, and so still don't have it. (这太糟糕了,但我们确实如此。)而且不仅仅是微软,很多移动设备都在使用移动浏览器的一个或两个版本,所以仍然没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM