Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

link with target="_blank" does not open in new tab in Chrome

Tags:

html

hyperlink

On this website, the top accordion navigation contains an element called "Foundation": (screenshot).

This element is produced by HTML code:

<a href="http://www.foracure.org.au" target="_blank" style="width: 105px;"></a> 

However, in Chrome, when you click on this element, the new website does not open in a new tab.

Can you please tell me why? Thank you.

like image 810
Steve Avatar asked Apr 04 '15 07:04

Steve


People also ask

Can you force a URL to open in a new tab?

You can make a HTML link open in a new tab by adding the target=”_blank” attribute. You should insert this after the link address.

How do I open a link in a new tab in target?

a target=”_blank” Open in New Browser Tab (or Window) The target attribute specifies where the linked document will open when the link is clicked. The default is the current window. If target="_blank" , the linked document will open in a new tab or (on older browsers) a new window.

How do I make Chrome open links in a new tab?

To quickly open a link in a new tab on Google Chrome, hold down the control button while clicking on it with your mouse. On a Mac computer, hold down the command button rather than control.

Why link is not opening in new tab?

Alternatively, you can click a link while holding down Shift + Ctrl (PC) or Shift + Command (Mac). That should not only open the link in a new tab but also shift your focus to it.


2 Answers

Replace   <a href="http://www.foracure.org.au" target="_blank"></a>      with   <a href="#" onclick='window.open("http://www.foracure.org.au");return false;'></a> 

in your code and will work in Chrome and other browsers.

Thanks Anurag

like image 198
Anurag Avatar answered Sep 23 '22 18:09

Anurag


Because JavaScript is handling the click event. When you click, the following code is called:

el.addEvent('click', function(e){     if(obj.options.onOpen){         new Event(e).stop();         if(obj.options.open == i){             obj.options.open = null;             obj.options.onClose(this.href, i);         }else{             obj.options.open = i;             obj.options.onOpen(this.href, i);         }        }        }) 

The onOpen manually changes the location.

Edit: Regarding your comment... If you can modify ImageMenu.js, you could update the script which calls onClose to pass the a element object (this, rather than this.href)

obj.options.onClose(this, i); 

Then update your ImageMenu instance, with the following onOpen change:

window.addEvent('domready', function(){     var myMenu = new ImageMenu($$('#imageMenu a'), {         openWidth: 310,         border: 2,         onOpen: function(e, i) {             if (e.target === '_blank') {                 window.open(e.href);                 } else {                 location = e.href;             }         }     }); }); 

This would check for the target property of the element to see if it's _blank, and then call window.open, if found.

If you'd prefer not to modify ImageMenu.js, another option would be to modify your links to identify them in your onOpen handler. Say something like:

<a href="http://www.foracure.org.au/#_b=1" target="_blank" style="width: 105px;"></a> 

Then, update your onOpen call to:

onOpen: function(e, i) {     if (e.indexOf('_b=1') > -1) {         window.open(e);        } else {         location = e;     } } 

The only downside to this is the user sees the hash on hover.

Finally, if the number of links that you plan to open in a new window are low, you could create a map and check against that. Something like:

var linksThatOpenInANewWindow = {     'http://www.foracure.org.au': 1 };  onOpen: function(e, i) {     if (linksThatOpenInANewWindow[e] === 1) {         window.open(e);        } else {         location = e     } } 

The only downside is maintenance, depending on the number of links.

Others have suggested modifying the link (using # or javascript:) and adding an inline event handler (onclick) - I don't recommend that at all as it breaks links when JS is disabled/not supported.

like image 27
Jack Avatar answered Sep 23 '22 18:09

Jack