为提升您的使用体验,本站正在维护,部分功能暂时无法使用。如果本站文章无法解决您的问题,您想要向社区提问的话,请到 Twitter 上的 @FirefoxSupport 或 Reddit 上的 /r/firefox 提问,我们的支持社区将会很快回复您的疑问。

搜索 | 用户支持

防范以用户支持为名的诈骗。我们绝对不会要求您拨打电话或发送短信,及提供任何个人信息。请使用“举报滥用”选项报告涉及违规的行为。

详细了解

Firefox 6 and Javascript: parent.document.getElementById('name').contentDocument.height not working

more options

In my site I have a Javascript function that resizes the iframe. The part related to Firefox is the following:

var fr2 = parent.document.getElementById('name');
if(fr2.contentDocument) {
  var doc1 = fr2.contentDocument;
  h = doc1.height + 60;
  if (h < 280)
    fr2.style.height = "280px";
  else
    fr2.style.height = h + "px"; 
 }

With Firefox 6 this does not work anymore. In particular, if I print the variable "h" I obtain NaN. Do you know what is changed?

Thanks, Fabrizio

In my site I have a Javascript function that resizes the iframe. The part related to Firefox is the following:<br /> <br /> <pre><nowiki>var fr2 = parent.document.getElementById('name'); if(fr2.contentDocument) { var doc1 = fr2.contentDocument; h = doc1.height + 60; if (h < 280) fr2.style.height = "280px"; else fr2.style.height = h + "px"; } </nowiki></pre> With Firefox 6 this does not work anymore. In particular, if I print the variable "h" I obtain NaN. Do you know what is changed? Thanks, Fabrizio

由cor-el于修改

所有回复 (3)

more options

Hi Fabrizio,

Today I had the same problem and I did it to resolve: 1) Remove the http:// from the src=".." of tag iframe. Now object contentDocument going to exists. 2) IF you can't edit the main page which contains iframe, you can do this:

- on src of iframe page, insert:

<script type="text/javascript">
window.onload = function(){
      top.document.getElementById('blockrandom').contentDocument['height'] = document.getElementsByTagName("body")[0].offsetHeight;
}
</script>
  • Change "blockrandom" to your id iframe.

But, If you can edit the main page, you can use this method to get height of your page :

document.getElementById('blockrandom').contentDocument.body.scrollHeight

Where, again blockrandom is your id iframe.

I hope I have helped.

BR.

由cor-el于修改

more options

Sorry, I forgot in the 'document.getElementsByTagName("body")[0].offsetHeight' to work you need set CSS tag body with height, like this:

<style type="text/css">
 body {
	height: 1000px;
 }
</style>

BR.

由cor-el于修改

more options

Hi and thanks for your reply! I can post my solution, that is a related directly to my problem.

The solution can be accessing the style of the body, parsing the height or the width (in fact, now they are no more a number like ‘60’, but something like ‘60px’) and use it. As an example, if the JavaScript code was:

var fr2 = parent.document.getElementById('name'); var doc1 = fr2.contentDocument; var h = doc1.height + 60; fr2.style.height = h + "px";

Now you have to do something like this (I am too verbose to simplify the comprehension):

var fr2 = parent.document.getElementById('name'); var doc1 = fr2.contentDocument; var body = doc1.getElementsByTagName('body')[0]; var h = parent.window.getComputedStyle(body, null).height; var length = h.length; h = h.substring(0, length-2); //remove px from the string h = parseFloat(h) + 60; fr2.style.height = h + "px";

Anyway, this does not work with previous versions, so you need an IF clause to check compatibility:

if (doc1.height) {

 OLD CODE

} else {

 NEW CODE

}