2

I'm using this plugin to scroll a div - https://github.com/thomd/jquery-scroll I'm not using it quite as it's written because it's geared toward a fixed height div. The height of my div grows so therefore I have to call the repaint method when the div resizes.

The issue I'm running into is that I don't always need scrollbars when the page first loads but upon resizing the page I do. So, when the page loads I'm getting the error - Uncaught TypeError: Cannot call method 'repaint' of undefined

I know that if I want to test to see if an element exists I can do something like $('.selector').length > 0 but in this case I want to see if the scrollbar object exists. I tried $('.selector').scrollbar.length and I'm getting back 1. So, I'm not quite sure what's going on. Does the object exist and if so, why can't I call the repaint method? Or does it not exist and if not, why is it returning 1? Or am I testing for it incorrectly? Thanks.

geoff swartz
  • 5,437
  • 11
  • 51
  • 75

1 Answers1

0

Try this:

if(typeof $('.selector').scrollbar != 'undefined')

But it might also be an error with the library that shows when your selector is running empty then try this:

if($('.selector').length > 0) 
  $('.selector').scrollbar({}); //or whatever you want to do with scrollbar here
bardiir
  • 14,556
  • 9
  • 41
  • 66
  • Thanks. The typeof is coming back as a function so apparently the scrollbar object exists. Since it doesn't actually draw out the divs to create the scrollbar, I'm guessing that's what's causing the repaint function to not work. But, I'd think the function should be there to call regardless of whether the divs exist on the page. Thanks. – geoff swartz Jan 23 '12 at 16:39
  • Probably a bug in the plugin itself. I had several plugins myself already that did not handle the non-existance of selector objects very well. So if the selector didn't hit anything they would spit out errors instead of just doing nothing at all. – bardiir Jan 23 '12 at 16:45
  • A better approach is to test whether `typeof` is `function`, instead of `undefined`. The property could be defined, but not be a function, which still causes an error. – Rob W Jan 23 '12 at 16:53
  • Instead of catching errors you might just want to try to select the divs yourself and only call the function if they exist :) - It should not be your concern but the functions but that way you'll not mangle real errors when debugging the code. – bardiir Jan 23 '12 at 17:32