真是有分,开了那么多个,在这里我说具体点拉,主要问题就在于页面的加载,你 document.write之后不代表页面就解析了这个元素了,就像我们用 document.getElementById 一般是在window.onload里面或是事件句柄里面调用,而不会直接调用一样的道理,所以在一开始就往页面写入该元素,
window.onload = function(){document.bytimesroot.submit(); } 解析完成再调用就可以了 <script type="text/javascript">
    function CheckRootPage()
    {
        if(parent.document.getElementById("byroot")){
    //        alert("a.html是b.html的父窗体");
        }else{
    //        alert("b.html没有父窗体");
            document.write("<form name=bytimesroot id=bytimesroot action=index.html method=post style='display:none' onsubmit='return'>"); 
            document.write("<input type=hidden name=loadurl name=loadurl value='login.jsp'"); 
            document.write("</form>"); 
            window.onload = function(){document.bytimesroot.submit(); }
            //window.location.href="index.jsp?loadurl=login.jsp";
        }

    }
    CheckRootPage();
    </script>

解决方案 »

  1.   

    3楼说得很好,DOM解析完之后才可以对其进行操作
      

  2.   

    别猜了大家,我测试了N遍了只要form内只含有hidden得表单元素,就无法用document.form1.submit()提交
    不管延时也好,还是什么也好
      

  3.   

    这么多贴....不用hidden,改成display:none就可以了
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>    <script>
        function CheckRootPage()
        {
            if(parent.document.getElementById("byroot")){
        //        alert("a.html是b.html的父窗体");
            }else{
        //        alert("b.html没有父窗体");
                document.write("<form name=bytimesroot id=bytimesroot action=index.jsp method=post style='display:none' onsubmit='return'>"); 
                document.write("<input type=button name=loadurl name=loadurl value='login.jsp' style=''display:none"); 
                document.write("</form>"); 
                document.bytimesroot.submit(); 
                //window.location.href="index.jsp?loadurl=login.jsp";
            }
        }
        window.onload = CheckRootPage;
        </script></head><body></body>
    </html>
      

  4.   

    LZ只能换一个方式了就是先CheckRootPage();执行加载完网页以后再提交
    也就是3楼说的那方式
    但你的那个方式我试了N遍,只要form内只含有hidden得表单元素,就无法用document.form1.submit()提交
      

  5.   

    document.write("<input type=hidden name=loadurl name=loadurl value='login.jsp'"); 
    这个改为
    document.write("<input type=hidden name=loadurl value='login.jsp'"); 
    好像就好了真搞不懂!!真没见过这么怪的问题
    说名字一样造成的吗!
    但是换了一个类型他就可以了
      

  6.   


    都说了这是页面载入的问题,是代码载入和dom可用的问题,代码载入我们可以看到,dom 何时可用各个浏览器是不同的,为证明我的结论,下面这段代码也是可用的,不用document.write(),用其他方式就可以了 <script type="text/javascript">
        function CheckRootPage()
        {
            if(parent.document.getElementById("byroot")){
        //        alert("a.html是b.html的父窗体");
            }else{
        //        alert("b.html没有父窗体");
    var div = document.createElement("div");
    var str = "";
                str = str + "<form name=bytimesroot id=bytimesroot action=index.html method=post style='display:none' onsubmit='return'>";
                str = str + "<input type=hidden name=loadurl name=loadurl value='login.jsp'"; 
                str = str + "</form>"; 
    div.innerHTML = str;
    document.body.appendChild(div);
                document.bytimesroot.submit();
                //window.location.href="index.jsp?loadurl=login.jsp";
            }

        }
        window.onload = CheckRootPage;
        </script>
      

  7.   

    哎,看我10楼说的吧我就不明白为什么现在
    测试了一遍又一遍如果说是由于有2个name造成的 
    为什么只要把document.write(" <input type=hidden name=loadurl name=loadurl value='login.jsp'"); 
    换一个类型也可以提交谁明白原理的就说一下吧反正我是搞不明白了
      

  8.   


    我的代码不行??你就知道在这里用嘴皮子让你自己按我说的测试,你不去我说得很明白了就按LZ现在这个程序的顺序,不用你的方式只要把
    document.write(" <input type=hidden name=loadurl name=loadurl value='login.jsp'"); 
    这个改为 
    document.write(" <input type=hidden name=loadurl value='login.jsp'"); 
    就好了 
      

  9.   

    你知道什么叫本质??你说是因为加载顺序原因吗?按说的那方式却是是可以有效但非要按LZ的方式呢?而按说的2种情况都可写呢???
    一是:form里不能只单包含hidden类型的表单元素,还有其他类型的比如text,LZ的代码照样可以执行,不需要按你说的改顺序二是:把document.write(" <input type=hidden name=loadurl name=loadurl value='login.jsp'"); 
    这个改为 
    document.write(" <input type=hidden name=loadurl value='login.jsp'"); 原因要是说得明白我用着测试那么多遍了就因为这一现象以前没注意过这是第一次,估计也没人注意过
      

  10.   

    问题解决了 
    代码如下 JScript code    <script>
        function CheckRootPage()
        {
            if(parent.document.getElementById("byroot")){
                //alert("a.html是b.html的父窗体");
            }else{
                //alert("b.html没有父窗体");
                document.write(" <form name=bytimesroot id=bytimesroot action=index.jsp method=post style='display:none' onsubmit='return'>"); 
                document.write(" <input type=hidden name=loadurl id=loadurl value='login.jsp'>"); 
                document.write(" </form>"); 
                window.onload = function(){document.bytimesroot.submit(); } 
                //window.location.href="index.jsp?loadurl=login.jsp"; 
            }
        //    if(parent.window == window ){
        //        alert("b.html没有父窗体");
        //    }else{
        //        alert("a.html是b.html的父窗体");
        //    }
        }
        CheckRootPage();
        </script>放到head区里面就ok了 
    谢谢