How to make popup.html links open in tab?

假如想象 提交于 2019-12-20 13:14:10

问题


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?


回答1:


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>



回答2:


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.




回答3:


Re: is there another way

chrome.tabs.create( { url: "http://www.ajaxian.com"} );

See http://code.google.com/chrome/extensions/tabs.html



来源:https://stackoverflow.com/questions/4549869/how-to-make-popup-html-links-open-in-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!