Jake
Jake

Reputation: 83

How do I open a link in the current tab for my chrome extension?

Here is my popup.html code:

<a href="http://google.com">Google</a>

But when I click the Google link nothing happens. How do I make the link take me to google.com in the current tab?

Upvotes: 7

Views: 3612

Answers (1)

ᔕᖺᘎᕊ
ᔕᖺᘎᕊ

Reputation: 3011

You need to use chrome.tabs.query to find the selected tab and update it.

So I would do something like:

popup.html:

<div id='clickme'>Google</div>
<script src = 'popupjs.js'></script>

popupjs.js:

document.getElementById('clickme').onclick = function() {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        var tab = tabs[0];
        chrome.tabs.update(tab.id, {url: 'http://www.google.com'});
    });
};

This uses chrome.tabs.query and chrome.tabs.update to find the current tab id and update it with the url http://www.google.com.

Note: You will need the tabs permission in your manifest file!

You could then use some CSS to make the div look like a real link, or just change the cursor when hovering over it.

Upvotes: 5

Related Questions