guardian / scribe

DEPRECATED: A rich text editor framework for the web platform

Home Page:http://guardian.github.io/scribe/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

frenzzy opened this issue · comments

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?

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.

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;
});