views:

514

answers:

2

Is there a simple and reliable solution for detecting window vertical scrollbar appears/disappears?

window.onresize isn't triggered when after JavaScript DOM manipulation page becomes high enough for appearing scrollbar.

In this very similar post Detect if a page has a vertical scrollbar described solution how to detect whether scrollbar is present or not, but I need to know when exactly it appears.

Thanks.

+4  A: 

Window.onresize won't work, because the window isn't resizing.

body.onresize won't work, as resize is implemented only for windows and frames.

This question deals with the same issue. The top answerer has some interesting ideas, though none simple, or cross-browser.

I think this Jquery based approach by Rick Strahl is the best you can get: Monitoring Html Element CSS Changes in JavaScript it uses the browser's "watch" functions if available, or a timer if not. The latter is not very resource friendly but there seems to be no way around it.

A good way how to tell the scrollbar once the resize thing is resolved is in this question by the way, just in case you aren't referring to that one in your question.

Pekka
I have tried timer polling solution, but it looks ugly for me.body.onresize works in IE, but does not work in FireFox (maybe I am doing something wrongly). Thank you for such a quick answer.
Roman
A: 

this solved the problem for me.. http://benalman.com/projects/jquery-resize-plugin/