Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

ExternalLinks2

Summary: Add and icon to external links and make them automatically open in a new window
Version:
Prerequisites:
Status:
Maintainer: Pierre Racine
Categories: links

Questions answered by this recipe

  • How can I make all external links open in a new window?
  • How can I add an icon to external links?

Description

The following markup adds an icon for every external links and make them to open in a new window. Add the markup definition to config.php or another local customisation file. You also need to create an icon images ext.gif and put it in a pub/icons/ directory.

This recipe is different from ExternalLinks in that it will not add the icon for internal links inadvertendly written with http://...

Markup('[[newwin|','<[[|',"/(?>\\[\\[\\s*(http:\/\/[^|\\]]*)\\s*\\|\\s*)(.*?)\\s*\\]\\]($SuffixPattern)/e", 
"MakeLinkNewWin(\$pagename, PSS('$1'), PSS('$2'), PSS('$3'),'$4')");

Markup('[[newwin','<[[', "/(?>\\[\\[\\s*(http:\/\/.*?)\\s*\\]\\])($SuffixPattern)/e", 
"MakeLinkNewWin(\$pagename, PSS('$1'), NULL, '$2')");

function MakeLinkNewWin($pagename, $target, $text, $suffix)
{
    global $UrlLinkFmt, $_SERVER, $PubDirUrl;

    $host = 'http://'.$_SERVER['HTTP_HOST'];

    // We do not make the link external if it is internal 
    if (substr_count($target, $host) == 0 && substr_count($text, "Attach:") == 0)
        $fmt = "%newwin%".$UrlLinkFmt."&nbsp;<img src=".$PubDirUrl."/icons/ext.gif>";

    if (is_null($text))
        return Keep(MakeLink($pagename, PSS($target), NULL, $suffix, $fmt), 'L');
    else 
        return Keep(MakeLink($pagename, PSS($target), PSS($text), $suffix, $fmt), 'L');
}

Get your icon from here: www.stylegala.com/features/bulletmadness/

Rename it to be named like this:
ext.gif
and place it in the icons directory.

Notes

I could not make this to work with bare hyperlink like http://www.site.com/file.pdf

Release Notes

If the recipe has multiple releases, then release notes can be placed here. Note that it's often easier for people to work with "release dates" instead of "version numbers".

Comments

Thanks a lot for this useful recipe! I have intalled it, and it works fine for all URLs starting with http://. However, it does not recognize external URLs that are implemented with the InterMap feature, so its coverage is somewhat limited. Perhaps it would be possible to include intermap consideration to this recipe to make it even more useful? --Henning March 10, 2008, at 12:32 PM

See Also

Contributors

Pierre Racine

Edit - History - Print - Recent Changes - Search
Page last modified on March 10, 2008, at 12:33 PM