加载前隐藏的 iframe 的 JavaScript 问题
我有一个页面,其中包含使用 Javascript 加载的 iframe
:
index.html
<iframe id="myFrame" width="800" height="600" style="display: none;"></iframe>
<div id="loader"><!-- some loading indicator --></div>
<script type="text/javascript">
function someFunction() {
var myFrame = document.getElementById('myFrame');
var loader = document.getElementById('loader');
loader.style.display = 'block';
myFrame.src = 'myFrame.html';
myFrame.onload = function() {
myFrame.style.display = 'block';
loader.style.display = 'none';
};
}
</script>
在 iframe
中加载的页面包含一些 Javascript 逻辑它计算某些元素的大小,以便添加 JS 驱动的滚动条(jScrollPane + jQuery Dimensions)。
myFrame.html
<div id="scrollingElement" style="overflow: auto;">
<div id="several"></div>
<div id="child"></div>
<div id="elements"></div>
</div>
<script type="text/javascript">
$(document).load(function() {
$('#scrollingElement').jScrollPane();
});
</script>
这在 Chrome(可能还有其他 Webkit 浏览器)中有效,但在 Firefox 和 IE 中失败,因为在调用 jScrollPane
时,所有元素仍然不可见并且jQuery Dimensions 无法确定任何元素的尺寸。
有没有办法确保我的 iframe
在调用 $(document).ready(...)
之前可见?除了使用 setTimeout
来延迟 jScrollPane
之外,这是我绝对想避免的。
I have a page that contains an iframe
that gets loaded using Javascript:
index.html
<iframe id="myFrame" width="800" height="600" style="display: none;"></iframe>
<div id="loader"><!-- some loading indicator --></div>
<script type="text/javascript">
function someFunction() {
var myFrame = document.getElementById('myFrame');
var loader = document.getElementById('loader');
loader.style.display = 'block';
myFrame.src = 'myFrame.html';
myFrame.onload = function() {
myFrame.style.display = 'block';
loader.style.display = 'none';
};
}
</script>
The page that gets loaded in the iframe
contains some Javascript logic which calculates the sizes of certain elements for the purposes of adding a JS driven scrollbar (jScrollPane + jQuery Dimensions).
myFrame.html
<div id="scrollingElement" style="overflow: auto;">
<div id="several"></div>
<div id="child"></div>
<div id="elements"></div>
</div>
<script type="text/javascript">
$(document).load(function() {
$('#scrollingElement').jScrollPane();
});
</script>
This works in Chrome (and probably other Webkit browsers), but fails in Firefox and IE because at the time jScrollPane
gets called, all the elements are still invisble and jQuery Dimensions is unable to determine any element's dimensions.
Is there a way to make sure my iframe
is visible before $(document).ready(...)
gets called? Other than using setTimeout
to delay jScrollPane
, which is something I definitely want to avoid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
某些浏览器假设当“display:none”应用于替换元素(如 Flash 或 iframe)时,不再需要该元素的视觉信息。因此,如果该元素稍后由 CSS 显示,浏览器实际上将重新创建临时数据形式。
我想象 iframe 默认为“display:none;”使浏览器跳过 HTML 的渲染,因此标签没有任何尺寸。我会将可见性设置为“隐藏”或将其放置在页面之外,而不是使用“display:none;”。
祝你好运。
Some browsers assume that when "display:none" is applied to replaced elements (like Flash or an iframe) the visual info for that element is no longer needed. So, if the element is later displayed by the CSS, the browser will actually recreate the visual data form scratch.
I imagine that having the iframe default to "display:none;" makes the browser skip the rendering of the HTML so the tags don't have any dimensions. I would set the visibility to "hidden" or position it off the page rather than use "display:none;".
Good luck.
您可以尝试...
设置
visibility:hidden
... 设置
position:absolute ,而不是使用
display:none
使 iframe 不可见; top:-600px;... 设置
opacity:0
或其他使 jQuery “看到”对象而不是用户的东西(并重置
中使用的 css 属性>myFrame.onload
函数)。instead of making the iframe invisible by using
display:none
, you could try to...... set
visibility:hidden
... set
position:absolute; top:-600px;
... set
opacity:0
or something else that makes jQuery "see" the objects but not the user (and reset the used css-attributes in your
myFrame.onload
function).可见性:折叠;
显示:隐藏;
height:0px;
也将努力消除空白..
iframe 也会加载..
visibility:collapse;
display:hidden;
height:0px;
Will work to get rid of white space too..
The iframe will also load..
隐藏的 iframe 是一个巨大的安全问题。如果合法的话,最好尝试找到另一种方法来完成您想要的任务,因为希望未来的浏览器将完全摆脱此功能。 http://blog.opendns.com/2012/ 07/10/opendns-security-team-blackhole-exploit/
Hidden iframes are a huge security issue. Probably best to try to find another way to accomplish what you want, if it is legitimate, because hopefully future browsers will get rid of this feature altogether. http://blog.opendns.com/2012/07/10/opendns-security-team-blackhole-exploit/