最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin3PV0评论

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!

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!

Share Improve this question asked Mar 31, 2014 at 3:24 user2335065user2335065 2,5674 gold badges34 silver badges56 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5

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.

Another way to do it to provide the necessary data in onOk function, see this particular mit

You add target attribute to data object in the onOk function in plugins/link/dialogs/link.js

onOk: function() {
            var data = {};

            // Collect data from fields.
            this.mitContent( data );


            // Overwrite, always set target="_blank"
            data.target = {
                dependent: "",
                fullscreen: "",
                height: "",
                left: "",
                location: "",
                menubar: "",
                name: "_blank",
                resizable: "",
                scrollbars: "",
                status: "",
                toolbar: "",
                top: "",
                type: "_blank",
                width: ""
            };
     //more code below
     }

Here is how I updated my links so they open in new tab, without affecting any other links on the page but only those from ckEditor. This is in Angular, but you can use the same idea depending on your platform.

I created a Pipe to transform the link to include a target:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'setlinktarget'
})
export class SetLinkTarget implements PipeTransform {

  transform(value: any): any {
    return value.split('<a ').join('<a target="_new"');
  }

}

I then applied the pipe to my content:

<div [innerHTML]="myRecord.mentsWithHTML | setlinktarget">

This seems much easier than all the other options out there where you have to modify the ckeditor files. Hope this helps someone.

CKEDITOR.on('dialogDefinition', function (ev) { 
   var dialogName = ev.data.name;
   var dialog = ev.data.definition.dialog;
   var dialogDefinition = ev.data.definition;

   if(dialogName == 'link') {
      dialogDefinition.onLoad = function () {
         if(dialogName == 'link'){
             dialogDefinition.getContents('target').get('linkTargetType')['default']='_blank';
         }
         dialog.hidePage( 'target' );                   
    };
}
});

//And configure the below
 config.removeDialogTabs = 'image:advanced;link:advanced;link:upload;';

Go to ckeditor/plugins/link/dialogs/link.js and paste the below code:

/* Here we are latching on an event ... in this case, the dialog open event */

CKEDITOR.on('dialogDefinition', function(ev) {
    try {    
        /* this just gets the name of the dialog */    
        var dialogName = ev.data.name;

        /* this just gets the contents of the opened dialog */
        var dialogDefinition = ev.data.definition;

        /* Make sure that the dialog opened is the link plugin ... otherwise do nothing */
        if(dialogName == 'link') {`enter code here`    
            /* Getting the contents of the Target tab */
            var informationTab = dialogDefinition.getContents('target');

            /* Getting the contents of the dropdown field "Target" so we can set it */
            var targetField = informationTab.get('linkTargetType');

            /* Now that we have the field, we just set the default to _blank
               A good modification would be to check the value of the
               URL field and if the field does not start with "mailto:" or a
               relative path, then set the value to "_blank" */
            targetField['default'] = '_blank';
        }
    } catch(exception) {
         alert('Error ' + ev.message);
    }
});

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far