我想将数据从子窗口传递到父窗口。
问题是打开了子窗口,但用户输入的值未传递到父窗口
parent.html
<form action="" method="post" name="f1">
<table border="0" cellpadding="0" cellspacing="0" width="550">
<tr>
<td>
<font face="Verdana" size="2">
Your Name
</font>
<input name="p_name" size="8" type="text">
<a href="javascript:void(0);" name="My Window Name" onclick='window.open("child.html","Ratting", "width=550,height=170,left=150,top=200,toolbar=1,status=1,");' title=" My title here ">
Click here to open the child window
</a>
</input>
</td>
</tr>
</table>
</form>
child.html
<html>
<head>
<script langauge="javascript">
function post_value() {
opener.document.f1.p_name.value = document.frm.c_name.value;
self.close();
}
</script>
<title>
(Type a title for your page here)
</title>
</head>
<body>
<form action="" method="post" name="frm">
<table border="0" cellpadding="0" cellspacing="0" width="250">
<tr>
<td align="center">
Your name
<input name="c_name" size="12" type="text" value="test">
<input onclick="post_value();" type="button" value="Submit">
</input>
</input>
</td>
</tr>
</table>
</form>
</body>
</html>
如何将数据从子窗口传递到父窗口?
答案 0 :(得分:1)
您可以打开一个充当对话框的新窗口,并将信息传递回主窗口。 但是,这受同源策略的限制,因此您需要确保两个文件都来自相同的起源。
Refer this SO answer for a detailed explanation on same-origin policy
问题
在本地计算机上(通过文件系统)运行时,浏览器在控制台中显示Uncaught DOMException: Blocked a frame with origin "null" from accessing a cross-origin frame
错误,因为它无法确认origin
。
解决方案
从一个Web服务器提供这两个文件
要在本地调试吗?
您可以使用简单的python服务器在任何目录中运行Web服务器。步骤如下:
cd /path/to/directory
python -m SimpleHTTPServer
localhost:8000
希望有帮助!