To the Ministry's main lobby The Ministry Blog
concert setlists

19 January, 2004

Movable Type tip: open links in new windows

For a while, I've been hoping there's a way to automatically insert "target=_blank" into all URLs I quote in the blog.  This attribute of the standard HTML anchor tag causes a linked page to load into a newly created window, leaving the linking page in the original window.  It's a useful way of offering links without encouraging visitors to leave the site.

Frustratingly, I remember seeing a website offering a simple way to do this in Movable Type, but couldn't find it again.  I just have.

The always useful the girlie matters offers a fuller explanation and further advice, but I wanted to repeat the part I actually used (as much for my own reference as anything else!)

The recommended technique uses a specific plugin, but those who, like me, don't have it, a straightforward alternative is to hack the insertLink function in tmpl/cms/edit_entry.tmpl, replacing the 'function insertLink' section with the following:

function insertLink () {
if (!document.selection) return;
var str = document.selection.createRange().text;
if (!str) return;
var my_link = prompt('<MT_TRANS phrase="Enter URL:">', 'http://');
if (my_link != null)
document.selection.createRange().text = '<a href="' + my_link + '" target="_blank">' + str + '</a>';
}

i.e. simply adding the 'target' attribute into the 'document.selection.createRange().text' line.

From now on, URLs inserted by the standard 'URL' button in the 'Create New Entry' section of MT will automatically include the required attribute. Yay!

Whilst searching for this info, I found a blog where the owner and comments regulars criticised 'target=_blank'; someone asked exactly my question, to be told there's no easy way to do it, and if there was, the author "wouldn't help someone shoot themself in the foot" . Evidently I disagree, both personally and as a pro web developer, so for those who dislike links opening in new windows: tough.

Comments

This is great - thank you for the information.

It is, of course, a matter of taste in most cases - some like links to open in new windows....others don't. I can't see the "foot-shooting" in that :-)

I use iFrame (for now) and my MT is published in that frame - so opening up links in that space looks...well...rather bad.

Best wishes,
Baldur

Posted by Baldur M. Rafnsson at February 13, 2004 12:02 PM
.
Site Home Tull Tour History Annotated Passion Play
.
Day in the life... Page design and original graphics © NRT, 2003