We're calling on all EU-based Mozillians with iOS or iPadOS devices to help us monitor Apple’s new browser choice screens. Join the effort to hold Big Tech to account!

This site will have limited functionality while we undergo maintenance to improve your experience. If an article doesn't solve your issue and you want to ask a question, we have our support community waiting to help you at @FirefoxSupport on Twitter and/r/firefox on Reddit.

Search Support

Avoid support scams. We will never ask you to call or text a phone number or share personal information. Please report suspicious activity using the “Report Abuse” option.

Rohkem teavet

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

Muudetud cor-el poolt

All Replies (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.

Muudetud cor-el poolt

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.

Muudetud cor-el poolt

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

}