您的位置:首页 > 其它

弹出窗口并向父页面返回值的两种常用方法

2017-11-16 18:02 423 查看
方法一:使用showModalDialog方法

新建文件lyh.html

输入
<!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=gb2312" />

<title>来自Eric空间</title>

</head>
<body>

<input type="button" onclick="show('lyhshow.html')" value="弹出窗口"><br><br><br>返回值:

<input type="text" id="returnvalue">
<script language="javascript">

function show(math)

{

var val= showModalDialog(math,"true", "dialogWidth:300px;dialogHeight:250px;status:no;help:no;");
if(!val)
 
{

return;

}

else

{

document.all.returnvalue.value=val;

}

}

</script>

</body>

</html>

新建文件lyhshow.html

输入

<!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=gb2312" />

<title>来自Eric空间</title>

</head>
<body>

请输入:<input type="text" id="txtValue" value=""><br><br><br>

<input type="button" onclick="ReturnValue()" value="返回输入值">
<script language="javascript"> 

function ReturnValue() 

{

window.returnValue=document.all.txtValue.value;

window.close();

}

</script>

</body>

</html>

 
方法二:用window.open弹出窗口
新建文件lyh2.html

输入

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>来自Eric空间</title>

</head>
<body>

<input type="button" onclick="show('lyhshow2.html')" value="弹出窗口"><br><br><br>返回值:

<input type="text" id="returnvalue">
<script language="javascript">

<!-- 

function show(math)

{
window.open (math,'newwindow','height=250,width=300,top=300,left=300,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no') 

//写成一行
}

--> 

</script>

</body>

</html>

新建文件lyhshow2.html

输入
<html xmlns="http://www.w3.org/1999/xhtml">
 
<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>来自Eric空间</title>

</head>
<body>

请输入:<input type="text" id="txtValue" value=""><br><br><br>

<input type="button" onclick="ReturnValue()" value="返回输入值">
<script language="javascript">

function ReturnValue()

{

window.opener.document.all.returnvalue.value=document.all.txtValue.value;

window.close();

}
/*优化
窗口关闭时生效*/
window.onbeforeunload = onbeforeunload_handler;   

    function onbeforeunload_handler(){   

       window.opener.doc
a01c
ument.all.returnvalue.value=document.all.txtValue.value;

    }

</script>

</body>

</html> 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: