How to open external links in a new tab/window by default?

Version 3.1 by Gunter Ohrner on 2019/06/26

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. Create a new page, or go to an existing page that won’t be removed
  2. Click Edit and select Objects.
  3. In the Select a Class box, select JavaScriptExtension and click the Add button.
  4. In the object that just appeared, enter the following code into the Code box:
    function fixExternalLinks() {
     var nodes = document.querySelectorAll('.wikiexternallink a');
     for(var i=0; i< nodes.length; i++){
       var link = nodes[i];
       if (link.hasAttribute('target') == false)  {
          link.setAttribute('target', '_blank');
        }
      }
    }
  5. Set Use this extension to On this wiki.

Thanks to laurin1 for this solution.

Tags:
Created by Gunter Ohrner on 2019/06/26
   

Get Connected