Javascript 如何使Internet Explorer使用Window Post消息跨域运行脚本
一个客户端要求我构建一个页面,该页面可以构建到不同域上的父页面中,并自动调整大小 经过一些尝试和错误,这是正常工作的铬,火狐等。。。但在IE11中,我得到了一个“SCRIPT5:访问被拒绝”错误 父页面上的调整大小代码Javascript 如何使Internet Explorer使用Window Post消息跨域运行脚本,javascript,html,iframe,cross-domain,postmessage,Javascript,Html,Iframe,Cross Domain,Postmessage,一个客户端要求我构建一个页面,该页面可以构建到不同域上的父页面中,并自动调整大小 经过一些尝试和错误,这是正常工作的铬,火狐等。。。但在IE11中,我得到了一个“SCRIPT5:访问被拒绝”错误 父页面上的调整大小代码 //Timer is used to prevent duplicate resize events var rtime; var timeout = false; var delta = 300; $(window).resize(fun
//Timer is used to prevent duplicate resize events
var rtime;
var timeout = false;
var delta = 300;
$(window).resize(function() {
rtime = new Date();
if (timeout === false) {
timeout = true;
//This shrinks the iframe before it provides a height value to prevent it growing exponentially
iFrame.style.height = "1100px";
setTimeout(resizeend, delta);
}
});
$(document).ready(function() {
rtime = new Date();
if (timeout === false) {
timeout = true;
setTimeout(resizeend, delta);
}
});
function resizeend() {
if (new Date() - rtime < delta) {
setTimeout(resizeend, delta);
} else {
timeout = false;
console.log('starting...');
iframe = document.getElementById('iFrame');
iframe.contentWindow.postMessage('sizing?', '*');
// we have to listen for 'message' window.addEventListener('message', respondToSizingMessage, false);
}
}
}
handleSizingResponse = function(e) {
console.log('begin response handling');
console.log('origin '+ e.origin);
var action = e.data.split(':')[0]
if(action == 'sizing') {
var values = e.data.split(':')[1];
var newHeight = values.split(',') [0] + 'px';
//var newWidth = values.split(',') [1] + 'px';
console.log ('New Height: ' + newHeight);
iFrame = document.getElementById('iFrame');
iFrame.style.height = newHeight;
//iFrame.style.width = newWidth;
}
else {
console.log("Unknown message: "+e.data);
}
}
window.addEventListener('message', handleSizingResponse, false);
我不清楚如何解决这个问题,我是否应该在我的JS中构建一个在浏览器为IE时表现不同的条件,或者我是否必须以完全不同的方式重写/处理这个问题
//Handle resize events
respondToSizingMessage = function(e) { // e.data is the string sent by the origin with postMessage.
console.log('resize event received by frame');
if(e.data == 'sizing?') {
var body = document.body,
html = document.documentElement;
//Get Body height
var height = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight );
//Send Body height
e.source.postMessage('sizing:'+height+','+document.body.scrollWidth, e.origin);
};
};
//Listen for incoming messages, and call response function
window.addEventListener('message', respondToSizingMessage, false);