WMD 编辑器在加载时使 Internet Explorer 7 冻结 3 秒

发布于 2024-08-27 06:54:39 字数 3172 浏览 8 评论 0原文

我正在使用 WMD 编辑器的原始代码(不是 Stack Overflow 版本),因为我需要在同一页面上使用多个编辑器,而 Stack Overflow 的版本在内部大量使用元素 ID,因为它们不会有多个编辑器实例每页。

该代码在 Firefox 3.5 等中运行良好。但是,当我在 Internet Explorer 8(Internet Explorer 7 兼容模式)中运行它时,它会冻结整个浏览器大约 3 秒。在新实例出现之前。我尝试使用 Internet Explorer 的开发工具对其进行分析,似乎代码缩小版本的第 520 行上的 getWidth() 函数一直占用着时间。然而,当我尝试对返回进行硬编码时(因为它总是返回相同的内容),瓶颈转移到了 getHeight() 函数。

我附上了我用来将其转换为 jQuery 插件的代码。

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
    };
}();

任何帮助将不胜感激。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夕色琉璃 2024-09-03 06:54:39

也许加载时间的冻结是由于 IE 7 渲染 JavaScript 造成的。 Firefox 的渲染速度可能更快,因此它使 IE 显得冻结。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文