Nilot
Nilot

Reputation: 55

Opening a specific URL in a new tab while click on any link within a webpage

I want to open a specific URL in a new tab while I click on any link (i.e. <a href="some_url">LINK</a>) within my blog using JavaScript. I run a Blogger blog and I want to open an another new blog URL when I click on any link in the old blog. I don't want to use any class or ID to target links.

I hope this is possible. Please help me.

Upvotes: 0

Views: 211

Answers (2)

If you only want open the link in a new tab, you only have to add this to the html:

target="_blank"

Your final result should be this:

<a href="some_url" target="_blank" >LINK</a>

Upvotes: 0

geevee
geevee

Reputation: 5451

using pure Javascript:

<script type="text/javascript">
(function(){
    var els = document.getElementsByTagName('a');
    for (i=0;i<els.length;i++) {
        els[i].addEventListener('click', function(){
            window.open('http://somepage.com');
        });
    }
})();
</script>

using jQuery:

<script type="text/javascript">
    $(function(){
        $('a').click(function(){
            window.open('http://somepage.com');
        });
    });
</script>

see example snippet here: http://jsfiddle.net/rbu2mg6e/

hope that helps.

Upvotes: 1

Related Questions