protect against scrolling to non-existent DOM elements
and now for the correct code to prevent scrolling to non-existent DOM elements