I inherited some code with <img src="../...gif" onclick="aURL">
.
I was wondering the reason behind this choice so I would like to know any potential advantages/disadvantages of window.location over a tag with href ?
I am considering changing to <a href="aURL"><img src="../...gif"></a>
.
Thanks,
The main disadvantage with the <img onclick="">
approach I immediately see is that various forms of clicks will not work, eg Ctrl +click opening in a new tab, Shift +click opening in a new window, etc.
Also, JavaScript being disabled is also an area the onclick="window.location=x;"
fails.
Definitely use an anchor here like you're thinking, that's its purpose.
As for the reason, I can't speak to that.
Typically, you're always better off setting the href on an anchor, even if you don't intend to use it. The href should be set as a fallback in case the user doesn't have JavaScript enabled, or if a bot is crawling your site and won't use the onclick event. If you have to open a popup, then put that in the onclick and return false so it doesn't hit the href. I would definitely recommend removing the onclick from the <img>
and putting it in an <a>
wrapping it.
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.