title: Prevent complete JS blackout when my function throws an error?
body: <p>I just don't know how to solve this altough it's probably fairly simple.
  I wrote a custom scroll function that simply scrolls the entire page to a certain position of an element on the page.</p>
  
  <pre><code>function scroll(selector, animate, viewOffset) {
  
          pageOffset = selector.offset();
          scrollPos = pageOffset.top - viewOffset;
  
          if ( animate ) {
              $('html, body').animate({ scrollTop : scrollPos + 'px' }, {
                  duration: '500',
                  easing: 'easeInOutExpo'
              });
          } else {
              $('html, body').scrollTop( scrollPos );
          }
  
      }
  </code></pre>
  
  <p>So I call the function like that … <code>scroll($('#something'), true, 30);</code> 
  This function works really fine, however if a <code>selector</code> is passed along that doesn't exist the function of course throws the following error …</p>
  
  <pre><code>pageOffset = selector.offset();
  scrollPos = pageOffset.top - viewOffset;
  </code></pre>
  
  <blockquote>
    <p>Uncaught TypeError: Cannot read property 'top' of null</p>
  </blockquote>
  
  <p>If that error occurs none of my JS does work anymore.</p>
  
  <p>How can I solve that problem so when I pass along a none existing <code>selector</code> the function doesn't throw an error or just doesn't get fired?</p>
  
  <p>Any ideas, thank you!</p>