First of all, I am not having a real problem. I'm asking this out of curiosity only.
I accidentally run into a strange behaviour when using javascript:void(0)
and target="_blank"
within the same link, like this.
<a href="javascript:void(0);" target="_blank" /> Link </a>
I found that Chrome
is handling this normally and not doing anything when you click the link, while IE
and Firefox
open up a blank new tab.
My question is, isn't javascript:void(0)
supposed to prevent any click event firing from a link, even if it targets new tab/window? And why is target="_blank"
overiding it?
Also what is the best approach if I am, let's say, filling the href
attribute with some backend language and I prefer target="_blank"
hard coded beside the href
attribute?
The HTML target attribute defines where the linked document will open when the user clicked on the link. If target=”_blank” is set with anchor element, then the linked document will open in a new tab otherwise document is open in the same tab.
Conclusion and recommendationsUse the javascript:void(0) if you want to be raw, precise and fast. Use href="#" and prevent the default event with javascript to be a standards follower. If your link doesn't go anywhere, don't use an <a> element.
It is ok to use target="_blank" ; This was done away with in XHTML because targeting new windows will always bring up the pop-up alert in most browsers. XHTML will always show an error with the target attribute in a validate.
The difference: target="_blank" is a special keyword that will open links in a new tab every time. target="blank" will open the first-clicked link in a new tab, but any future links that share target="blank" will open in that same newly-opened tab.
<a href=# onclick="return false;">I look like a link!</a>
Always used this.
EDIT: To your question about void(0), the onclick is supposed to have a higher priority than the href attribute because it provides richer functionality and has the capability of actually preventing the href from operating - thus my return false
. The href is only there for robots and old browsers if the onclick event is present.
Also, whether a regular navigation is performed with oddly-formed URLs is at the discretion of the browser. If the link starts with a hash, it should just jump to a bookmark. But when it's empty or contains other protocols, it all depends on the browser if it decides to try and visit the new document. Entering the javascript:
pseudo protocol is traditionally used for bookmarklets which are expected to work with the current page, so anything javascript:
that doesn't do document.write()
or other destructive modification should leave the page unreloaded.
EDIT2: I'm not sure what you mean by backend language in href, but consider HTML-valid data-
attributes to give your code something to work with if it doesn't belong logically in a href attribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With