javascript window.parent.document 在 firefox 中工作,但不在 chrome 和 IE 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17781938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
window.parent.document working in firefox , but not in chrome and IE
提问by Nishanth Lawrence
My concept is to update the value of the text box in the main page from the iframe . This code is working in firefox
, but not working in Internet Explorer
and Chrome
. Both main.html
and frame.html
are in same location . I need suggestions to make it work in all the browsers .
我的概念是从 iframe 更新主页中文本框的值。此代码适用于firefox
,但不适用于Internet Explorer
和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
框架.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>
回答by Abhishek Chaitanya Gorlagunta
As per security policies, cross-domain access is restricted. This will happen if you are trying to show a page from domain 1 in domain 2 and try to manipulate the DOM of page in domain 2 from the script in domain 1. If you are running the pages from same location on a server. This shouldn't happen. However, if you are just saving them as HTML files and trying to open them in your browser, it should not work. I have created two jsbins for your code and it is working on chrome. Try to access them using the below links.
根据安全策略,跨域访问受到限制。如果您尝试在域 2 中显示域 1 中的页面并尝试从域 1 中的脚本操作域 2 中页面的 DOM,则会发生这种情况。如果您在服务器上的同一位置运行页面。这不应该发生。但是,如果您只是将它们保存为 HTML 文件并尝试在浏览器中打开它们,则它应该不起作用。我为您的代码创建了两个 jsbin,它正在处理 chrome。尝试使用以下链接访问它们。
Main.html: http://jsbin.com/afazEDE/1iframe.html: http://jsbin.com/ayacEXa/1/
Main.html:http://jsbin.com/afazEDE/1 iframe.html:http: //jsbin.com/ayacEXa/1/
Try to run main.html in edit mode in JSBin by keeping console open in chrome (F12) and click fill button. It will not work and will show you the error. If you run them as it is (in run mode of JSBin), it will work.
通过在 chrome (F12) 中保持控制台打开并单击填充按钮,尝试在 JSBin 中以编辑模式运行 main.html。它不起作用,并会向您显示错误。如果按原样运行它们(在 JSBin 的运行模式下),它将起作用。
回答by Ishan Jain
Jquery -
jQuery -
function PushValues()
{
var frame_value = $('#framebox').val();
parent.$('body').find('#parentbox').val(frame_value);
}
It's always work for me.
它总是对我有用。
回答by Richie Fredicson
Run this code on a server like xamp or wamp it wont work directly
在像 xamp 或 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>
回答by Radu Toader
You should try P3P policy which is highly related to iframes and Internet Explorer. response header set to the iframe document
您应该尝试与 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"'
回答by Praveen
Since you're using jQuery try this
既然你使用 jQuery 试试这个
var frame_value = $('#framebox').val();
$('#parentbox', window.parent.document).val(frame_value);