Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript:void(0) and target="_blank" behaviour

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?

like image 605
Goran Lepur Avatar asked Jun 13 '13 18:06

Goran Lepur


People also ask

What is target _blank in JavaScript?

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.

Should I use JavaScript void 0?

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.

Why target _blank is deprecated?

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.

What is the difference between target blank and target _blank?

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.


1 Answers

<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.

like image 107
Zdenek Avatar answered Oct 10 '22 02:10

Zdenek