PTN
PTN

Reputation: 427

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.

Upvotes: 3

Views: 5378

Answers (3)

Prasanth M P
Prasanth M P

Reputation: 858

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

Upvotes: 3

j.swiderski
j.swiderski

Reputation: 2445

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) {
...

Upvotes: 0

Ravi Chauhan
Ravi Chauhan

Reputation: 1458

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.

Upvotes: 2

Related Questions