External Links in new windows

Freshness Warning
This article is over 9 years old. It's possible that the information you read below isn't current.

Methods for changing how external links work in Movable Type templates have been previously been published by Mark Pilgrim and others, but I recently made some changes to a Movable Type installation that forces all external links to open in a new window and uses CSS to add the external site icon, so I thought I’d share the code here.

Like Mark’s method, you’ll need to use the Macro plugin and insert some macro code into your template or into a template module that is included in your templates.

<MTMacroDefine name="links" ctag="a"><MTIfMatches expr="[MTMacroAttr name='href']" pattern="m|^http:|">    <MTMacroAttr name="target" value="_blank">    <MTMacroAttr name="class" value="offsite"></MTIfMatches><MTIfMatches expr="[MTMacroAttr name='href']" pattern="m|^http://.*.yoursite.com|">    <MTMacroAttr name="target" value="_top">    <MTMacroAttr name="class" value=""></MTIfMatches><MTMacroTag rebuild="1"><MTMacroContent></a></MTMacroDefine>

Find the part that says yoursite.com and change it to your site’s URL.

Then add a bit of CSS to your stylesheet to tell browsers what to do with the “offsite” class.

.offsite {    padding-right: 9px;    background: url(aoutside.gif) no-repeat right;     color: darkgreen;}

This class adds some space after anything that uses the offsite class. The space is there to provide room for the image, which is added using the backround property.

You’ll need to create or pick an image to use as your offsite icon. For this example, I’m using one of the micro icons from WebGragpics. You might also find some of the QBullets icons suitable.

Make sure the padding for the offsite class is big enough to hold whatever icon you decide to use.

Then update your templates to activate the macro. Change your entry body and extended entries tags to look like <MTEntryBody apply_macros="1"> and <MTEntryExtended apply_macros="1">

Trackback from Reflective Reality
September 7, 2003 2:11 AM

External links open new windows

Excerpt: Just installed the regex and macro plugins and a macro from Adam to open links in new windows. Wanna know how? Want an Icon? read on......

Adam Kalsey
September 7, 2003 8:34 PM

All you need is Macros, the regex plugin isn’t needed.

Christian
October 28, 2003 10:08 PM

Hello, I just installed MTMacro. I’m still having a difficult time trying to understand how this plugin works. I have the AXS visitor tracker on in my blog. I want to track “outside” links, and it requires me to prepend “http://mydomainname.com/axs/ax.pl?” before each link in the href attribute. ie. If I wanted to track who clicks a link to nytimes.com, I’d have href=”http://mydomainname.com/axs/ax.pl?http://nytimes.com” target=blank” I want to use this plugin to automatically prepend this to each link outside of my site because handtyping it in is such a pain. But I don’t want to it to prepend to any internal links since they’re all tracked anyway. And also add the target=”blank” attribute to the external links as well. Any help with be greatly appreciated! Thanks!

girlie
November 3, 2003 4:16 PM

Hmmm. So why would a space appear instead of the icon in some links? For example, the link you included to Web Graphics has a blank space where the icon should be. I have a similar situation with a link on my site.

At first I thought it was the hyphen in the domain name (because your link and mine both have one), but I tested by removing the hyphen and still had the same problem.

Ben
February 11, 2007 10:43 AM

i love oll

These are the last 15 comments. Read all 17 comments here.


Your comments:

Text only, no HTML. URLs will automatically be converted to links. Your email address is required, but it will not be displayed on the site.

Name:

Not your company or your SEO link. Comments without a real name will be deleted as spam.

Email: (not displayed)

If you don't feel comfortable giving me your real email address, don't expect me to feel comfortable publishing your comment.

Website (optional):

Follow me on Twitter

Lijit Search

Best Of

  • Comment Spam Manifesto Spammers are hereby put on notice. Your comments are not welcome. If the purpose behind your comment is to advertise yourself, your Web site, or a product that you are affiliated with, that comment is spam and will not be tolerated. We will hit you where it hurts by attacking your source of income.
  • Best of Newly Digital There have been dozens of Newly Digital entries from all over the world. Here are some of the best.
  • Let it go Netscape 4 is six years old.
  • The importance of being good Starbucks is pulling CD burning stations from their stores. That says something interesting about their brand.
  • Google on the desktop Google picks up Picasa, giving them an important foothold on people's PCs.
  • More of the best »

Recently Read

Get More

Subscribe | Archives

9

Recently

invisible Fence (Mar 22)
The New York Times has a paywall now. Sorta. If you don't choose to ignore it.
Black status icon for Chrometa (Mar 17)
Replacing the status icon of Chrometa
Using Google Voice as your voicemail on AT&T (Oct 26)
How I set up my iPhone to use Google Voice as it's voicemail system.
Don Mattingly forced to make coaching change (Sep 17)
New LA Dodgers coach starts to wonder if he knows the rules of baseball at all.
In which Vonage pretends their prices haven't changed (Apr 12)
Translating what Vonage marketing says about their price increase into plain English.
Twitter app competition (Apr 12)
Life as a Twitter app developer is far from over.
Twitter app competition (Apr 12)
Life as a Twitter app developer is far from over.
The rest of the world is not like you (Apr 5)
Normal people are different. Keep that in mind when creating or marketing a product.

Subscribe to this site's feed.

Elsewhere

IMified
Build instant messaging applications. (My company)
SacStarts
The Sacramento technology startup community.
Pinewood Freak
Pinewood Derby tips and tricks

Contact

Adam Kalsey

Mobile: 916.600.2497

Email: adam AT kalsey.com

AIM or Skype: akalsey

Resume

PGP Key

©1999-2012 Adam Kalsey.
Content management by Movable Type.