New Windows in Textpattern

Published 30 July 04 by Justin French, 12 comments

The JavaScript

Create a new file called scripts.js, and save it to /scripts/ in the root of your Textpattern website (usually /public_html/), with the following contents.

function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
            anchor.target = "_blank";
        }
    }
}
window.onload = externalLinks;

Open up the Textpattern interface, and edit your pages (Presentation > Pages) so that they include a link to this script in the <head> of each page template…

<script type='text/javascript' src='/scripts/scripts.js'></script>

Upon loading the document, the script will look for any <a> tags with both a href attribute and a rel attribute with the value external.

Changing the txp:linklist Tag

In theory, all links added via Content > Links are to external websites. So all we need to do is add a rel="external" attribute to the tag in /publish/taghandlers.php.

Change line 160 to…

$link = '<a href="'.$url.'" rel="external">'.$linkname.'</a>';

Change line 162 to…

'" title="'.$description.'" rel="external">'.$linkname.'</a>';

Changing Textile’s Linking

In links generated by Textile, we need to look at the URL and decide if it’s an external or internal link. Whilst it’s not a perfect rule, I’ve decide that any URLs beginning with “http:” or “https:” are not relative links, so they must be external.

We need to add a line to /lib/classTextile.php after line 553:

$atts .= (preg_match("/^(http|https):/",$url)) ? " rel='external'" : '';

This adds rel='external' to any external links generated by Textile.

Wrap-up

This will give you external links into new windows for any <txp:linklist /> links on your site, as well as any external links generated by Textile. Everything is perfectly valid and semantically rich XHTML Strict.

The downside is that the new Textile links will only apply to articles you add or edit after modifying classTextile.php. So if you want to “upgrade” your old articles, you’ll need to re-open and save them all re-apply Textile, or perhaps create a batch processing script to do it all for you!

Options

What is this?

portrait of Justin

This is the online home of Justin French, a designer & web application developer located in Melbourne, Australia. I like finding ways to make things work better. I like clarifying and simplifying. I like to understand how you understand things.

» read more

Subscribe to my feed

Follow me on Twitter

@justinfrench

More Notebook Articles

Show more notebook articles

Search