Open all links in new tab

If you have a website with articles containing links, and if you want to make those links open in a new tab, normally you need to edit those posts and placed target=”_blank” tag right? Well what if your site have lots of posts, say 1000+ its difficult to open each article and edit it. Well there is a solution for this, just by putting a code to the end of the page ie., just before </body> tag of your website.

To do this, Simple open your theme’s footer file and Just above to “</body>” tag paste any one of the below codes as per your requirements.

If you want all links (including your website links) to open in new tab, then use this code:

<script type=”text/javascript”>
$(document).ready(function(){
$(“body a[href^=’http://’]”).attr(“target”,”_blank”);
});
</script>

If you want external links to open in new tab, then use this code:

<script type=”text/javascript”>
$(document).ready(function() {
$(“a[href^=http]”).each(function(){
if(this.href.indexOf(location.hostname) == -1) {
$(this).attr({
target: “_blank”,
title: “Opens in a new window”
});
}
})
});
</script>

Save the theme and now open your website. Y0u can now see all URL(s) opening in new tab.




July 17, 2012
/
Previous Post Next Post

Tharun recommends you to read these fantastic articles