user2139421
user2139421

Reputation: 63

Chrome Extension: How do i open urls in popup.html in the same tab

Google Chrome extension:

i am running mad on a simple thing. Please dont blame me, i am not english origin and i have trouble to read and understand all the extension docs.

I simply want to do the following:

I have lets say 8 different URLs in my popop.html which opens when i click on my icon in the top right browserbar.
(url) example.com
(url) Other Example and etc ...

clicking an url does nothing, target="_blank" opens always a new tab but i want to open them all in the same tab when clicking on one. target="_top" or target="_self" doesnt seem work here.

I have no idea how to program this. I come rather from php and i need a quick solution.

Has anyone a litte ready (easy to understand) code snippet or an Idea how to open my urls in the same tab ? (where to place what) ( i understood the manifest definitions so far, but all this java up and down is too difficult to understand for me at the moment only for this "simple" task).

Thanks in Advance RJ

Upvotes: 6

Views: 12269

Answers (4)

Alaa Sadik
Alaa Sadik

Reputation: 1089

You only need to use chrome.tabs.update({active: true, url: 'yoursite.com'}); instead of chrome.windows.create({url:'yoursite.com'}); and the location will be opened in the same active tab

Upvotes: 2

ivetame
ivetame

Reputation: 233

Although this is an old question, I'll post the working code, as of 2018.

To open anchor links from Chrome extension in the same window, place this code in your popup.js:

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.update({active: true, url: location});
            };
        })();
    }

Upvotes: 1

Uzair Farooq
Uzair Farooq

Reputation: 2427

I believe that chrome does not allow the popups to open an external page by any way. The only solution I know is to place an iframe in your popup.html file with src attribute set to popup2.html and place all your html inside popup2.html. However, consider that all websites do not work well inside iframe.

If you are trying to open url in currently active tab then try following:

Attach following script to your popup.html file:

var hrefs = document.getElementsByTagName("a");

function openLink() {
    var href = this.href;
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        var tab = tabs[0];
        chrome.tabs.update(tab.id, {url: href});
    });
}

for (var i=0,a; a=hrefs[i]; ++i) {
    hrefs[i].addEventListener('click', openLink);
}

You need to add tabs permisson to your manifest file for this to work.

Upvotes: 15

zuc0001
zuc0001

Reputation: 930

This may be a simple fix if I understand you correctly;

For each of the links that you want to open in the same tab, use this HTML:

<a href="LINK" target="_self">Link Name</a>

A simpler way of writing this is to do:

<a href="LINK">Link Name</a>

Hope I was of help :)

Upvotes: -2

Related Questions