ckeditor default target link=" _blank" not work properly

Viewed 3815

My ckeditor version is 4.4.7

I want to change the default target to every link of the text that I add to ckeditor and I found this code

CKEDITOR.on('dialogDefinition', function(ev) {

  try {

    var dialogName = ev.data.name;
    var dialogDefinition = ev.data.definition;

    if (dialogName == 'link') {

      var informationTab = dialogDefinition.getContents('target');

      var targetField = informationTab.get('linkTargetType');

      targetField['default'] = '_blank';

    }

  } catch (exception) {

    alert('Error ' + ev.message);

  }

});

CKEDITOR.on('instanceReady', function(ev) {
  var editor = ev.editor,
    dataProcessor = editor.dataProcessor,
    htmlFilter = dataProcessor && dataProcessor.htmlFilter;
  htmlFilter.addRules({
    a: function(element) {
      element.attributes['target'] = "_blank";
    }
  });
});

I added this code to link.js file of ckeditor folder and it's working but not correctly

I mean if I copy the text that have a link from word to editor,it doesn't add target_blank to a href automatically

but I have to click 'edit link' on it and see the default target already on _blank

enter image description here

then I click ok and save then it works.

but I want it to auto set target="_blank" on every link that I copy from word.

anyone can help?

thanks.

3 Answers

Where did you put your code?

I changed

type : 'select',
id : 'linkTargetType',
label : commonLang.target,
'default' : 'notSet',

in _source\plugins\link\dialogs\link.js to

type : 'select',
id : 'linkTargetType',
label : commonLang.target,
'default' : '_blank',

and this works fine.

Editing inside plugin files is not an ideal solution.

The best solution would be to add this to your js file

CKEDITOR.on( 'dialogDefinition', function( ev ) {
   var dialogName = ev.data.name;
   var dialogDefinition = ev.data.definition;
      if ( dialogName == 'link' ) {
          var targetTab = dialogDefinition.getContents( 'target' );
          var targetField = targetTab.get( 'linkTargetType' );
          targetField[ 'default' ] = '_blank';
      }
});

I added this code to link.js file of ckeditor folder and it's working but not correctly

You use this code directly on HTML page were you initialize the editor and not in link.js file:

var editor = CKEDITOR.replace( 'editor1', { });
CKEDITOR.on('dialogDefinition', function(ev) {
...
Related