2

I have a website where by default I would like all external links to have the "open in a new tab" option to be selected by default. I'm wondering if this is possible either through craft or redactor? I came across this but think it may be overkill so I'm wondering if its possible without a plugin.

1
  • Guessing the easiest solution would be to add target="_blank" to all link elements with javascript. But the UX of doing this is awful. There is hardly any cases where target="_blank" is a good idea.
    – KnutSv
    Commented Aug 28, 2016 at 22:25

3 Answers 3

2

In Craft 3, you can set "linkNewtab": true and "linkTarget": "_blank" in your redactor config. This should default so the "Open in new tab" is automatically checked when creating links in a redactor field.

https://imperavi.com/redactor/docs/settings/link/

1
  • (Quibbling, but this of course only affects content in Redactor fields. It does not result in all external links opening in a new windows - which, again, may or may not be desirable, but the JS solution below is more general, if you do want this...) Commented Jun 24, 2020 at 1:45
6

Merits of doing this aside, I agree that JS is probably the easiest way to go here.

This code adds target="_blank" to all external links.

// Make sure any offsite links (non js/mailtos) open in a new window
// from https://css-tricks.com/snippets/jquery/open-external-links-in-new-window/ - comment at end.
function openAllExternalLinksInANewWindow() {
    $(document).on('mousedown', 'a:not([href*="mailto:"],[href*="javascript"])', function(e) {
        var isInternalLink = new RegExp('/' + window.location.host + '/');
        if (!isInternalLink.test(this.href)) {
            $(this).attr('target', '_blank');
        }
    });
}
5
  • The only case in which I'd use target="_blank" is if opening a link would destroy some content the user has made on your site. In all other cases it's a very bad practise.
    – KnutSv
    Commented Aug 29, 2016 at 5:52
  • Thanks Jeremy. I should've explained better in my question that yes I just wanted to add it just to external links. I'm surprised to see @KnutSv opposed to adding target="_blank". Could someone maybe point me to a discussion or study on the pros and cons of using it? Maybe I'm just naive on this one. Commented Aug 30, 2016 at 8:42
  • 1
    That's almost a holy war on the web :) Persoanlly, I don't mind it for external links as we have a large, complex site, and people don't like losing their place on it...but I can also understand others views that it's always bad practise... I'll edit my answer above to reflect the updated question. Commented Aug 30, 2016 at 23:46
  • 1
    @patrick.altair People like to control for themselves whether a link should open in a new tab. I always hate when a site forces me to open external links in new tabs, just imagine if Google forced you to open all external links in new tabs. It's the same with your site, maybe they're just passing through? straightupcraft.com/craft-plugins does this when you go to the plugin page and it's always a pain in the ass to have to go back and close the original tab. Also less experienced users and blind users don't notice the new tab, just that they can no longer use the back button
    – KnutSv
    Commented Aug 31, 2016 at 7:34
  • Ah ok, I can see both sides now. In my case its longform articles that link to products talked about in the articles. I wanted to open this is a new tab so readers don't lose their place in the article basically but this has made me reconsider. Thanks for the discussion guys! Commented Aug 31, 2016 at 11:48
0

I've been trying to gather ideas from different places and still not sure how can I do it on Craft 2 having Redactor v2.13 or Redactor II. I looked into the redactor.js file (it's a huge file about 10k lines) and didn't find linkTarget: '_blank' to linkTarget: '_blank' May be it's because of difference in versions? There are similar mentions though such as

Link Target settings Redactor 2.13

linkNewTab settings

I'd try to explore more and see how this goes. Just incase if someone has already gone through this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.