admin管理员组文章数量:1424942
Here is my testing.html:
<html>
<meta charset="utf-8">
<script>
window.onload = function() {
var a = document.getElementById("divid");
var ifr = document.createElement('iframe');
a.appendChild(ifr);
ifr.contentDocument.body.style.cssText = (
'margin: 0px;' +
'padding: 0px;' +
'height: 100%;' +
'width: 100%;');
}
</script>
<head></head>
<body>
<div id="divid"/>
</body>
</html>
Here ifr.contentDocument.body.style.cssText = (...) works fine on chrome but not on Firefox. Is it a firefox bug? Is there any workaround?
Found workaround: Looks like there is a weird race bug in firefox. The following workaround with setTimeout will make this work fine as shown below:
<html>
<meta charset="utf-8">
<script>
window.onload = function() {
var a = document.getElementById("divid");
var ifr = document.createElement('iframe');
ifr.id = "fid";
a.appendChild(ifr);
setTimeout (function() {
ifr.contentDocument.body.style.cssText = (
'margin: 0px;' +
'padding: 0px;' +
'height: 100%;' +
'width: 100%;');
}, 100);
}
</script>
<head></head>
<body>
<div id="divid"/>
</body>
</html>
Here is my testing.html:
<html>
<meta charset="utf-8">
<script>
window.onload = function() {
var a = document.getElementById("divid");
var ifr = document.createElement('iframe');
a.appendChild(ifr);
ifr.contentDocument.body.style.cssText = (
'margin: 0px;' +
'padding: 0px;' +
'height: 100%;' +
'width: 100%;');
}
</script>
<head></head>
<body>
<div id="divid"/>
</body>
</html>
Here ifr.contentDocument.body.style.cssText = (...) works fine on chrome but not on Firefox. Is it a firefox bug? Is there any workaround?
Found workaround: Looks like there is a weird race bug in firefox. The following workaround with setTimeout will make this work fine as shown below:
<html>
<meta charset="utf-8">
<script>
window.onload = function() {
var a = document.getElementById("divid");
var ifr = document.createElement('iframe');
ifr.id = "fid";
a.appendChild(ifr);
setTimeout (function() {
ifr.contentDocument.body.style.cssText = (
'margin: 0px;' +
'padding: 0px;' +
'height: 100%;' +
'width: 100%;');
}, 100);
}
</script>
<head></head>
<body>
<div id="divid"/>
</body>
</html>
Share
Improve this question
edited May 5, 2013 at 6:31
Krishna Srinivas
asked May 5, 2013 at 5:19
Krishna SrinivasKrishna Srinivas
1,7101 gold badge13 silver badges20 bronze badges
1
- Here is exactly what you need: stackoverflow./questions/926916/… – Ivan Chernykh Commented May 5, 2013 at 6:01
3 Answers
Reset to default 6When you append the iframe in Firefox it starts loading about:blank
in the iframe, asynchronously. Then you touch the document, so it has to synchronously create an about:blank
document in there, and you modify it. Then the async load pletes and replaces the document you modified.
Waiting for the load event on the frame to fire before modifying it would work.
try this:
ifr.contentDocument.getElementsByTagName('body')[0].style.cssText = (
'margin: 0px;' +
'padding: 0px;' +
'height: 100%;' +
'width: 100%;');
also
<div id="divid"> </div>
To expand on Boris' answer, Here be Dragons!
Although this will get Firefox working, it will currently also break Chrome, Opera, Safari, and all their mobile counterparts, as they will NOT throw the 'onLoad' event as on these platforms, the iframe sits idle until used.
You will need to make this a Firefox only exception. Luckily due to Firefox's frustrating lack of features, its pretty easy to tell them apart. Just look for something the rest of the internet has but Firefox does not, or vice versa.
if(typeof InstallTrigger !== 'undefined'){ //if ie, wait sorry, firefox
iframe.onload = function(){
iframe.contentDocument.body.appendChild(contents);
}
}else{
iframe.contentDocument.body.appendChild(contents);
}
Firefox, The new Internet Explorer
本文标签: javascriptbehavior of iframe contentDocument in chrome and firefoxStack Overflow
版权声明:本文标题:javascript - behavior of iframe contentDocument in chrome and firefox - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745414683a2657627.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论