« Moderate Comments, Trackbacks or Both (with TypePad's New & Improved Feedback Interface) | Main | How To: Put Google Search in Your New Menu Bar »

John T. Unger

A Link Widget I Really Like, And A Hack to Use it In Advanced Templates

John T. Unger May 24, 2006

Also announced in today's post on Everything TypePad, is a pretty cool new Widget which you can get here.

This widget will install an invisible snippet of JavaScript on your site that will scan the page for links that point outside of your domain, and force those links to open in a new browser window. Any links that point inside your domain -- archive links, category links, individual entry links, etc. -- will open in the current browser window.

Note that because this Widget just installs JavaScript on your blog, there will be nothing for you to preview during the Widget installation process.

I would so love to be able to set links preferences on the whole blog like that! But since I use Advanced Templates, I can't use the Widget here.

No problem… I just added it to a basic blog, viewed the source code, found the JavaScript and pasted it into one of the TypeLists here. Bingo! It seems to be working fine on the index page, although it may not be functioning right on archived pages… I'll have to study it a bit. Since the script is invisible, you can add it to any Typelist you like. I put mine in the Colophon with the rest of the JavaScripts, just so I'd know where to find it.

Here's the script for those of you with advanced template blogs that also want to try out this feature. Just cut and paste into a TypeList:

Note: Both the Widget and the hack may violate the Terms Of Service of some blog advertising services. See this comment below for details. Thanks to Rich Owings for pointing this out. I wouldn't have thought of it and I'll bet that it didn't occur to the designers of the Widget either!

<script type="text/javascript">
(function() {
var x = function() {
var links = document.getElementsByTagName
? document.getElementsByTagName( "a" )
: document.links;
for( var i = 0; i < links.length; i++ ) {
var link = links[ i ];
if( link.host && link.host != window.location.host )
link.target = "_blank";
}
}

var o = window.onload;
window.onload = function( e ) {
x( e );
if( o )
return o( e );
}
})();

</script>

From Google AdSense Help Center: Can I open the ads in a new window?

We understand the importance of maintaining the quality of your site and your site's user experience. However, at this time we do not have an option to open Google ads in a new browser window. Please do not modify the AdSense ad code or alter the result of clicking on an ad on your website, as this is against our program policies.

More Like This: Advanced Templates , Basic Templates , Hacks for Navigation , Hacks for Typelists , Widgets

Comments

Seshu says:

I gave this a whirl, but no dice. I tucked this script first within a TypeList item. That didn't work. Then I created a new item within an pre-existing TypeList and that too didn't do the trick. When I click on external links on my main page, it doesn't trigger a new window for the link.

Seshu

john t unger says:

Seshu,

I'll admit, I'm kinda winging it on this one… It seems to be working on some (but not all) of my pages. Not sure what the hang-up is.

But I will find out, and update the post. Probably in the next few days. I'll see if I can get any assistance from TypePad.

Rich Owings says:

Isn't this a violation of some advertising services TOS?

john t unger says:

For instance, the link in this post to the widget opens in a new window if clicked from the index page. But on this page, if you click *the same dang link* it opens in the same window. Puzzling.

One thought is that because the script is one of the last page elements to load, you need to let the page finish loading before clicking. But that still doesn't explain why the script only seems to work on the index page.

Suggestions anyone? Bud? JavaScript isn't really my strong suit.

john t unger says:

Rich,

Unclear what you mean in your comment… Is opening links in a new window proscribed by some advertising programs?

I don't do much with ads on my blogs, so I'm not familiar with all the TOS agreements. I guess I'd advise people to read the terms of any programs they enter into and comply. If that means skipping a hack, well, there you are.

I was always pretty annoyed by anything that opened a new window until I set Firefox to open links in new tabs… *That* I don't mind. And since it would appear that most readers here are using Firefox or RSS I figure it's probably not going to annoy them too much. If I had a ton of comments to the contrary ("I hate it. Switch it back!") I'd probably remove the code. But that's exactly what I like about being able to set the links on a blog-wide basis with one bit of code.

john t unger says:

From Google AdSense Help Center:Can I open the ads in a new window?

We understand the importance of maintaining the quality of your site and your site's user experience. However, at this time we do not have an option to open Google ads in a new browser window. Please do not modify the AdSense ad code or alter the result of clicking on an ad on your website, as this is against our program policies.

Thanks, Rich.

So, those of you using AdSense should avoid this hack as well as the Widget it's based on.

Glad someone pointed this out. I'd not have thought of it.

jeff says:

Let me know when you get this figured out! Not working on my blog... Thanks!

john t unger says:

Jeff,

Since it's an in-house design from TypePad (rather than a third-party widget) I've submitted a help ticket to find out where I went wrong with the JS. I'll update the post when I get an answer and will also email you.

john t unger says:

Update: TypePad tech support sent me the same code as above and suggests that it should be copied from a plain text environment.

I've responded to see if there is any additional code that should be added to archive templates to make it run. It seems to me that if it can run in a TypeList, it shouldn't need additional template code but maybe I'm missing something.

Frank Steele says:

I installed the new widget on one of my weblogs before I read the suggestion that it might violate the AdSense TOS. Before removing it, I thought I should check it out, and for some reason (Google's JS code?), the AdSense links come up in place of my page, but all other links on the page open in a new window.

That's the official TypePad widget; don't know whether that's the case with the hack.

john t unger says:

Frank,

wow, that's *very* interesting. I've been talking with Jeff about his concerns that inserting adsense code into each post would violate googles TOS also (only three ad blocks are allowed per page). He found that the server only sent three sets of ads no matter how many times the code was placed in the page.

It kind of looks like google is managing their content in a way that prevents breaking the rules… which *may* mean that both hacks fall within acceptable guidelines, since the behaviors that would violate the TOS aren't actually happening in either case.

I will try to chase down some answers on this from google if I can find someone there to talk to.

Mike says:

Great tool. The only problem I've have is that it seems to open ALL links in a new window even the internal ones. Any suggestions?

Also how would you go about specifying the new window size?

Thanks

Mike

Christopher Keys says:

Thank you!!!

The comments to this entry are closed.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d8341c4fdf53ef00d834c2d11069e2

Listed below are links to weblogs that reference A Link Widget I Really Like, And A Hack to Use it In Advanced Templates:

» Testing 1, 2, 3 from BAMCAT.com
Thanks to a post on TypePad Hacks I just added some JavaScript code to BAMCAT and Criticize This! that will open all external links in a new window (Google Ads excluded). Seems to be working fine on our end, but [Read More]

typepad hacks is a typepad featured weblog typepad hacks is listed on Alltop social media

Subscribe

Email RSS Comments  
Subscribe to TypePad Hacks with email Read TypePad Hacks posts via RSS Join the conversation at TypePad Hacks via RSS (comments feed) subscriber count

Search

Socialize

Twitter Logo
    follow typepadhacks on twitter

    Read and reply to the 100 most recent comments at the TypePad Hacks Community Page


    Grazr

    Colophon

    Powered by TypePad
    Member since 03/2005

    TypePad Status

    Creative Commons License