Manual:Opening external links in a new window

How to open a link in a new window in Strict HTML 4
Re: Opening a link in a new window... > Therefore how, in Strict HTML 4.01, do you target a link to a new > window? The short answer is: you don't. The long answer is [...]

Think long and hard before deciding on opening any links in new windows. Users of most browsers are able to open new windows where they deem useful, but forcing links to open in the same window is difficult.


 * On Spawned Windows
 * Guideline 10. Use interim solutions. Checkpoint 10.1
 * Until user agents allow users to turn off spawned windows, do not cause pop-ups or other windows to appear and do not change the current window without informing the user.
 * The Top Ten New Mistakes of Web Design (2. Opening New Browser Windows)
 * Opening up new browser windows is like a vacuum cleaner sales person who starts a visit by emptying an ash tray on the customer's carpet.

How to make external links open 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 (don't forget to set the right value for "YourdomainWithoutHttp"): 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='_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 |_blank Wikipedia main page in new window |&lt;/nowiki&gt;_blank
 * 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 _blank 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 |_blank on every link. In MediaWiki 1.12.0 the method above works fine, but it does print the |_blank on every link inside tables. In MediaWiki 1.9.2, it works everywhere except in the sidebar, where it prints the |_blank and opens the link in the same window.

MediaWiki versions
Tested in 1.6.6

Tested in 1.8.3

Tested in 1.9.2

Tested in 1.10.0

Tested in 1.11.2

Tested in 1.12.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='_blank'</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='_blank' Wikipedia main page in new window |target='_blank'|style='font-weight:bold;' Wikipedia main page in new window |&lt;/nowiki&gt;target='_blank'
 * 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='_blank'</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='_blank' on every link

| | |target='_blank'
 * Inside of a table, use two pipes (which is normally needed to define row styles) at the beginning of the line:


 * NOTE: Tested on MediaWiki 1.13

MediaWiki versions
Tested in 1.6.6

The problem stated above is still occurring in version 1.11.0 of mediaWiki. Using &lt;nowiki&gt;|&lt;/nowiki&gt;target='_blank' results in the page displaying |target='_blank' instead of the text intended. For example, entering:


 * Yahoo Messenger: |&lt;/nowiki&gt;target='_blank' asa.rand

displays:


 * target='_blank' asa.rand

External links in new window except for links to same server
I applied the suggest change to function getExternalLinkAttributes</tt> in Linker.php</tt>. All external links open in a new window. OK but when you go to page that doesn't exist, there is link to edit it, which is an external link.

So, I patched function getExternalLinkAttributes</tt> to open in new window ONLY if the link points to another server. If it's on same server, the link is normal and opens in the same window.

Before changes

After changes

Works with MediaWiki 1.12.

External links in new window except for links to the same server (for v1.13)
Update to above patch, to make it compatible with MediaWiki version 1.13. Cause function getExternalLinkAttributes doesn't receive link parameter anymore, I make changes in function makeExternalLink in Linker.php.

Before changes

After changes

Works with MediaWiki 1.13.

External links in new window except for links to the same server (for v1.14)
See Manual:$wgExternalLinkTarget.