0

I was expreimenting to build a page editor. One issue just drove me crazy in firefox.

The page code is below:

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

It just check whether it is approprate to set "designMode" in Mozilla way or IE way. When the page loads, a "Mozilla" pops up; click the iframe area, and the focus is on the iframe and I can input with keyboard.

This looks fine, but when I comment out the line “alert("mozilla");”, it doesnt work. The "designMode" is "Off" as FireBug shows.

This is so wired. Why a alert can affect the DOM and javascript? BTW, my Firefox is 3.0.6.

Morgan Cheng
  • 73,950
  • 66
  • 171
  • 230

1 Answers1

2

Because the alert gives the iframe time to load. You should set designMode to "on" only after the iframe document has loaded:

iframe.onload = function() {
    doc.designMode = "on";
};
Ionuț G. Stan
  • 176,118
  • 18
  • 189
  • 202
  • Thanks. the fix makes it work! The "designMode can be on after iframe loaded" only applies to Firefox, right? It seems IE doesn't have such limitation. – Morgan Cheng Apr 12 '09 at 06:16
  • I don't quite remember this thing. Last time I was playing with designMode I applied "onload" in both browsers, but yeah, I first observed it in FF. Anyway, I do remember having problems in IE with dynamically generated iframes. So be careful with those too. – Ionuț G. Stan Apr 12 '09 at 06:19