Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript window.parent.document在firefox中工作,但在chrome和IE中不工作_Javascript_Jquery_Html_Iframe - Fatal编程技术网

Javascript window.parent.document在firefox中工作,但在chrome和IE中不工作

Javascript window.parent.document在firefox中工作,但在chrome和IE中不工作,javascript,jquery,html,iframe,Javascript,Jquery,Html,Iframe,我的概念是从iframe更新主页中文本框的值。此代码在firefox中工作,但在InternetExplorer和Chrome中不工作。main.html和frame.html都位于同一位置。我需要的建议,使它在所有的浏览器工作 main.html <!DOCTYPE html> <html> <head> <title> main window </title> </head> <body> Parent te

我的概念是从iframe更新主页中文本框的值。此代码在
firefox
中工作,但在
InternetExplorer
Chrome
中不工作。
main.html
frame.html
都位于同一位置。我需要的建议,使它在所有的浏览器工作

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>

框架窗口
函数PushValues()
{
var frame_value=document.getElementById(“framebox”).value;
window.parent.document.getElementById(“parentbox”).value=frame\u值;
}

既然您正在使用jQuery,请尝试以下操作

var frame_value = $('#framebox').val();
$('#parentbox', window.parent.document).val(frame_value);
Jquery-

function PushValues()
{
  var frame_value = $('#framebox').val();
  parent.$('body').find('#parentbox').val(frame_value);
}

它总是对我有用。

在xamp或wamp这样的服务器上运行此代码。它不会直接工作。

Main.html


主窗口
父文本框:


窗口。\u fn={ printval:函数(响应){ $(“输入”).val(响应); }, };
iframe


框架窗口
函数PushValues(){
window.parent._fn['printval']($('input').val());
}

您应该尝试与iFrame和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"'

根据安全策略,跨域访问受到限制。如果您试图在域2中显示域1中的页面,并试图从域1中的脚本操作域2中页面的DOM,则会发生这种情况。如果您正在服务器上的同一位置运行页面。这不应该发生。但是,如果您只是将它们保存为HTML文件,并试图在浏览器中打开它们,则该操作不起作用。我已经为你的代码创建了两个JSBIN,它正在chrome上运行。尝试使用以下链接访问它们

Main.html: iframe.html:


通过在chrome(F12)中保持控制台打开并单击填充按钮,尝试在JSBin中以编辑模式运行main.html。它将不起作用,并将向您显示错误。如果您按原样运行它们(在JSBin的运行模式下),它会工作。

控制台中有错误吗?您是否尝试过
window.top.document
该代码与我在Firefox、Chrome、Safari和IE中的代码一样工作正常,尽管您确实应该关闭标记。@Chris我确实有一个iframe的关闭标记,我只是错过了这里。您能告诉我您的浏览器版本吗。@Cherniv我在Chrome控制台中遇到以下错误。1.
阻止原点为“null”的帧访问原点为“null”的帧。
2.
协议、域和端口必须匹配。未捕获的TypeError:无法调用未定义的方法“getElementById”。
我尝试了这个方法,这就是为什么我有那个jqery。即使这在firefox中也可以单独使用,而不是在chrome中。@user2564024那么你应该看看它,如果父级
不包含jQuery怎么办?我不明白你想说什么。这段代码被写入到“IFrame”中,所以“IFrame”总是有一个父级。没错,
parent
总是存在的。但是
父项。$
-并非100%存在!是的,你说得对。如果父窗口不包含jQuery文件,这就是创建问题。我在IE中工作,但在Chromewell中不工作。我刚刚发现,即使您的代码也可以正常工作,只是它需要在服务器级别运行,以避免iframe的跨域问题。