jqueryexeccommandcleditor

CLEditor - adding target="_blank"


I have been fighting with CLEditor for several hours trying to implement a simple addition - possibility to add target="_blank" option checkbox. The code is this:

 if (url != "") {


   if ($("#blank").is(':checked')) {


     editor.doc.execCommand("insertHTML", false, '<a href="' + url + '" target="_blank">' + selectedText(editor) + '</a>');

   } else {

     execCommand(editor, data.command, url, null, data.button);

   }

   // Reset the text, hide the popup and set focus
   $text.val("http://");
   hidePopups();
   focus(editor);

 }

it works well save one strange glitch - I have to CLICK the editable area after the link WITH target="_blank" is added to be able to save it. I can see the newly added link in DOM BUT - if I won't click the area (anywhere) - I won't be able to save it.

I am adding it with execCommand("insertHTML"..) while links without target="_blank" are being inserted with execCommand(editor, data.command, url, null, data.button); and there is no such problem.

what could cause such problem?

whole thing without PHP part: https://jsfiddle.net/rzj0f334/


Solution

  • weird workaround which cannot be considered as a proper answer but anyway. What I did is this:

    if ($("#blank").is(':checked')) {
    
      editor.doc.execCommand("insertHTML", false, '<a href="' + url + '" target="_blank">' + selectedText(editor) + '</a>');
    
        // copy all the editor's iframe HTML and send it to textarea    (queer solution but that's all I was able to come up with)
        var iframe_content = $('#666').contents().find("body").html();
        $('#input').html(iframe_content);
    
      } else {
    
        execCommand(editor, data.command, url, null, data.button);
    
      }
    

    Definitely not the best way to do that but at least it works. I would GLADLY appreciate a better workaround though.