开发者

WMD editor freezes Internet Explorer 7 for 3 seconds on load

开发者 https://www.devze.com 2022-12-25 02:58 出处:网络
I am using the WMD editor\'s original code (not the Stack Overflow version) since I need multiple of them on the same page and Stack Overflow\'s version makes heavy use of element IDs internally since

I am using the WMD editor's original code (not the Stack Overflow version) since I need multiple of them on the same page and Stack Overflow'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 fine in Firefox 3.5, etc. However, when I run it in Internet Explorer 8 (in Internet Explorer 7 compatibility mode), it freezes the whole browser for about 3 sec. before a new instance shows up. I tried profiling it with Internet Explorer's development 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 wmdInst开发者_运维百科anceId = 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.


Maybe the freeze in load time is due to IE 7 rendering the JavaScript. Firefox may be faster at rendering and so it makes IE appear to freeze.

0

精彩评论

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