Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make popup.html links open in tab?

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?

like image 485
user556396 Avatar asked Dec 28 '10 21:12

user556396


2 Answers

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>
like image 139
serg Avatar answered Jan 01 '23 10:01

serg


I had the same issue and this was my approach:

  1. Create the popup.html with link (and the links are not working when clicked as Chrome block them).
  2. Create popup.js and link it in the page: <script src="popup.js" ></script>
  3. 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.

like image 28
lasantha Avatar answered Jan 01 '23 11:01

lasantha