简体   繁体   English

tinymce 3.x advlink插件-设置链接以默认在新窗口/选项卡中打开

[英]tinymce 3.x advlink plugin - set links to open in new window/tab by default

Using the advlink plugin, the default value for "target" is "_self" (ie links open in same window/tab). 使用advlink插件时,“目标”的默认值为“ _self”(即,链接在同一窗口/选项卡中打开)。 How can I make it so that links open in a new window/tab by default? 如何使链接默认在新窗口/选项卡中打开?

We need to make the following changes in the file advlink.js located in [yourTinymcePluginsDirectory]/advlink/js/advlink.js . 我们需要在文件中进行以下更改advlink.js位于[yourTinymcePluginsDirectory]/advlink/js/advlink.js

Locate the following part: 找到以下部分:

    function getTargetListHTML(elm_id, target_form_element) {

        var targets = tinyMCEPopup.getParam('theme_advanced_link_targets', '').split(';');
        var html = '';

        html += '<select id="' + elm_id + '" name="' + elm_id + '" onchange="this.form.' + target_form_element + '.value=';
        html += 'this.options[this.selectedIndex].value;">';
        html += '<option value="_self">' + tinyMCEPopup.getLang('advlink_dlg.target_same') + '</option>';
        html += '<option value="_blank">' + tinyMCEPopup.getLang('advlink_dlg.target_blank') + ' (_blank)</option>';
        html += '<option value="_parent">' + tinyMCEPopup.getLang('advlink_dlg.target_parent') + ' (_parent)</option>';
        html += '<option value="_top">' + tinyMCEPopup.getLang('advlink_dlg.target_top') + ' (_top)</option>';

We need to change the order of the options. 我们需要更改选项的顺序。 So just put the "_blank" option on top: 因此,只需将“ _blank”选项放在顶部:

        html += '<option value="_blank">' + tinyMCEPopup.getLang('advlink_dlg.target_blank') + ' (_blank)</option>';
        html += '<option value="_self">' + tinyMCEPopup.getLang('advlink_dlg.target_same') + '</option>';
        html += '<option value="_parent">' + tinyMCEPopup.getLang('advlink_dlg.target_parent') + ' (_parent)</option>';
        html += '<option value="_top">' + tinyMCEPopup.getLang('advlink_dlg.target_top') + ' (_top)</option>';

And voila, that was it. 瞧,就是这样。 Was it? 是吗 No! 没有!

Although this will indeed work, if we try changing the target of a link back to _self , although the HTML will indeed be updated correctly, the dropdown will falsely show the _blank option as selected. 尽管这确实可行,但是如果我们尝试将链接的目标更改回_self ,尽管HTML确实会正确更新,但下拉列表将错误地显示_blank选项。 This happens because when selecting the _self option the target Tinymce attribute is not updated to _self but rather to "" - so an empty string. 发生这种情况的原因是,选择_self选项时, target Tinymce属性不会更新为_self而是更新为“”-则为空字符串。 Therefore, when selecting _self the script will try to look for an attribute named "", will of course find none, therefore it will select the first option by default. 因此,在选择_self ,脚本将尝试查找名为“”的属性,当然不会找到任何属性,因此默认情况下它将选择第一个选项。 While this works when the first option is _self , it will not work when _self is not the first option. 虽然当第一个选项是_self时此方法有效,但如果_self不是第一个选项,则该方法无效。

To solve this problem, we need to tell the script that when the target Tinymce attribute is set to an empty string, it should look for the attribute named _self instead. 要解决此问题,我们需要告诉脚本,当目标Tinymce属性设置为空字符串时,它应该查找名为_self的属性。 That way it will be able to locate it even if it's not the first option. 这样,即使不是第一个选项,它也能够找到它。

To achieve this we also need to make one more change to the script's behavior. 为此,我们还需要对脚本的行为进行另一处更改。 Locate the following line: 找到以下行:

selectByValue(formObj, 'targetlist', inst.dom.getAttrib(elm, 'target'), true);

And replace it with: 并替换为:

    var target_value = inst.dom.getAttrib(elm, 'target');
    if (inst.dom.getAttrib(elm, 'target') == ""){
        target_value = "_self";
    }//end if

    selectByValue(formObj, 'targetlist', target_value, true);

And now everything should work perfectly, with _blank being the default target option. 现在,一切都应该正常工作, _blank是默认的目标选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM