[solved] External links, set default target as "_blank"

If you’re talking about CKEditor’s WYSIWYG, see http://extensions.xwiki.org/xwiki/bin/view/Extension/CKEditor%20Integration/#HAdministrationSection

Note that AFAIK this has always been like this since we introduced CKEditor. Maybe you’re remembering some old feature with the GWT WYSIWYG editor?

Thomas, you’re “twice” right: I was used to the old WYSIWYG editor and it’s easy to change the new CKEditor behaviour.

Many thanks for your quick and relevant answer.

Vincent maybe ? :slight_smile:

@tmortagne and @vmassol you’re are both right … most of the time :rofl:.

lol :slight_smile:

I did it! I created my first skin extension using this JS code!

Well done! :slight_smile:

Well, geez, I never tested this in Internet Explorer - works fine in Chrome, but not at all in IE and we use IE a lot still here at work.

      function fixExternalLinks() {
        document.querySelectorAll('.wikiexternallink a').forEach(function(link) { 
          if (link.hasAttribute('target') == false)  {
            link.setAttribute('target', '_blank');
          }
        })
      }
      document.addEventListener("DOMContentLoaded", fixExternalLinks);

Object doesn’t support property or method ‘forEach’

Sorry laurin1, I didn’t test with IE as I consider the browser too old and out of date to support.

Can you tell me what version of IE you are using and I’ll see if I can find a workaround for you.

1 Like

IE 11, which supports almost all of the modern web development standards. I know people don’t like it, but in an Active Directory domain, we still have more control over it in an enterprise environment and so that is still our standard internally. I’ve reviewed the code (I’m a web developer myself) and I actually don’t see why it would not work.,

document.querySelectorAll is supported in IE (since version 8), except with pseudo classes, which this is not using. Given some time, I can write the JS necessary to fix this, so don’t spend too much time on it, I’m sure I can hammer it out eventually.

As an aside, I’m using chrome in an AD environment, and I can set all the useful defaults in the group policies.

1 Like

Looks like IE is not implementing forEach on the the nodelist querySelectorAll provides. But a standard for loop should work.

Something like

nodes=…querySelectorAll()
for(var i=0; i< nodes.length; i++){
// do sth. with nodes [i]
}

Obviously this is only pseudo code and I did not try it, put typing this little code on a mobile is already killing me :wink:

1 Like

:slight_smile: Well, it’s your thread we are hijacking, but yes, we use those for the limited usage of Chrome we allow, but they are not as granular (and some just don’t work as well) for Chrome - we are healthcare and keep things locked down pretty tight here. Still, we are evaluating what our next option will be, but we will possibly move to Edge and not Chrome - the main reason is that, while the us IT people love Chrome, it’s a resource hog (and I’ve got the stats to prove it), and we have some older hardware out there. Still, we are considering Chrome as an option, depending on the how the next iteration or two of of Edge goes…

1 Like

I considered that as well and just needed time to test it - thanks for the leg up!

Works in IE and Chrome:

  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');
      }
    }
  }
  document.addEventListener("DOMContentLoaded", fixExternalLinks);
1 Like

Thanks.

I would have gotten to it eventually, but this just save me a whole lot of time.

And since you mentioned it, I’ll reexamine edge to see how well it behaves in our environment. :wink:

1 Like

I just tried to reproduce the solution in the current version of xwiki. Unfortunately, (parts of ?) the code is just shown at the top of the wiki and does not solve the problem anymore

browser is firefox (newest) and chrome (newest)

You’ll have to be more specific about what you actually did so we can figure out where it is going wrong for you.

The above solution continues to work for me.

Specifically, I implemented the following under Administration, Look & Feel, Presentation, HTTP Meta Info:
Screenshot 2022-09-18 at 01.11.29

<script>
  function fixExternalLinks() {
    document.querySelectorAll('.wikiexternallink a').forEach(function(link) { 
      if (link.hasAttribute('target') == false)  {
        link.setAttribute('target', '_blank');
      }
    })
  }
  document.addEventListener("DOMContentLoaded", fixExternalLinks);
</script>

Basically, the javascript code code executes once the page is loaded, looks for any external links and if they do not already have a target specified, gives it a _blank target.

1 Like

I’m embarrassed to say that I never got back to this since the javascript injection was working so well.

bam, sorry. I don’t know what was happening but I tried it again and now it is working like a charm. Thanks you!