真是有分,开了那么多个,在这里我说具体点拉,主要问题就在于页面的加载,你 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>
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>
不管延时也好,还是什么也好
<!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>
也就是3楼说的那方式
但你的那个方式我试了N遍,只要form内只含有hidden得表单元素,就无法用document.form1.submit()提交
这个改为
document.write("<input type=hidden name=loadurl value='login.jsp'");
好像就好了真搞不懂!!真没见过这么怪的问题
说名字一样造成的吗!
但是换了一个类型他就可以了
都说了这是页面载入的问题,是代码载入和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>
测试了一遍又一遍如果说是由于有2个name造成的
为什么只要把document.write(" <input type=hidden name=loadurl name=loadurl value='login.jsp'");
换一个类型也可以提交谁明白原理的就说一下吧反正我是搞不明白了
我的代码不行??你就知道在这里用嘴皮子让你自己按我说的测试,你不去我说得很明白了就按LZ现在这个程序的顺序,不用你的方式只要把
document.write(" <input type=hidden name=loadurl name=loadurl value='login.jsp'");
这个改为
document.write(" <input type=hidden name=loadurl value='login.jsp'");
就好了
一是: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'"); 原因要是说得明白我用着测试那么多遍了就因为这一现象以前没注意过这是第一次,估计也没人注意过
代码如下 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了
谢谢