开发者

TinyMCE: How to prepend 'http://' to URL if it's not there

Is there way to prepend 'http://' to URL if it's not there 开发者_如何学Gowhile adding URL with Insert Link in TinyMCE?


For that you would need to copy the tinymce Insert Link plugin, rename it, add the necessary code to it (the "http"-Adding) and use it as your own plugin.

EDIT: Ok, here is an example (using jQuery):

// You might need to change the event and/or tha handling 
// but this should give you a guess at what needs to be done
setup : function(ed)
{
       ed.onClick.add(function(ed, evt)
       {
           $(ed.getBody()).find('a').each(function(index, element){
               if (!$(this).attr('href').search('http')){
                    $(this).attr('href', 'http://' + $(this).attr('href'));
               }
           });
       });
},


I was facing this problem as well with version 4.x. I discovered that the link plugin supports an option that prompts the user to add the protocol. But unfortunately it's not mentioned in the documentation, it's only documented in the changelog so I would imagine this is widely unknown.

$('textarea').tinymce({
    ...
    link_assume_external_targets: true
    ...
});

TinyMCE: How to prepend 'http://' to URL if it's not there


I have achieved this by prepopulating the field value with 'http://' in the tinymce insertLink.aspx file.

<ui:PropertyPanel runat="server" Text="Url">
   <input type="hidden" id="localUrl" name="localUrl" onchange="" />
   <input id="href" name="href" type="text" style="width: 220px;" value="http://" onchange="document.getElementById('localUrl').value = ''; 
selectByValue(this.form,'linklisthref',this.value);" />
</ui:PropertyPanel>

value="http://"


in link.js, find "if (!f.href.value)"

add an "else" clause

    else {
        var href = f.href.value;
        if (href.indexOf('http') == -1) {
            href = 'http://' + href;
            f.href.value = href;
        }
    }

** remember that you did that in case you update your tinymce component!


i just saw this and it might help, version 5:

link_assume_external_targets: [option];

you can use the option "https" or "http" and it should not prompt

https://www.tiny.cloud/docs/plugins/opensource/link/#link_assume_external_targets

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜