父页面:main.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>main.html</title> </head> <body> <script type="text/javascript"> // 按照W3C标准父页面找到子页面,语法如下document.getElementById(”iframe1″).contentWindow;取到子页面对象 // <iframe id=”iframe1″ name=”iframe1″ frameborder=”0″ scrolling=”0″ // style=”width:300px; height:150px; border:1px #009966 solid;” src=”1.html”></iframe> function show() { window.open("open.html","newindow","width=100,height=200,toolbar=no,scrollbars=no"); // window.open(url, name, features, replace); 实现打开一个新窗口 // url地址,name新窗口名字,name相同的窗口只能创建一个,features用于设置新窗口的设置 // replace 指可选布尔参数,设置新窗口中的操作历史的保存方式,其中true 指创建新历史记录,false指替换旧的历史记录。 } function getNewLinkValue(value) { alert(value);//可以得到子页面设的值 } function shw() { var url = "open.html"; //----------方法一--start------- var obj = document.form1; //指定页面对象 var returnValue = window.showModalDialog(url, obj, 'dialogHeight:550px;dialogWidth:500px;center:yes'); //方法返回值 //----------方法一--end------- alert("returnValue:"+returnValue); alert("fag1.value:"+document.form1.fag1.value); } </script> <form name="form1"> <input type="text" name="fag1"> <input type="text" name="fag2"> <input type="button" value="button" onclick="shw()"> <input type="button" value="show" onclick="show()"> </form> </body> </html>
弹出页面open.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>open.html</title> </head> <body> <script type="text/javascript"> function setNewLinkValue() { window.opener.getNewLinkValue("abc");//调用父页面的方法给父页面的传值 // window.parent能获取一个框架的父窗口或父框架。顶层窗口的parent引用的是它本身。(通常在使用ifream调用或者location.href调用时可以使用此) // window.opener引用的是window.open打开的页面的父页面,当一个窗口用open方法打开了一个新窗口的时候,opener属性就生效; } function sendValue() { var TNO = document.all.TNO.value; var SNO = document.all.SNO.value; //----------方法二--start------- var obj = window.dialogArguments; //父页面对象 obj.elements["fag1"].value = SNO; //给父页面对象赋值 // 显然,这里也同样可以得到父页面中指定的值 alert("子页面form表单中的值:"+obj.elements['fag1'].value); obj.elements["fag2"].value = TNO; //----------方法二--end------- //----------方法一--start------- window.returnValue = SNO + "," + TNO; //方法返回值 //----------方法一--end------- window.close(); } </script> <form name="form2"> <input type="text" name="SNO"> <input type="text" name="TNO"> <input type="button" value="ok" onclick="sendValue()"> <input type="button" value="setNewLinkValue" onclick="setNewLinkValue()"> </form> </body> </html>