Wednesday, January 12, 2011

How to Make Links & Buttons in Blogger or other Websites Open in a New Window

Finally, here is the answer to all your prayers... kidding but seriously! Fot those who know what I am talking about... this code below will allow all your links and buttons to open in a new window without having to do any extra work (meaning - WITHOUT adding the target="_blank" to each and every link). Yay!! The code below will scan your blog for external pages and add the target="_blank" to each and every one of them.

Before you begin please be sure to save a copy of your current HTML Code to your computer first.

Step 1:  Log into Blogger then click on "Design"  >  "Edit HTML"

Step 2:  Press Ctrl+F and find this code </body>. Right before that code copy and paste the following:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
//<![CDATA[
jQuery('a').each(function() {
    // Let's make external links open in a new window.
    var href = jQuery(this).attr('href');
   
    if (typeof href != 'undefined' && href != "" && (href.indexOf('http://') != -1 || href.indexOf('https://') != -1) && href.indexOf(window.location.hostname) == -1) {
        jQuery(this).attr("target", "_blank");
    }
});
//]]>
</script>


Step 3:  Click "Save Template"

Let me know if it works for you.

View My Portfolio

ShareThis

Blog Hops Everyday Blog Hops Everyday
Related Posts Plugin for WordPress, Blogger...