Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In CKEditor, how to set link open in a new window as default while removing the target tab?

I want links to open on a new window as default. I tried:

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

It doesn't work. But I figured out that if I remove the following line. It works.

config.removeDialogTabs = 'image:advanced;image:Link;link:advanced;link:target';

But then the problem is now there is the target tab that allow user to change the link target.

What I want to keep the editor as simple as possible and don't want to allow users to change the link target. Yet, I want to set default target as target:_blank. Any suggestions? Thanks!

like image 798
user2335065 Avatar asked Dec 14 '25 13:12

user2335065


1 Answers

It seems that if you remove the Target tab, you cannot change the default value to "new window".

However, you can remove all the options in the Target list except "new window", and set it as the default value.

Try the following code:

CKEDITOR.on('dialogDefinition', function(e) {
    if (e.data.name === 'link') {
        var target = e.data.definition.getContents('target');
        var options = target.get('linkTargetType').items;
        for (var i = options.length-1; i >= 0; i--) {
            var label = options[i][0];
            if (!label.match(/new window/i)) {
                options.splice(i, 1);
            }
        }
        var targetField = target.get( 'linkTargetType' );
        targetField['default'] = '_blank';
    }
});

In this case, the Target tab still exists, but there's only one value ("new window") to select, so the users can't change this.

Hope this helps.

like image 128
Zack Xu Avatar answered Dec 16 '25 01:12

Zack Xu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!