External tab.


You asked me if it is possible to open external links in a new tab without jquery. Yes, it is possible very positively.

So, these are the codes below,

<script type="text/javascript">for(var a=document.getElementsByTagName("a"),b=0;b<a.length;b++){var c=a[b];c.getAttribute("href")&&c.hostname!==location.hostname&&(c.target="_blank")};</script>

Now, your question is, where to put them in wordpress? Simply put them to your header.php file and save the file.

These codes will go after the <body> and before </body> for HTML. If this is wordpress, then, after the <body <?php body_class(); ?>> or something similar.

So, finally, it will shape like following,

For any HTML / HTML scratch,

<body>

<script type="text/javascript">for(var a=document.getElementsByTagName("a"),b=0;b<a.length;b++){var c=a[b];c.getAttribute("href")&&c.hostname!==location.hostname&&(c.target="_blank")};</script>

</body>

And for wordpress,

<body <?php body_class(); ?>>

<script type="text/javascript">for(var a=document.getElementsByTagName("a"),b=0;b<a.length;b++){var c=a[b];c.getAttribute("href")&&c.hostname!==location.hostname&&(c.target="_blank")};</script>

Yes, I need a cup of tea now and meanwhile, you go add these codes and make it work.

(Please see related posts below)

Leave a Reply

Your email address will not be published. Required fields are marked *