-7

Say we have a form that loads in an iframe on another site. Now There is a new version of it and has to match the same height as the previous form in order to fit perfectly on the sites where it was loaded.

How can you check, even using JavaScript, how much the height of your web page is? then I can check for both and compare. Like the number of vertical pixels your page has.

Edit

Ignore the iframe, just looking for the height of current html document, that's it.

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • Check the height attribute of the iframe - that's the space you have to work with. – Moob Oct 18 '13 at 11:16
  • Nope that isn't it. That's not the actual height of the content that's loaded inside – Hanky Panky Oct 18 '13 at 11:17
  • could you please use a bit of naming in your question (parent page p1, iframe i1, etc) so that it is easy to understand the context of each sentence. Did you tried scrollHeight property https://developer.mozilla.org/en-US/docs/Web/API/element.scrollHeight – gurvinder372 Oct 18 '13 at 11:17
  • Perhaps you could better clarify your requirements. – Moob Oct 18 '13 at 11:18
  • If you are trying to get the parent page details from its child which is in a different domain it is not possible. Because for security reasons accessing information between cross domains is not allowed. – Kaf Oct 18 '13 at 11:21
  • @Kaf nor do I intend to – Hanky Panky Oct 18 '13 at 11:23
  • Ah Okay, It is clear now with your edit. – Kaf Oct 18 '13 at 11:24

6 Answers6

4

Getting the height of the document can be done using these:

jQuery

$(document).height()

Javascript only (works cross browser)

function getDocHeight() {
    var D = document;
    return Math.max(
        D.body.scrollHeight, D.documentElement.scrollHeight,
        D.body.clientHeight, D.documentElement.clientHeight
    );
}



For Max(documentHeight, browserHeight) use:

jQuery

Math.max($(document).height(), $(browser).height());

Javascript only (works cross browser)

function getDocHeight() {
    var D = document;
    return Math.max(
        D.body.scrollHeight, D.documentElement.scrollHeight,
        D.body.offsetHeight, D.documentElement.offsetHeight,
        D.body.clientHeight, D.documentElement.clientHeight
    );
}
ediblecode
  • 11,701
  • 19
  • 68
  • 116
1

It is quite unclear from your question what you are actually asking. But if you are trying to get the height of the parents document, then you need to use:

var parents_height = parent.document.body.clientHeight;

This will return the height of the parent windows document. However, it will only return it if the iframe/popup is on the same domain.

If you are only interested in the current documents height, then use:

var height = document.body.clientHeight;

However, if you have manipulated the height of the body tag in CSS, tehn you can run into issues. You might be better off using jQuery like so:

$(document).height();
Ben Carey
  • 16,540
  • 19
  • 87
  • 169
0

try this

var _docHeight = (document.height !== undefined) ? document.height : document.body.offsetHeight;
    var _docWidth = (document.width !== undefined) ? document.width : document.body.offsetWidth;
0

how about scrollHeight property

https://developer.mozilla.org/en-US/docs/Web/API/element.scrollHeight

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
-1

You can use jQueries height function:

$(document).height();
R. Oosterholt
  • 7,720
  • 2
  • 53
  • 77
  • -1. This isn't guaranteed to be correct, its dependent on their styling. It's better to use $(document).height(); – ediblecode Oct 18 '13 at 11:24
-1

This simple line of javascript code will give you the answer

document.body.offsetHeight;

Remember you need to fetch it after the whole document is loaded

<script>
window.onload = function() {
    document.body.offsetHeight;
}
</script> 
matewka
  • 9,912
  • 2
  • 32
  • 43