Manual:Opening external links in a new window

Find this function in 'YourWikifolder\includes\Skin.php' and change to: function bottomScripts { global $wgJsMimeType; return "\n\t\tif (window.runOnloadHook) runOnloadHook; \n" ."  \n" ." getXterlinks \n"; }

Then create a folder and a file in 'YourWikifolder/javascript_form/getXterlinks.js', and copy and paste this into the file then save it: function getXterlinks // By Thong Nguyen nkthong@yahoo.com // This code just VIRTUALLY add a target="_Blank" to any external link in the wiki document! // NOTE: YOU DON"T Need to modify any of your text document! { var Xterlinks = document.getElementsByTagName('A'); for (var i=0;i<Xterlinks.length;i++) { 	 var eachLink = Xterlinks[i];		var regexp_isYourdomain=/(YourdomainWithoutHttp i.e meta.wikimedia)+/;		var regexp_ishttp=/(http(.)*:\/\/)/; //Check if the link is valid and is external link		if( (eachLink.href != null) && (eachLink.href.match(regexp_isYourdomain) == null) && eachLink.href.match(regexp_ishttp)!=null ) 	{  		eachLink.target ="_blank";//make the target for this external link  	} } } MediaWiki versions Tested in 1.9.3 Or you can follow ANOTHER METHOD below.  This page describes how to change the MediaWiki code so that external links open in a new window.

Keep in mind that some consider it rude to force new windows on your users. Additionally, if you are using Google AdSense (or similar services) on your wiki, these links should open within the same browser ("_self) according to the policies of the Ad Service.

Customized skin and script
This method keeps the markup more compliant by avoiding the  attribute. Use a script to redirect all the links on a page instead. externalLinks = function { 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") != null && 				(anchor.getAttribute("rel").indexOf("external") >= 0 || anchor.getAttribute("rel").indexOf("nofollow") >= 0)			) { anchor.target = "_blank"; }	} }
 * Open the .js page corresponding to the skin. (For the Monobook skin, type   in the Search box.)
 * Create a function that parses all elements for anchor tags with a specific  (relation) attribute, such as   or.

if (window.addEventListener) { window.addEventListener("load", externalLinks, false); } else if (window.attachEvent) { window.attachEvent("onload", externalLinks); }
 * Attach the function as a load event (compatible with Firefox and IE).

Version &lt; 1.5.x (? I think ?)

 * 1) Open "Includes/Skin.php or "Includes/Linker.php"
 * 2) Find "function getExternalLinkAttributes"
 * 3) Replace $r = ($class != ) ? " class='$class'" : " class='external'"; with $r = ($class != ) ? " class='$class' target='_blank' " : " class='external' target='_blank' ";

Note: It doesn't change already present links to open in new windows. It will only make new links you insert open in new windows. Or you can alter all the pages that have external links and then save it to make them work.

Note 2: This doesn't work for the Sidebar on v1.5 --209.150.74.25 19:50, 21 November 2005 (UTC)'''
 * Thank goodness it doesn't! -- &#8465;ilver&#167;&#8465;ide 21:32, 8 January 2006 (UTC)

Version &gt; 1.5.0

 * 1) Open "includes/Linker.php"
 * 2) Find "function getExternalLinkAttributes"
 * 3) After $r = ($class != '') ? " class='$class'" : " class='external'"; insert the following code $r .= " target=\"_blank\"";

This works for all external links and is used by the wiki for the next page generation of each page. The only thing you will have to do if you have a page which still has no "new" links is clear your cache.

Confirmed works in 1.6.x

Confirmed works in 1.8.2

Confirmed works in 1.9.0, but only on newly created external links. You need to make a none-edit and save old pages again.

Dynamically defining an href target
I didn't want to change the behavior in a static way, but sometimes wanted to define an alternate target. To open a link in a new window, one should be able to define target='_new' or target='_blank'. The following changes allow doing so.

php changes
Open "includes/Linker.php"

1. function getExternalLinkAttributes

find the function getExternalLinkAttributes function getExternalLinkAttributes( $link, $text, $class= '' ) { $link = htmlspecialchars( $link ); $r = ($class != '' ) ? " class='$class'" : " class='external'"; $r .= " title=\"{$link}\""; return $r; } change it to       function getExternalLinkAttributes( $link, $text, $class= '' ) { $targ = ""; $pos = strpos( $link, '|' ); if ( $pos !== false ) { $targ = " target='". substr ( $link, $pos + 1 ). "'";                       $link = substr ( $link, 0, $pos ); }               $link = htmlspecialchars( $link ); $r = ($class != '' ) ? " class='$class'" : " class='external'"; $r .= $targ. " title=\"{$link}\""; return $r; }

2. function makeExternalLink

find the function makeExternalLink function makeExternalLink( $url, $text, $escape = true, $linktype = '' ) { $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype ); global $wgNoFollowLinks; if( $wgNoFollowLinks ) { $style .= ' rel="nofollow"'; }               $url = htmlspecialchars( $url ); if( $escape ) { $text = htmlspecialchars( $text ); }               return ''.$text.''; } change it to       function makeExternalLink( $url, $text, $escape = true, $linktype = '' ) { $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype ); global $wgNoFollowLinks; if( $wgNoFollowLinks ) { $style .= ' rel="nofollow"'; }               $pos = strpos( $url, '|' ); if ( $pos !== false ) { $url = substr ( $url, 0, $pos ); }               $url = htmlspecialchars( $url ); if( $escape ) { $text = htmlspecialchars( $text ); }               return ''.$text.''; }

usage
wikipedia main page |_new wikipedia main page in new window |&lt;/nowiki&gt;_new
 * Default behavior hasn't changed. Thus the following link should still open in the same window:
 * To open the same link in a new window, add _new</tt> or _blank</tt> to the link, separated from the link by a pipe:
 * Inside of a table, the pipe symbol must be put into a nowiki-tag:

MediaWiki versions
tested in 1.6.6

tested in 1.8.3

Dynamically adding any href attributes
I did't want to change the behavior in a static way, but wanted to add the possibility of adding attributes to the href tag for external links.

If one wants to open a certain link in a new window, he probably wants to add an attribute target='_new'</tt> to the href tag. The following changes allow doing so.

Attention: If your wiki is open to the public, this might be dangerous since one could also execute javascript code by adding event listener attributes.

php changes
Open "includes/Linker.php"

1. function getExternalLinkAttributes

find the function getExternalLinkAttributes</tt> function getExternalLinkAttributes( $link, $text, $class= '' ) { $link = htmlspecialchars( $link ); $r = ($class != '' ) ? " class='$class'" : " class='external'"; $r .= " title=\"{$link}\""; return $r; } change it to       function getExternalLinkAttributes( $link, $text, $class= '' ) { $atts = ""; $pos = strpos( $link, '|' ); if ( $pos !== false ) { $text = substr ( $link, $pos + 1 ); $link = substr ( $link, 0, $pos ); $pos = strpos ($text, '|' ); while ( $pos !== false ) { $atts .= " ". substr( $text, 0, $pos ); $text = substr ( $text, $pos + 1 ); $pos = strpos ($text, '|' ); }                       $atts .= " ". $text; }               $link = htmlspecialchars( $link ); $r = ($class != '' ) ? " class='$class'" : " class='external'"; $r .= $atts; $r .= " title=\"{$link}\""; return $r; }

2. function makeExternalLink

find the function makeExternalLink</tt> function makeExternalLink( $url, $text, $escape = true, $linktype = '' ) { $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype ); global $wgNoFollowLinks; if( $wgNoFollowLinks ) { $style .= ' rel="nofollow"'; }               $url = htmlspecialchars( $url ); if( $escape ) { $text = htmlspecialchars( $text ); }               return '<a href="'.$url.'"'.$style.'>'.$text.'</a>'; } change it to       function makeExternalLink( $url, $text, $escape = true, $linktype = '' ) { $style = $this->getExternalLinkAttributes( $url, $text, 'external ' . $linktype ); global $wgNoFollowLinks; if( $wgNoFollowLinks ) { $style .= ' rel="nofollow"'; }               $pos = strpos( $url, '|' ); if ( $pos !== false ) { $url = substr ( $url, 0, $pos ); }               $url = htmlspecialchars( $url ); if( $escape ) { $text = htmlspecialchars( $text ); }               return '<a href="'.$url.'"'.$style.'>'.$text.'</a>'; }

usage
wikipedia main page |target='_new' wikipedia main page in new window |target='_new'|style='font-weight:bold;' wikipedia main page in new window |&lt;/nowiki&gt;target='_new'
 * Default behavior hasn't changed. Thus the following link should still open in the same window:
 * To open the same link in a new window, add target='_new'</tt> to the link, separator is a pipe:
 * Multiple attributes can be added, separated by pipes:
 * Inside of a table, the pipe symbol must be put into a nowiki-tag:

MediaWiki versions
tested in 1.6.6