WMD editor freezes IE7 for 3 seconds on load

Posted by dhruvbird on Stack Overflow See other posts from Stack Overflow or by dhruvbird
Published on 2010-03-29T04:10:04Z Indexed on 2010/03/29 4:13 UTC
Read the original article Hit count: 601

Filed under:
|
|

Hello all, I am using the WMD editor's original code(not the stackoverflow version) since I need multiple of 'em on the same page and stackoverflow's version makes heavy use of element IDs internally since they aren't going to be having more than one editor instance per page.

The code runs fin in FF 3.5, etc.. However, when I run it in IE8 (in IE7 compatibility mode), it freezes the whole browser for about 3 sec. before a new instance shows up. I tried profiling it with IE's dev. tools, and it seems that the getWidth() function on line 520 of the minified version of the code is taking up all the time. However, when I tried to hard-code the return (since it was always returning the same thing), the bottleneck shifted to the getHeight() function.

I am attaching the code I am using to convert it to a jQuery plugin.

jQuery.fn.wmd = function(params) {
function createInstance(container, params) {
    /* Make sure WMD has finished loading */
    if (!Attacklab || !Attacklab.wmd) {
        alert("WMD hasn't finished loading!");
        return;
    }
    var defaultParams = {
        width    : "600px", 
        rows     : 6,
        autogrow : false, 
        preview  : false, 
        previewDivClassName: "wmd-preview-div"
    };
    if (typeof(params) == "undefined") {
        var params = defaultParams;
    }
    else {
        var params = jQuery.extend({}, defaultParams, params);
    }

    /* Build the DOM elements */
    var textarea = document.createElement("textarea");
    textarea.style.width = params.width;
    textarea.rows = params.rows;
    jQuery(container).append(textarea);

    var previewDiv = document.createElement("div");
    if (params.preview) {
        jQuery(previewDiv).addClass(params.previewDivClassName);
        jQuery(container).append(previewDiv);
    }
/* Build the preview manager */
var panes = {input:textarea, preview:previewDiv, output:null};
var previewManager = new Attacklab.wmd.previewManager(panes);

    /* Build the editor and tell it to refresh the preview after commands */
    var editor = new Attacklab.wmd.editor(textarea,previewManager.refresh);

    /* Save everything so we can destroy it all later */
    var wmdInstance = {ta:textarea, div:previewDiv, ed:editor, pm:previewManager};
    var wmdInstanceId = $(container).attr('postID');
    wmdInstanceProcs.add(wmdInstanceId, wmdInstance);

    if (params.autogrow) {
        // $(textarea).autogrow();
    }
};

if (jQuery(this).html().length > 0) {
    var wmdInstanceId = jQuery(this).attr('postID');
    var inst = wmdInstanceProcs.get(wmdInstanceId);
    jQuery(inst.ta).show();
}
else {
    createInstance(this, params);
}
}

jQuery.fn.unwmd = function(params) {
var wmdInstanceId = $(this).attr('postID');
var inst = wmdInstanceProcs.get(wmdInstanceId);
if (inst != null) {
    jQuery(inst.ta).hide();
}
}

wmdInstanceProcs = function() {
var wmdInstances = { };
var getProc = function(wmdInstanceId) {
    var inst = wmdInstances[wmdInstanceId];
    if (typeof(inst) != "undefined") {
        return inst;
    }
    else {
        return null;
    }
};
var addProc = function(wmdInstanceId, wmdInstance) {
    wmdInstances[wmdInstanceId] = wmdInstance;
};
return {
    add: addProc, 
    get: getProc
};
}();

Any help would be much appreciated.

© Stack Overflow or respective owner

Related posts about wmd-editor

Related posts about wmd