From version 4.2
edited by Ryan Delk
on 2019/06/26
To version 5.1
edited by Ryan Delk
on 2019/06/26
Change comment: [document] added jquery option for HTML elements

Summary

Details

FAQCode.FAQClass[0]
answer
... ... @@ -1,9 +1,25 @@
1 -In order to configure all external links to open in new tabs / windows automatically, i.e. to use the link target "##_blank##" from an HTML point of view, you have to add some JavaScript to your XWiki:
1 +In order to configure all external links to open in new tabs/windows automatically, i.e. to use the link target "##_blank##" from an HTML point of view, you have to add some JavaScript to your XWiki.
2 2  
3 +How you are generating your external links will dictate which of the two snippet options you choose.
4 +
5 +**XWiki Syntax**
6 +If you are only generating your external links via [[XWiki Syntax>>https://www.xwiki.org/xwiki/bin/view/Documentation/UserGuide/Features/XWikiSyntax]], you will use **Option 1** on **Step 4**.
7 +
8 +**HTML Links**
9 +If you are writing any external links directly in HTML, either through the ##~{~{html~}~}## macro or in a Velocity ##.vm## file, you have two options:
10 +
11 +*. Use the Javascript code above and manually wrap your links in an element with the ##wikiexternallink## class.
12 +**. Will not work: {{code}}<a href="https://www.xwiki.org">XWiki</a>{{/code}}
13 +**. Will work: {{code}}<span class="wikiexternallink"><a href="https://www.xwiki.org">XWiki</a></span>{{/code}}
14 +*. No modifications need to be made to existing HTML. Instead, use **Option 2** on **Step 4**.
15 +
16 +== Steps ==
17 +
3 3  1. Create a new page, or go to an existing page that won’t be removed.
4 4  1. Click //Edit// and select //Objects//.
5 5  1. In the //Select a Class// box, select //JavaScriptExtension// and click the //Add// button.
6 -1. In the object that just appeared, enter the following code into the //Code// box:
21 +1. In the object that just appeared, enter one of the following snippets into the //Code// box (explained above):
22 +11. If you are using only [[XWiki Syntax>>https://www.xwiki.org/xwiki/bin/view/Documentation/UserGuide/Features/XWikiSyntax]] for your external links:
7 7  ((({{code language="JavaScript"}}
8 8  function fixExternalLinks() {
9 9   var nodes = document.querySelectorAll('.wikiexternallink a');
... ... @@ -16,6 +16,14 @@
16 16  }
17 17  document.addEventListener("DOMContentLoaded", fixExternalLinks);
18 18  {{/code}})))
35 +11. If you have any external links in HTML ##a## elements:
36 +((({{code language="JavaScript"}}
37 +require(['jquery'], function($) {
38 + jQuery(document.links).filter(function() {
39 + return this.hostname != window.location.hostname;
40 + }).attr('target', '_blank');
41 +});
42 +{{/code}})))
19 19  1. Set //Use this extension// to //On this wiki//.
20 20  
21 21  Thanks to [[pdwalker>>https://forum.xwiki.org/u/pdwalker]] and [[laurin1>>https://forum.xwiki.org/u/laurin1]] for this [[solution>>https://forum.xwiki.org/t/solved-external-links-set-default-target-as--blank/2583/21]].

Get Connected