guardian / scribe

DEPRECATED: A rich text editor framework for the web platform
http://guardian.github.io/scribe/
Apache License 2.0
3.51k stars 245 forks source link

How to add target="_blank" attribute to anchor tag #477

Closed frenzzy closed 8 years ago

frenzzy commented 8 years ago

If you want to keep visitors on your site and also give them the ability to add links, it is necessary to make external links open in a new tabs.

It's not good to use javascript handlers for this links because the guest version of the page can work without javascript at all.

So, need an ability to add target="_blank" attribute for anchor tags.

Any suggestions how to do it?

rrees commented 8 years ago

I think it would probably make sense to create a plugin to manage the creation of the link for you so that the user will provide the URL and target value. This plugin doesn't quite do what you want but might be a place to start.

danburzo commented 8 years ago

Here's one way to retroactively add the target="_blank" attribute to all links, by registering a formatter on export:

scribe.registerHTMLFormatter('export', function(html) {
  var temp = document.createElement('div');
  temp.innerHTML = html;
  var links = temp.querySelectorAll('a');
  for (var i = 0; i < links.length; i++) {
    links[i].setAttribute('target', '_blank');
  }
  return temp.innerHTML;
});