Georgi Angelov
Georgi Angelov

Reputation: 4388

window.body.scrollHeight does not change when decreasing height

So I am trying to create a way to detect when the body resizes and then I grab the new height and send it through a postmessage. The not correctly functioning part is when I downsize the body ( when I removed(hide) some elements so the body shrunk). This does not get detected as the window.body.scrollHeight simply holds the last value. Here is my code and I would appreciate any suggestions:

  sendMessage();
  checkDocumentHeight(sendMessage);
  function sendMessage(){
    var heightWidth = document.body.scrollHeight+ " " +document.body.scrollWidth;
    parent.postMessage(heightWidth, "*");
  }
  function checkDocumentHeight(callback){
      var lastHeight = document.body.scrollHeight, newHeight, timer;
      (function run(){
          newHeight = document.body.scrollHeight;
          if( lastHeight != newHeight )
              callback();
          lastHeight = newHeight;
          timer = setTimeout(run, 1000);
      })();
  }

Upvotes: 2

Views: 3634

Answers (1)

Michael Tempest
Michael Tempest

Reputation: 834

scrollHeight with not change, you want to use the innerHeight. scrollHeight is basically the same as document height. It's the height of the scrollbar.

window.innerHeight

http://jsfiddle.net/gUUYc/4/

Upvotes: 1

Related Questions