if you do not like me answering it, just ignore the followingwhen you call
alert(test.document.body); 
the first time, the page for the iframe is not created yet, that is why you got "null", but by displaying the alert message box, the execution of the current window is on hold, the browser will have time to create the iframe, so when you call
alert(test.document.body); 
the second time, the iframe is created, and you saw "[object]". In these situations, you should always do it in the onload event of the main window, something like this:<SCRIPT LANGUAGE="JavaScript">
<!--
function window.onload()
{
alert(test.document.body)
alert(test.document.body)
}
//-->
</SCRIPT>

解决方案 »

  1.   

    yeah,你是对的。它需要一点时间。
    我也没错。我是在定义了它以后调用的。即使我把它放到</html>的后面,结果还是一样(某些版本的IE不允许):
    <html>
    <body>
    <iframe id=test>
    </iframe>
    </body>
    </html>
    <SCRIPT>
    alert(test.document.body)
    alert(test.document.body)
    </SCRIPT>其实只要我们把IE对html的解析看成是一个动态生成页面的过程,这个问题又回到了一个老问题上:对页面做的改动需要等页面刷新后才能生效。
    <html>
    <body>
    <iframe id=test>
    </iframe>
    <SCRIPT>
    setTimeout("alert(test.document.body)",0)
    </SCRIPT>
    </body>
    </html>唉,重新发现了一个旧的bug,不过这次是以一种新的方式。
    本来以为document.recalc()是用来解决这个问题的,竟然不行:<html>
    <body>
    <iframe id=test>
    </iframe>
    <SCRIPT>
    document.recalc();
    alert(test.document.body)
    </SCRIPT>
    </body>
    </html>那这个莫名其妙的recalc()方法到底有什么用呢?
      

  2.   

    like I said, call the function in onload event
    <body onload="whatever()">
      

  3.   

    recalc Method  Internet Development Index 
    ----------------------------------------------------------------
    Recalculates all dynamic properties in the current document.Syntax    document.recalc( [bForceAll])Parameters    bForceAll   Optional. Boolean that specifies one
                    of the following values:
                    false Default. Recalculates only those expressions
                      that have changed since the last recalculation. 
                    true Recalculates all expressions in the document. 
     
    Return Value    No return value.ResImplicit dependencies, internal property changes, and related properties can cause some expressions not to recalculate, even though the properties being referenced might have changed. For example, resizing the main window changes the clientWidth property. Expressions that reference clientWidth might not be recalculated, because the change might not be recognized.Implicit dependencies refer to properties that can be altered by changes in other properties. For example, the height of a div implicitly depends on the innerHTML of the div. However, if an expression references the height, a change in the innerHTML, which might alter the height, does not cause a recalculation of the expression on a subsequent call to recalc.Related properties can access or manipulate data or behaviors through one or more other properties. For example, pixelLeft and posLeft can set or retrieve the left position of the element. However, if an expression that references pixelLeft and posLeft is altered, the expression might not be recalculated on subsequent calls to recalc.Related properties that can cause this behavior include the following: clientHeight, clientLeft, clientTop, clientWidth, height, left, offsetHeight, offsetLeft, offsetTop, offsetWidth, pixelHeight, pixelLeft, pixelTop, pixelWidth, posHeight, posLeft, posTop, posWidth, and top.To force recalculations of all expressions, you should refer to the same property name or manually call recalc(true).ExampleThe following example uses the recalc method in HTML and script to help create a timer. If the calls to recalc are commented out, the timer does not work correctly.Show Example<HTML>
    <HEAD> 
    <TITLE>Recalc Example</TITLE>
    <STYLE>
    BUTTON {font-size:14;width:150}
    </STYLE>
    <SCRIPT>
     
    var timerID = null;
    var seconds = 0;
     
    //
    // This function is called when the page loads. 
    // It sets up a couple of expressions.
    //
     
    function init()
    {
      A.style.setExpression("width","seconds*10");
      B.setExpression("innerText","seconds.toString()");
    }
     
    //
    // This function gets calls once a second and updates the seconds
    // variable. Notice that without recalc, the expressions aren't
    // updated until the mouse is moved.
    //
     
    function timer()
    {
      seconds++;
      document.recalc();
    }
     
    //
    // starts the timer
    //
     
    function starttimer()
    {
      if (timerID == null) 
      {
        timerID = setInterval("timer()", 1000);
        startButton.disabled = true;
        stopButton.disabled = false;
      }
    }
     
    //
    // stops the timer
    //
     
    function stoptimer()
    {
      if (timerID != null)
      {
        clearInterval(timerID);
        timerID = null;
        startButton.disabled = false;
        stopButton.disabled = true;
      }
    }
     
    //
    //  resets the timer
    //
     
    function resettimer()
    {
      seconds = 0;
    }
     
    </SCRIPT>
    </HEAD>
    <BODY onload="init()">
     
    <DIV id=A style="background-color:lightblue" ></DIV>
    <DIV id=B style="color:hotpink;font-weight:bold"></DIV>
     
    <BR>
    <BUTTON id="startButton" onclick="starttimer()">Start the Timer</BUTTON></BR>
    <BUTTON id="stopButton" DISABLED="true" onclick="stoptimer()">Stop the Timer</BUTTON><BR>
    <BUTTON id="resetButton" onclick="resettimer()">Reset the Timer</BUTTON><BR>
     
    <P style="width:320;color:peru;background-color:wheat">
    This example illustrates the use of document.recalc().  If the calls
    to recalc are omitted in this example, expressions will not be updated 
    until the mouse is moved.
    </P>
     
    </BODY>
    </HTML>
    Show Me
    Standards InformationThere is no public standard that applies to this method. 
      

  4.   

    emu你前面说的那个问题确实如此。mozilla就按照你的想法运行,不会显示null。经过了4年的开发,mozilla 1.0已经于6月5日发布,也许你应该试一试它。它跨平台,而且有最好的DOM支持。
      

  5.   

    怎么搞的,都是夜猫,还都还掉洋文?like I said?不是 like I've said 吗?
      

  6.   

    emu,你前面说的问题确实如此。mozilla就按照你的想法运作,因此它不会显示null。经过了四年的开发,mozilla 1.0终于发布,也许你应该试一试它。
      

  7.   

    but the problem is, most of your users will not use Mozilla, not in a year, not in 2 years... so if you develop your pages based on Mozilla, they will break miserably in IE
      

  8.   

    u'r wrong, saucer. because ms ie 6 also support web standard well.
      

  9.   

    ic, then why switch to Mozilla? :-)
      

  10.   

    。。
    干什么?
    当然是IE好啦。。
    IE能在IFRAME加载完前运行脚本,
    而MLZ只能在IFRAME后才运行脚本。
    根本上就是谁包含谁的问题。
      

  11.   

    emu (ston) 不是又通宵了吧?
      

  12.   

    今晚肯定又没的睡了。关键是现在多数的网页都是针对IE写的,在其他的浏览器上表现就会有差异。比如IE的事件是冒泡的,NS的却是递归的,有些时候,你只能为他们各写一个版本,要是再考虑其他的浏览器,天啦!
      

  13.   

    不是没有在一个帖子中解决两个问题的习惯
    是不想解决以前重复的问题
    而且相信你的实力<iframe id=test></iframe>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    timer=setInterval("try{test.document.body.bgColor='red';clearInterval(timer)}catch(e){}",100);
    //-->
    </SCRIPT>