Installation is a cinch, simply copy and paste the following code into your blog template just below the <head> tag:
<script src='http://code.jquery.com/jquery-latest.js'/>In Closing
<script type='text/javascript'>
//<![CDATA[
function jqFixLink() {
var strLink = $(this).attr("href");
if(strLink != undefined) {
if(strLink.search(document.domain) == -1 && strLink.indexOf("http:") != -1) {
$(this).attr("target", "_blank");
}
}
}
function jqFixExtLinks() {
$("#Blog1 a").each(jqFixLink);
}
$("#Blog1").ready(jqFixExtLinks);
//]]>
</script>
That's all it takes, now as soon as your Blog widget loads all of its HTML this script runs. Only links pointing to another domain are affected, everything to other parts of your blog works normally. However, I should point out this introduces a small dependency by linking directly to the JQuery library. If their site doesn't respond for any reason, the script will fail, but in my experience that is an uncommon event. Give it a try and enjoy!
6 comments :
If i use this code then Adsense links also open in new tab or window?
It works fine. Many thanks buddy! Its really helpful post.
If JQuery fails, will all my external links not be able to load, or will they just open in the same window of my blog?
@Kristen, if for some reason the JQuery site is inaccessible the links will open as if the script never ran. So, in the same window.
First of all i want say thank you for this script. It really work, sometime its not work. I have 2 questions regarding this script.
1).This script some extend slow down my blog loading time, so is it OK if i added this code before the "/body". Because if i added this code before the "/body" some extend page loading time increase.
2).Why sometimes this code not work properly?
Loading slow down or failure is most likely a timeout loading the JQuery library from their servers. Alternatively, you may download JQuery and save it to your own hosting location such as Google code or locate a copy elsewhere.
Once JQuery is loaded, the script should execute quite quickly even with hundreds of links on each page. Few operations are performed per link.
Post a Comment