【发布时间】:2017-08-09 03:50:37
【问题描述】:
我在 HTML 页面中有一个 iframe。 我的网站在域上:abc.domain.local 我的 iframe 在域上:def.domain.local
我做的一些测试甚至没有返回任何东西,然后一些返回 Permission denied to access property 'document'
我尝试了 9 种不同的方法(代码如下),但没有一种方法有效。 我已经在 Firefox(最新版本)上安装了 Cors Everywhere 并禁用了策略。
我已经安装了很多其他扩展来禁用 Chrome 和 Firefox 上的跨域策略,但没有任何结果。 我已经用一些参数启动了 chrome 来禁用网络安全,但也没有用
不幸的是,我无法在 def.domain.local 上为网页设置任何内容,而不是事件创建 JS 文件以使用 postmessage
<html>
<head>
<script src="js/jquery-3.1.1.js"></script>
</head>
<body>
<input type="button" value="1" onclick="teste1()" />
<input type="button" value="2" onclick="teste2()" />
<input type="button" value="3" onclick="teste3()" />
<input type="button" value="4" onclick="teste4()" />
<input type="button" value="5" onclick="teste5()" />
<input type="button" value="6" onclick="teste6()" />
<input type="button" value="7" onclick="teste7()" />
<input type="button" value="8" onclick="teste7()" />
<input type="button" value="9" onclick="teste7()" />
<input type="button" value="10" onclick="teste7()" />
<br /><br />
<iframe id="frame" name="frame" style="width: 1000px; height: 600px;" src="http://abc.domain.local">
</iframe>
<script type="text/javascript">
document.domain = 'domain.local';
function teste1(){
console.log(document.getElementById('frame').contentWindow.document.body.innerHTML);
}
function teste2(){
$.getJSON('http://def.domain.local/monitor.htm', function(data){
console.log(data);
});
}
function teste3(){
console.log($("#frame").find("iframe").contents().find('body'));
console.log($("#frame").find("iframe").contents().find('html'));
}
function teste4(){
a = $("#frame").find("iframe").contents().find('body');
console.log(a.innerHTML);
console.log(a.html());
}
function teste5(){
var iframeWindow1 = document.getElementById("frame").contentWindow;
iframeWindow1.addEventListener("load", function() {
var doc1 = iframe.contentDocument || iframe.contentWindow.document;
var target1 = doc.getElementById("html");
console.log(target1.innerHTML);
});
}
function teste6(){
$.ajax({
url: 'http://def.domain.local/monitor.htm',
type: 'GET',
dataType: 'html',
success: function (data) {
console.log(data);
},
error: function (data) {
console.log("error: " + data);
console.log(data);
}
});
}
function teste7(){
$('iframe').contents().find('body').css('opacity','0.1');
}
function teste8(){
var iframe = document.getElementById('frame');
var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
var iframeContent;
iframeContent = iframeDocument.getElementById('body');
var iframeWindow = iframe.contentWindow;
// you can even call jQuery or other frameworks if it is loaded inside the iframe
console.log(iframeWindow.jQuery('body'));
// or
console.log(iframeWindow.$('body'));
// or even use any other global variable
console.log(window.outside_iframe_variable);
}
function teste9(){
var req = create("get", "http://def.domainlocal");
if (req){
req.onload = function (){
console.log(req.reposnseText)
};
req.send();
}
}
function create(method, url){
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr){
xhr.open(method, url, true);
}
else if (typeof XDomainRequest != "undefined"){
xhr = new XDomainRequest();
xhr.open(method, url);
}
else {
xhr = null
}
return xhr;
}
</script>
</body>
</html>
【问题讨论】:
-
究竟是什么给了你这个错误?您是否尝试修改 rthe iframe 运行时?
-
当我执行“teste3”时收到此错误。我正在尝试从 iframe 获取 HTML
-
你是否在两个 abc 中都添加了
document.domain = 'domain.local';。和定义。 ? -
不幸的是,我无法在 def.domain.local 上为网页设置任何内容,而不是创建 JS 文件以使用 postmessage
-
如果我没记错你就不能这样做
标签: javascript iframe dns