我希望在屏幕调整大小时调整我的iframe高度。我的iframe中有以下脚本:
<script type="text/javascript">
function sendHeight() {
if (parent.postMessage) {
// wrapper holds whole content of the page
var height = document.getElementsByClassName("wrapper").offsetHeight;
parent.postMessage(height, '*');
}
}
// Create browser compatible event handler.
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";
// Listen for a message from the iframe.
eventer(messageEvent, function (e) {
if (isNaN(e.data)) return;
sendHeight();
},
false);
</script>
在页面上,我包含了我的iframe:
<iframe id="iframeid" src="https://iframesource.com" scrolling="no" width="100%" onload="document.getElementById('load-img').style.display='none';"></iframe>
<script>
// Create browser compatible event handler.
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";
// Listen for a message from the iframe.
eventer(messageEvent, function(e) {
if (isNaN(e.data)) return;
// replace #sizetracker with what ever what ever iframe id you need
document.getElementById('antrag').style.height = e.data + 'px';
}, false);
</script>
但它没有用。控制台中未显示任何错误。 我的iframe位于不同的域中。
我怎样才能使这个工作?我已经找到了解决方案,但还没找到一个...