Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7170247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:10:22+00:00 2026-05-28T15:10:22+00:00

I just don’t know how to solve this altough it’s probably fairly simple. I

  • 0

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.

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 );
        }

    }

So I call the function like that … scroll($('#something'), true, 30);
This function works really fine, however if a selector is passed along that doesn’t exist the function of course throws the following error …

pageOffset = selector.offset();
scrollPos = pageOffset.top - viewOffset;

Uncaught TypeError: Cannot read property ‘top’ of null

If that error occurs none of my JS does work anymore.

How can I solve that problem so when I pass along a none existing selector the function doesn’t throw an error or just doesn’t get fired?

Any ideas, thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:10:22+00:00Added an answer on May 28, 2026 at 3:10 pm

    use a try/catch block

    try {
     pageOffset = selector.offset();
    scrollPos = pageOffset.top - viewOffset;
    } catch(e) {
      //error objects typically contain a .name & .message property
      var name = "";
      if (e.hasOwnProperty("name") ) {
        name = e.name; //super-defensive 
      }
    
      var message = "";
      if (e.hasOwnProperty("message") ) {
        message = e.message; //super-defensive 
      }
    
      console.log("Error occurred - " + name + " [" + message + "]");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably quite simple, but I just don't know how to do this...
I'm sure this is simple but I just don't know how to do it...
I just don't know how to explain this clearly. So I create a simple
Maybe I am over complicating this in my head but I just don't know
Coming from C#, I just don't get this 'throws exception' that is written after
This algorithm is so advanced for my basic programming skills that I just don't
I just don't know why i get this error: 2010-10-28 16:32:29,925 ERROR [ache.commons.digester.Digester.error :1635]
I just don't know that where the variable used in some method has gone
I just don;t know how to do this. I want more flexibility to edit
Ok this has been become sooo confusing to me. I just don't know what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.