I have a Chrome extension that has some links in it. Currently when clicked the links do nothing, i would like to make them open in a new tab when clicked. Is this possible?
Asked
Active
Viewed 6,869 times
10
-
There are also some good answers here: http://stackoverflow.com/questions/8915845/chrome-extension-open-a-link-from-popup-html-in-a-new-tab – rogerdpack Oct 20 '16 at 19:51
3 Answers
27
Add target="_blank"
to links.
Another way is to attach link opening javascript code to mousedown event on a link.
You can also use base
tag to make all links open with target="_blank"
:
<head>
<base target="_blank">
</head>

Zev Eisenberg
- 8,080
- 5
- 38
- 82

serg
- 109,619
- 77
- 317
- 330
-
2+1. Make sure to specify `http://` (not just www.) in the links or they will open relative to the extension. – Kai Dec 28 '10 at 22:07
-
ok, thing is i was getting javascript from an external src so to my knowledge i can't edit it. – user556396 Dec 28 '10 at 22:10
-
@user556396 so you can't edit html and can't edit javascript? Is this inside iframe or something? – serg Dec 28 '10 at 22:12
-
6
I had the same issue and this was my approach:
- Create the popup.html with link (and the links are not working when clicked as Chrome block them).
- Create popup.js and link it in the page:
<script src="popup.js" ></script>
Add the following code to popup.js:
document.addEventListener('DOMContentLoaded', function () { var links = document.getElementsByTagName("a"); for (var i = 0; i < links.length; i++) { (function () { var ln = links[i]; var location = ln.href; ln.onclick = function () { chrome.tabs.create({active: true, url: location}); }; })(); } });
That's all, links should work after that.

lasantha
- 825
- 8
- 7
-
You've forgotten to close over variable `i` in the closure. Further, `chrome.tabs.create` doesn't require any permissions. – Rob W Jul 18 '13 at 20:05
-
I do not need to close over i, only ln and location are enough. You are right about permission, modified my answer. – lasantha Jul 19 '13 at 04:17
2
Re: is there another way
chrome.tabs.create( { url: "http://www.ajaxian.com"} );

Kai
- 9,038
- 5
- 28
- 28