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

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


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.


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});            };        })();    }


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