beakr
beakr

Reputation: 5857

Linking to the web in Google Chrome extensions?

I was sad to see that in my work-in-progress Google chrome extension, when the valid link to my blog didn't actually open a page in the browser to view the page. My popup.html file contained the standard <a href="http://sample.com">Sample</a>. I was wondering if there's a script or something needed to insert a valid, loading link to a webpage intoyour Google Chrome extension popup.

Upvotes: 0

Views: 117

Answers (1)

hamczu
hamczu

Reputation: 1774

There are two common ways:

<a href="http://sample.com" target="_blank">Sample</a>

or (if you want to have more control):

<a href="http://sample.com">Sample</a>

<script>
window.onload = function(){
    var links = document.getElementsByTagName('a');
    for(var i = 0; i < links.length; i++){
        links[i].addEventListener('click', function(){
            chrome.tabs.create({'url': this.getAttribute('href')}, function(tab) {
                // tab opened
            });
        });
    }
}
</script>

Upvotes: 4

Related Questions