Manual:Opening external links in a new window

Find this function in 'YourWikifolder\includes\Skin.php' and change to:

Then create a folder and a file in 'YourWikifolder/javascript_form/getXterlinks.js', and copy and paste this into the file then save it: 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.
 * 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.


 * 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   with
 * 4) If you wish, you can do the same for the function getInterwikiLinkAttributes. This will make interwiki links open in a new window.

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 v 1.5.

Version &gt; 1.5.0

 * 1) Open includes/Linker.php
 * 2) Find "function getExternalLinkAttributes"
 * 3) After   insert the following code

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.

Confirmed works in 1.11 ( new pages )

Note: In the version 1.9.0; the line of code has "\" and not "'"

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 change it to ''2. function makeExternalLink''

find the function makeExternalLink change it to

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:


 * NOTE: MediaWiki 1.10.1 this nor the one below works inside Tables. It prints |_new on every link

MediaWiki versions
Tested in 1.6.6

Tested in 1.8.3

Tested in 1.10.0

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.

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</tt>

1. function getExternalLinkAttributes

find the function getExternalLinkAttributes</tt> change it to ''2. function makeExternalLink''

find the function makeExternalLink</tt> change it to

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:


 * NOTE: MediaWiki 1.10.1 this does not work inside Tables. It prints |target='_new' on every link

MediaWiki versions
Tested in 1.6.6