Sandy
Sandy

Reputation: 14125

How to set target attribute in tinyMCE

I have set a link to some text using tinymce.execCommand("CreateLink", False, theLink). The problem is that I want to set "Target=_blank".

How do I set a Target attribute after the above?

Thanks.

Upvotes: 5

Views: 11433

Answers (5)

Arth
Arth

Reputation: 13110

If you prefer giving the user the option, but making target="_blank" the default.

This appears to be correct for v4.4.3:

tinymce.init({
    plugins: "link",
    default_link_target: "_blank"
});

You can also still specify the options:

tinymce.init({
    plugins: "link",
    target_list: [
        {title: 'None', value: ''},
        {title: 'Same page', value: '_self'},
        {title: 'New page', value: '_blank'}
    ],
    default_link_target: "_blank"
});

It also appears support for selected: true has been removed.

Upvotes: 5

behz4d
behz4d

Reputation: 1849

Or you could simply:

tinymce.init({
    plugins: "link",
    target_list: [
        // remove `none` and `_self` by commenting them like below
        // {title: 'None', value: ''},
        // {title: 'Same page', value: '_self'},
        {title: 'New page', value: '_blank'}
    ]
});

http://www.tinymce.com/wiki.php/Configuration:target_list

Upvotes: 0

elshnkhll
elshnkhll

Reputation: 2223

Making 'New page' an only option did work for me (see: http://www.tinymce.com/wiki.php/Configuration:target_list) in version 4.1.0

target_list: [ {title: 'New page', value: '_blank', selected: true} ]

Upvotes: 4

Ryan
Ryan

Reputation: 921

Are you trying to set the target attribute to _blank on all user-provided links in TinyMCE? If so, set this property in your tinymce.init configuration:

extended_valid_elements: 'a[href|target=_blank]'

This works for me on TinyMCE 4.0.2.

With this configuration, all links in the editor will have target set to _blank when the editor saves. (Additionally, all attributes other than href and target will be removed since I excluded them in the configuration; add whatever valid attributes/constraints that you wish to allow.)

see: http://www.tinymce.com/wiki.php/configuration:extended_valid_elements

If these are user-provided links, then you may also want to set rel=nofollow for all links they provide.

Upvotes: 9

Thariama
Thariama

Reputation: 50832

You need to identify the correct html element in your tinymce editor. You can use this code to replace/set the target attribute of your link

$(ed.getBody()).find('a [href="'+theLink+'"]').attr('Target', '_blank');

Be aware that you might have the same link in the editor already. In this case all such links' target attributes become '_blank'.

Upvotes: 2

Related Questions