Reputation: 4388
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
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
Upvotes: 1