Search code examples
httpurltinymce

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 while adding URL with Insert Link in TinyMCE?


Solution

  • 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'));
                   }
               });
           });
    },