Search code examples
javascriptanchorhref

How to open a window in new tab with just href attribute?


Edit: The link above doesn't contain any answer that applies to this question. If the person who put it there actually read this question they would have realized this.

What's my best option for opening a link in either a new tab or new window when I only have control over the href attribute of an anchor tag?

We have a homegrown menu navigation system that someone before me built and it sets the href of the menu anchor links based on a url column in a database table. So I know there are much better ways to open a link in a new window than what I'm asking for and I wouldn't normally try this, but for now I just need to do it this way.

I've tried using:

javascript:window.open('http://www.goodwill.org')

That does work for opening the link in a new tab, however it changes the current tab to be a blank page except for the text [object], which obviously defeats the purpose.


Solution

  • You need to ensure that the JS expression in your link does not return a value:

    javascript: void(window.open('http://www.goodwill.org'))