[英]How can I resize a modal iframe when a new page loads in it?
I have several linked pages I want to display in a modal iframe. 我想要在模态iframe中显示几个链接页面。 The pages are not the same size and I want to be able to resize the iframe when each new page is loaded.
页面大小不同,我希望能够在加载每个新页面时调整iframe的大小。 I have looked at several jquery plugins to create the iframe, but can't figure out how to resize any of them.
我已经查看了几个jquery插件来创建iframe,但无法弄清楚如何调整其中任何一个。 I am currently experimenting with prettyPhoto and nyroModal, but am open to suggestions.
我目前正在尝试使用prettyPhoto和nyroModal,但我愿意接受建议。 I just need to make it work.
我只需要让它工作。 Also I am not very good with JavaScript, but I am trying to learn.
我也不是很擅长JavaScript,但我正在努力学习。
NOTE: all my pages are on my web server, so there is no issue with cross domain. 注意:我的所有页面都在我的Web服务器上,因此跨域没有问题。
Thanks for all the quick responses. 感谢所有快速回复。 I'm headed to bed but look forward to trying out your suggestions when I wake up.
我要去睡觉了,但是当我醒来的时候,我期待着尝试你的建议。
Try putting this in the iframe: 尝试将其放在iframe中:
document.onload = function() {
var i = parent.document.getElementById('myIframe');
i.style.width = document.body.offsetWidth+"px";
i.style.height = document.body.offsetHeight+"px";
}
When loading pages to iframe you probably will encounter cross-domain problems. 将页面加载到iframe时,您可能会遇到跨域问题。 Accessing page inside the iframe from parent's JS is blocked.
从父级JS访问iframe中的页面被阻止。
If you want to do it easily - keep track of what is loaded int o the iframe in your code and resize it manually to hard-coded dimensions. 如果您想轻松完成 - 跟踪代码中iframe的内容并手动调整为硬编码尺寸。
Create an array of dimensions you want to use for each page and use this function: 创建要用于每个页面的维度数组并使用此函数:
function() {
var i = document.getElementById('myIframe');
var sr=i.src;
i.style.width = myDimensionsArray[sr][w]+"px";
i.style.height = myDimensionsArray[sr][h]+"px";
}
You can use postMessage for this. 你可以使用postMessage。 In your iframe write this:
在你的iframe中写这个:
document.body.onload=function(){
parent.postMessage("resize_me", "*");
}
In your main page:
window.addEventListener("message", function(e){ if(e.data=="resize_me") { //resize the iframe } }
There are 2 cases here: 这里有2个案例:
iframe is loaded before the modal is open 在模态打开之前加载iframe
function resizeIframe(obj){ obj.style.height = 0; obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px'; } $(".modal").on('shown.bs.modal', function () { resizeIframe(document.getElementById(IFRAME_ID)); }); window.onload=function(){ document.getElementById('IFRAME_ID').addEventListener('load', function(){ resizeIframe(document.getElementById(IFRAME_ID)); }); }
In case of 的情况下
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.