Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ckeditor default target link=" _blank" not work properly

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.

like image 925
PTN Avatar asked Mar 19 '26 15:03

PTN


1 Answers

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';
      }
});
like image 134
Prasanth M P Avatar answered Mar 22 '26 03:03

Prasanth M P