window.location.href与单击锚点
问题描述:
点击之间有什么区别
<a href />
vs.
呼叫window.location.href = ...
?
答
出于许多很好的原因,应尽可能在window.location.href
上使用<a href="foo.html">
.
Wherever possible, you should use <a href="foo.html">
over window.location.href
, for a number of very good reasons.
- 如果您禁用了javascript,则所有链接均无效.
- 蜘蛛(例如Google Bot)不会解释javascript,因此它们不会跟踪您的任何链接.
- 它破坏了互联网.不,确实如此-万维网 Web 是建立在页面之间可发现的链接的基础上的.将这些链接与非标准.. err,链接隐藏在一起就违反了这一前提.
- 这会带来糟糕的用户体验:用户希望当他们将鼠标悬停在链接上时,他们将可以访问一些信息:
- 状态栏中显示的目的地(非常重要!)
- 右键单击->复制链接位置
- 单击鼠标中键->打开新标签页
- 等
- 使用
window.location
会破坏所有这些
- If you have javascript disabled, none of the links would work.
- Spiders, such as Google Bot, do not interpret javascript, and so they won't follow any of your links.
- IT BREAKS THE INTERNET. No, really though - the World Wide Web is built on the very basis of discoverable linkages between pages. Hiding these linkages with non-standard .. err, links, goes against that very premise.
- It makes for a bad user experience: a user expects that when they mouse over a link, they will have access to some information:
- the destination displayed in the status bar (very important!)
- right-click -> copy link location
- middle-click -> open new tab
- etc
- Using
window.location
breaks all of these