javascript - behavior of iframe contentDocument in chrome and firefox - Stack Overflow

Here is my testing.html:<html><meta charset="utf-8"><script>window.onloa

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
Add a ment  | 

3 Answers 3

Reset to default 6

When 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745414683a4626691.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信