我的概念是从iframe更新主页面的文本框的值。此代码在firefox
中工作,但在Internet Explorer
和Chrome
中不工作。CCD_ 4和CCD_。我需要建议,使它在所有浏览器中都能工作。
main.html
<!DOCTYPE html>
<html>
<head>
<title> main window </title>
</head>
<body>
Parent textbox :<input type="text" id="parentbox"></br></br></br>
<iframe src="frame.html" ></iframe>
</body>
</html>
frame.html
<!DOCTYPE html>
<html>
<head>
<title> frame window </title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
function PushValues()
{
var frame_value = document.getElementById("framebox").value;
window.parent.document.getElementById("parentbox").value =frame_value;
}
</script>
</head>
<body>
<input type="text" id="framebox" >
<input type="button" value ="fill" onclick="PushValues()">
</body>
</html>
根据安全策略,跨域访问受到限制。如果您试图在域2中显示来自域1的页面,并试图从域1中的脚本操作域2中页面的DOM,则会发生这种情况。如果您在服务器上的同一位置运行页面。这不应该发生。但是,如果您只是将它们保存为HTML文件,并试图在浏览器中打开它们,那么它应该不起作用。我为您的代码创建了两个jsbin,它正在chrome上工作。请尝试使用以下链接访问它们。
Main.html:http://jsbin.com/afazEDE/1iframe.html:http://jsbin.com/ayacEXa/1/
通过在chrome(F12)中保持控制台打开并单击填充按钮,尝试在JSBin中以编辑模式运行main.html。它将不起作用,并将向您显示错误。如果您按原样运行它们(在JSBin的运行模式下),它就会工作。
function PushValues()
{
var frame_value = $('#framebox').val();
parent.$('body').find('#parentbox').val(frame_value);
}
这对我来说总是有效的。
在example或wamp这样的服务器上运行此代码,它不会直接工作
Main.html
<!DOCTYPE html>
<html>
<head>
<title> main window </title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
Parent textbox :<input type="text" id="parentbox" value=""></br></br></br>
<iframe src="iframe.html"></iframe>
<script>
window._fn = {
printval: function (response) {
$("input").val(response);
},
};
</script>
</body>
iframe
<!DOCTYPE html>
<html>
<head>
<title> frame window </title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<input type="text" id="framebox">
<input type="button" value="fill" onclick="PushValues()">
<script language="javascript">
function PushValues() {
window.parent._fn['printval']($('input').val());
}
</script>
</body>
</html>
由于您正在使用jQuery,请尝试此
var frame_value = $('#framebox').val();
$('#parentbox', window.parent.document).val(frame_value);
您应该尝试与iframes和Internet Explorer高度相关的P3P策略。设置为iframe文档的响应标头
header key= 'P3P' header value: 'CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"'