window.location.href与单击锚点 [英] window.location.href vs clicking on an Anchor

查看:229
本文介绍了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.

  1. 如果您禁用了javascript,则所有链接均无效.
  2. 蜘蛛(例如Google Bot)不会解释javascript,因此它们不会跟踪您的任何链接.
  3. 它破坏了互联网.不,确实如此-万维网 Web 是建立在页面之间可发现的链接的基础上的.将这些链接与非标准.. err,链接隐藏在一起就违反了这一前提.
  4. 这会带来糟糕的用户体验:用户希望当他们将鼠标悬停在链接上时,他们将可以访问一些信息:
    • 状态栏中显示的目的地(非常重要!)
    • 右键单击->复制链接位置
    • 单击鼠标中键->打开新标签页
    • 使用window.location会破坏所有这些
  1. If you have javascript disabled, none of the links would work.
  2. Spiders, such as Google Bot, do not interpret javascript, and so they won't follow any of your links.
  3. 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.
  4. 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

这篇关于window.location.href与单击锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆