为什么弹出警报会影响“designMode”?

时间:2009-04-12 06:07:04

标签: javascript firefox alert designmode

我很想建立一个页面编辑器。一个问题让我在firefox中疯狂。

页面代码如下:

<body>
<iframe WIDTH=200 HEIGHT=200 id="myEditor"></iframe>
<script>

    function getIFrameDocument(sID){
        // if contentDocument exists, W3C compliant (Mozilla)
        if (document.getElementById(sID).contentDocument){
            alert("mozilla"); // comment out this line and it doesn't work
            return document.getElementById(sID).contentDocument;
        } else {
            // IE
            alert("IE");
            //return document.getElementById(sID);
            return document.frames[sID].document;
        }
    }

    getIFrameDocument("myEditor").designMode = "On";

</script>

</body>

它只是检查是否适合以Mozilla方式或IE方式设置“designMode”。当页面加载时,弹出“Mozilla”;点击iframe区域,焦点在iframe上,我可以用键盘输入。

这看起来不错,但是当我注释掉 “alert(”mozilla“); ”这一行时,它不起作用。 FireBug显示“designMode”为“Off”。

这是如此有线。为什么警报会影响DOM和javascript? 顺便说一句,我的Firefox是3.0.6。

1 个答案:

答案 0 :(得分:2)

因为警报会给iframe加载时间。只有在iframe文档加载后才应将designMode设置为“on”:

iframe.onload = function() {
    doc.designMode = "on";
};
相关问题