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 thi开发者_JS百科s possible?
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>
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.
Re: is there another way
chrome.tabs.create( { url: "http://www.ajaxian.com"} );
See http://code.google.com/chrome/extensions/tabs.html
精彩评论