I am having a problem, on a blog I have this javascript code below that is used to get the Position of a DIV id author-info. The problem is this div is only on actual article pages and not on any of the directory pages or the index.
So on these pages that do not have the div I get this error
Uncaught TypeError: Cannot read property 'top' of null
And here is the javascript causing the error
var commentsPos = $('#author-info').offset().top - $(window).height();
I know I could do this in the backend to only show that line of JS on the proper pages but I would like to avoid that as this is just 1 line of a much bigger javascript file that is included into the page.
What are my options here? Any help please, I assume I need to somehow check to see if this div exist on the page before calling this action?
You could check if the DOM element exists and then do your jQuery