Home

Replacing All External Links Open in a New Tab

Replacing All External Links Open in a New Tab

Replacing All External Links Open in a New Tab
Replacing All External Links Open in a New Tab

Replacing All External Links Open in a New Tab


If manually to make the Link open in a new tab on a blog you usually add target = '_ blank' in each link. In the code that I will share, you don't need to add target = '_ blank' to each link, so it will save you a little time in modifying the blog.


As in the title of this post, the way it works is quite simple ie every external link on the blog if we click it will open directly in a new browser tab. With this trick, we can also help in reducing the bounce rate of our blog.

Well, here ThugsofBD will give tips on how to Replace All External Links Open in a New Tab.

Replacing All External Links Open in a New Tab

Log in to Blogger > Click the Themes menu > Click the Edit HTML button and add the code below right before the code  </body>


<script type='text/javascript'>
//<![CDATA[
// New Tab
function externalLinku(){for(var e=document.getElementsByTagName("a"),t=0;t<e.length;t++){var n=e[t];n.getAttribute("href")&&n.hostname!==location.hostname&&(n.target="_blank")}}externalLinku();
//]]>
</script>


Or this code that has been given rel = 'nofollow noopener'


<script type='text/javascript'>
//<![CDATA[
// New Tab
function externalLinku(){for(var e=document.getElementsByTagName("a"),t=0;t<e.length;t++){var n=e[t];n.getAttribute("href")&&n.hostname!==location.hostname&&(n.rel="nofollow noopener")&&n.hostname!==location.hostname&&(n.target="_blank")}}externalLinku();
//]]>
</script>




If so, click the Save theme button and see the results on your blog.



This code has two checkers to check links that start with http: // and https: //. Usually all bloggers have a hostname starting with http: // and without secure protocol or https: //. Therefore this code is added to be able to open external links from either http: // or https: //.



Newer post Older post

RELATED ARTICLES