0

I made a simple Chrome Extension that, when clicked, has a dropdown menu that has a row of icons. Is it possible to set it so when the icon is clicked, it opens its respective website in a new tab?

(NOTE: I have tried to make it refer to a link in the popup.html file, but if I click on the icon, it doesn't do anything.)

Any help, suggestions or advice is greatly appreciated, thanks!

EvilLemon
  • 3
  • 3

2 Answers2

0

You need to assign a click handler to the icon that executes the following:

chrome.tabs.create({url: websiteToOpen});

If you want it to open in the background without closing the popup, one more parameter is required:

chrome.tabs.create({url: websiteToOpen, active: false});

Do note that "tabs" permission is not needed for this.

This example from Chrome docs shows how to properly assign a click hanlder in a way that's compatible with Chrome extensions (onclick attribute will not work)

Xan
  • 74,770
  • 16
  • 179
  • 206
0

@Xan Hey, thanks for your answer and sorry for the late reply... I figured it out though, and I didn't need to add any click handlers... I just did:

<a target = "_blank" href = "http://google.com">
  <img src = "http://upload.wikimedia.org/wikipedia/commons/a/a0/Google_favicon_2012.jpg"/>
</a>

the target attribute says to open it in a new tab and it does exactly that when I click on it. Problem solved! Thanks!

EvilLemon
  • 3
  • 3
  • 1
    This works for most URLs, but not all. For instance, you can't open non-web URLs with it (`chrome://`, `chrome-extension://`, etc.) My solution is more robust and allows more customization. – Xan May 03 '15 at 11:13
  • Just a heads up to those using this solution in the future. This method is vulnerable to reverse tabnapping. See https://stackoverflow.com/questions/50709625/link-with-target-blank-and-rel-noopener-noreferrer-still-vulnerable/50709760 for a solution. – Spiderixx Nov 18 '19 at 08:31