Search code examples
firefoxinternet-explorer-8hyperlinkniceditexeccommand

NicEdit link creation doesn't work in IE 8 and FireFox if text wasn't selected


I have a problem with nicEdit link creation tool in IE and Firefox.

In general, I think the problem is related to the execCommand in IE and FireFox. It seems document doesn't get updated after execCommand executes.

This is an example of my problem with nicEdit create link command.

if(!this.ln) {
        var tmp = 'javascript:nicTemp();';
        this.ne.nicCommand("createlink",tmp);
        this.ln = this.findElm('A','href',tmp);
        // set the link text to the title or the url if there is no text selected
        alert(this.ln);
        if (this.ln.innerHTML == tmp) {
            this.ln.innerHTML = this.inputs['title'].value || url;
        };
    }

The code above is called when no text is selected, Chrome returns 'javascript:nicTemp()' for the alert(this.ln), while IE 8 and Firefox return 'undefined', so the next line after the alert encounters an error in IE and Firefox.

it seems findElem can't find the newly created link by nicCommand which in turn calls execCommand

I had similar problems when I try to find and modify tags created with execCommand, it seems the dom isn't updated to include them.

Am I right? How can I solve this problem? how can I force the document to be updated ....

please help


Solution

  • my trick for nicEdit, in the situation when no text is selected, is to paste the title given via the Add Link form into the document and select it, then the rest code works as it works when a text is selected.

    I used the function pasteHtmlAtCaret described in the following link to paste the title

    Insert html at caret in a contenteditable div

    this.removePane();
    var url = this.inputs.href.value;
    var selected = getSelected();
    var B= 'javascript:nicTemp()';
    if (selected == '')
    {
        var B = url;
        pasteHtmlAtCaret(this.inputs['title'].value || url,true);
    }                   
    if(!this.ln){
        this.inputs.title.value;this.ne.nicCommand("createlink",B);
        this.ln=this.findElm("A","href",B)
    }
    

    the getSelected is also a simple function as below

    function getSelected()
    {
        if (document.selection)
            return document.selection.createRange().text;
        else
            return window.getSelection();
    }