开发者

NicEdit Error in Chrome

I'm using the NicEdit WYSIWYG plugin on my site.

It's come to my attention that when NicEdit is instantiated in Chrome, the following Javascript error is generated:

Uncaught TypeError: Object  has no method 'createRange'

This doesn't stop the plugin from working, but I would like to prevent this if possible. Here is the offending method:

getRng : function() {
        var s = this.getSel();
        if(!s) { return null; }
        return (s.rangeCount > 0) ? s.getRangeAt(0) : s.createRange();
}

NicEdit seems to be pretty much dead as a project, which is why I am asking this question开发者_开发问答 here instead of over at the NicEdit forums. I am hoping that someone knows of a 'quickfix' to this problem. In all other respects NicEdit works well for me, so I am reluctant to change over to a different WYISWYG plugin just yet...

Thanks (in advance) for your help.


The problem is that the implementation of the selection object for Webkit does not define a createRange( ) method. That method seems to be specific to Internet Explorer. For Webkit and Gecko DOM implementations, the createRange( ) method is defined on the document object. With this knowledge, the fix for getRng( ) becomes:

getRng : function() {
    var s = this.getSel();
    var rng;        

    if(!s) { return null; } 
    if (s.rangeCount > 0) {
        rng = s.getRangeAt(0);
    } else if ( typeof s.createRange === 'undefined' ) {
        rng = document.createRange();
    } else {
        rng = s.createRange(); 
    }       
    return rng;
 },

I encountered this as I was evaluating a number of rich text editors for an upcoming project and had to create a sample page with nicEdit.


The version at https://github.com/danishkhan/NicEdit contains this and other bugfixes.

This particular fix: https://github.com/danishkhan/NicEdit/commit/efa6a1e8867b745b841157e919a0055cb626d2c4


Same code, written in nicEdit current design:

getRng : function() {
    var s = this.getSel();
    if(!s) { return null; }
    return (s.rangeCount > 0) ? s.getRangeAt(0) : (typeof s.createRange == 'undefined') ? document.createRange() : s.createRange();
},
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜