location.href跳转和给a标签设置点击事件有什么区别
我本来用的是window.location.href=url
来跳转都相关的页面,但是在某些第三方app里,发现好像不管用了,看别人的代码,他是先创建一个隐藏的a标签,然后给这个a标签一个click事件,执行跳转:
var a = document.createElement("a");
a.setAttribute("href", aV);
a.style.display = "none";
var ev = document.createEvent('HTMLEvents');
ev.initEvent('click', false, true);
a.dispatchEvent(ev);
那么请问location.href跳转和给a标签设置点击事件有什么区别呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Wherever possible, you should use
<a href="foo.html">
overwindow.location.href
, for a number of very good reasons.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 theseIt's much easier!
Reference: https://stackoverflow.com/que...