<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='http://www.baidu.com' style='display:none' onsubmit='return'>");
document.write("<input type=button name=loadurl name=loadurl value='login.jsp'");
document.write("</form>");
document.bytimesroot.submit();
//window.location.href="index.jsp?loadurl=login.jsp";
}
}
window.onload = CheckRootPage;
</script>
document.bytimesroot导致找不到这个对象
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>
没有解析完dom就调用document.bytimesroo可能会出错的
<script>
function CheckRootPage()
{
if(parent.document.getElementById("byroot")){
//alert("a.html是b.html的父窗体");
}else{
//alert("b.html没有父窗体");
document.write("<form id='bytimesroot' name='bytimesroot' action='index.jsp' method='post' style='display:none'>");
document.write("<input type='hidden' name='loadurl' name='loadurl' value='login.jsp'/>");
document.write("</form>");
setTimeout(inputform(),500);
//document.bytimesroot.submit();
//window.location.href="index.jsp?loadurl=login.jsp";
}
// if(parent.window == window ){
// alert("b.html没有父窗体");
// }else{
// alert("a.html是b.html的父窗体");
// }
}
function inputform(){
var forms=document.getElementById("bytimesroot");
alert(forms);
forms.submit();
}
window.onload = CheckRootPage;
</script>
这里为什么取不到表单对象?????
function CheckRootPage()
{
if(parent.document.getElementById("byroot")){
//alert("a.html是b.html的父窗体");
}else{
//alert("b.html没有父窗体");
document.write("<form name='bytimesroot1' action='index.jsp' method='post' style='visibility:hidden'>");
document.write("<input type='hidden' name='loadurl' name='loadurl' value='login.jsp'/>");
document.write("</form>");
document.write("<script>document.bytimesroot1.submit();\<\/script\>");
}
}
window.onload = CheckRootPage;
</script>
那么多回复的,就这位讲对了原因呢,不过没有讲清楚lz代码中主要有两个问题,在window.onload后调用document.write()估计lz也不知道会发生什么事情吧,调用这个方法后会覆盖当前页面,类似于你新请求一个窗口,不过因为页面里面没有内容,就无所谓了还有一个就是执行顺序的问题,lz想在window.onload里面调用 form.submit,估计是知道没有load完调用不到,不过可惜你用了document.write(),等于重新载入一个页面,所以在这里调用form不一定载入完成了,我做了个小修改就可以用了<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> CheckRootPage(); 在未载入完成前调用document.write()
window.onload = function(){document.bytimesroot.submit(); } 在载入完成后调用submit不过我觉得载入完成后用document.createElement("");添加元素可能会好一点
好像就只有是在里面加一些非hidden类型的表单就可以取到
刚提交完帖子就看到你的帖子了,看来你也没有看出具体的问题所在页面代码载入完成不代表 dom可用,这个一般跟浏览器解析有关,不过一般载入该元素的下一个元素的时候该元素的dom就可用了(个人经验,所以有人不在 window.onload里面写代码,而在</html>后面加入js代码),你的代码等于是在下一个script里面调用 form,这时候form应该就可用了
<!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>
这个改为
document.write(" <input type=hidden name=loadurl value='login.jsp'");
好像就好了 真搞不懂!! 真没见过这么怪的问题
说名字一样造成的吗!
但是换了一个类型他就可以了
index.jsp?loadurl=login.jsp
这样了.也是最简单的了.
都说了这是页面载入的问题,你们用各种方式去测试什么时候dom会在代码载入的时候可用是没有意义的,代码载入时候dom 何时可用各个浏览器是不同的,但是只要第一次 dom载入完成,以后dom都可用,这是各个浏览器都相同的,为证明我的结论,下面这段代码也是可用的,不用document.write(),用其他方式就可以了 ,form里面的元素和lz是一样的,没有任何修改<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>
就是 dom载入的问题,你看看我#15,17,20的回复吧,我写的代码我不用做跨浏览器测试凭经验就知道在ff,ie,google等遵循标准的浏览器可用,你是可以解决问题,但是你能说清楚原因,并不做测试就觉得会在遵循标准的浏览器上都可用吗?
代码载入,如页面载入到<form name="test">...</form>的时候,document.test是否可用,或是要在form里面设置什么东西,或是里面要添加什么东西(如可见的表单元素,才能使页面载入到<form name="test">...</form>的时候,document.test可用
我开始也说过是加载顺序问题但经过我测试如果是加载顺序问题嘛那么我可以这样<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.write("<script>document.bytimesroot.submit();\<\/script\>")
}
}
window.onload = CheckRootPage;
</script>
就是红色部分,甚至再红色部分再加一个延时也不行所以我确定并非是加载顺序问题因此,我做了即方面的测试结果发现了以上我说的2种方式都可以解决问题
具体什么原因也不清楚,因为以前根本没去注意的估计也没人去在意这问题
因为很少有人这么用
代码如下
<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了
谢谢
结贴了!
你加延时没用,要关闭文档才行,不过你倒是让我发现了另一个解决问题的方法,下面是一个例子:<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 name=loadurl value='login.jsp'");
document.write("</form>");
document.close();
document.bytimesroot.submit();
//window.location.href="index.jsp?loadurl=login.jsp";
}
}
window.onload = CheckRootPage;
</script>
就加了上面红色那句话,也可以在各个浏览器运行,反正我个人觉得是dom加载的问题,然后以这个方法去解决问题,想出了几种方案都是可以解决问题的,至于是不是真的是 dom加载的问题,大家见仁见智吧
如果你说是dom加载的问题我只要一个说法就把你推翻把
document.write(" <input type=hidden name=loadurl name=loadurl value='login.jsp'");
改为
document.write(" <input type=text name=loadurl name=loadurl value='login.jsp'");
或者是在
document.write(" <input type=hidden name=loadurl name=loadurl value='login.jsp'");
下面加
document.write(" <input type=text name=loadurl name=loadurl value='1'");
就可以执行到,不用加任何东西算了,不过说再说估计也只会耍嘴皮子,怕推翻了自己的观点刚开始看这个的题的时候我的第一反映也是加载顺序问题可事实证明并非如此