1

I would like to have target="_blank" selected as the default for new links in any Wygwam field. I've tried a variety of things using the CKEditor config file, but I haven't been able to get this working properly.

Anyone out there done this with any success?

Thanks.

2 Answers 2

1

Here's a bit of jQuery that adds target="_blank" to all external link (links to another domain):

$(document).ready(function() {
  $("a[@href^=http]").each(
    function(){
      if(this.href.indexOf(location.hostname) == -1) {
        $(this).attr('target', '_blank');
      }
    }
  )
});

Would that solve your problem?

4
  • not a great solution as all links could potentially have the full url and not the uri. Jul 28, 2013 at 18:14
  • Read the js: it checks if the link has a domain-name in it equal to the current location.hostname :)
    – Steven
    Jul 29, 2013 at 13:13
  • On a sidenote: $("a[@href^=http]") only selects links with hrefs that start with http :).
    – Steven
    Jul 29, 2013 at 13:14
  • I missed the location.hostname. All of my hrefs start with http:// is all I meant. Aug 1, 2013 at 14:33
0

Not quite the solution you're after but if you were to wrap each instance where you output the content of the Wygwam field with a specific class in your templates. You could then add some javascript to target any links within it and force them to open in a new window.

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.