简体   繁体   中英

<a> tag inside div with window.location - why?

I'm currently trying to fix a few bugs on a website that has been built by some guys. The thing is, I'm having trouble seeing the point of a few things they've done.

The website has a <div> with an onclick="window.location='foobar'" and inside it an <a> tag. Both lead to the same place.

Is there a reason for that?

Thank you!

Some developers are better than others.

More importantly, developers are human and make mistakes. You've found one.


regarding why a developer would use <button onclick="location='somewhere'"> , there's a lot of bad advice on the internet , even on stackoverflow , even by high rep users (not trying to pick on j08691 , just making a point).

Additionally, button elements may not contain a elements per the specification, so a nested anchor is invalid.

With all that said, the page probably still works. The thing that makes HTML really powerful is its ability to fail gracefully. Instead of erroring out or preventing the entire page from working, the browser is able to make things work, even when the developer does something silly like writing invalid HTML.

I only see downsides:

  • The user can't use right-click copy link. It will just copy the javascript
  • Bots from search engines won't follow the link
  • Users that have javascript disabled can't navigate using that link

However if I understand you correctly, then there is <a href="foobar"> around it? If that is true, then that would render the disadvantages I have listed above to not apply. In this case the author of the website may have used this technique as some sort of a hack to style something on multiple browsers the same way...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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