title: Javascript Error Uncaught TypeError: Cannot read property 'top' of undefined
body: <p>i was coding my page and i use a template. I don't understand javascript very well so im getting this error only when i go to portfolio.html page and switch to the mobile view. in the index.html is not showing any error. index page uses the nav links like:  <code>&lt;li&gt;&lt;a href="#portfolio-section" class="nav-link"&gt;Portfolio&lt;/a&gt;&lt;/li&gt;</code> and works right. in the portfolio page navlinks i wanted to go to other file like this:<code>&lt;li&gt;&lt;a href="index.html" class="nav-link"&gt;Inicio&lt;/a&gt;&lt;/li&gt;</code>.</p>
  
  <p>my code give this error:</p>
  
  <blockquote>
    <p>Javascript Error Uncaught TypeError: Cannot read property 'top' of
    undefined</p>
  </blockquote>
  
  <p>any tips to solve?</p>
  
  <p>my html:</p>
  
  <pre><code>&lt;html lang="pt-br"&gt;
  &lt;head&gt;
    &lt;title&gt;Nova Pintura&lt;/title&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"&gt;enter code here
  
    &lt;link href="https://fonts.googleapis.com/css?family=Work+Sans:400,700,900&amp;display=swap" rel="stylesheet"&gt;
  
    &lt;link rel="stylesheet" href="css/bootstrap.min.css"&gt;
    &lt;link rel="stylesheet" href="css/jquery-ui.css"&gt;
    &lt;link rel="stylesheet" href="css/owl.carousel.min.css"&gt;
    &lt;link rel="stylesheet" href="css/owl.theme.default.min.css"&gt;
    &lt;link href="https://fonts.googleapis.com/css?family=Dosis|Oswald&amp;display=swap" rel="stylesheet"&gt;
    &lt;link rel="stylesheet" href="css/jquery.fancybox.min.css"&gt;
    &lt;link rel="stylesheet" href="css/bootstrap-datepicker.css"&gt;
    &lt;link rel="stylesheet" href="fonts/flaticon/font/flaticon.css"&gt;
    &lt;link rel="stylesheet" href="fonts/icomoon/style.css"&gt;
    &lt;link rel="stylesheet" href="css/aos.css"&gt;
    &lt;link rel="stylesheet" href="css/style.css"&gt;
  
  &lt;/head&gt;
  
  &lt;body data-spy="scroll" data-target=".site-navbar-target" data-offset="300"&gt;
  
    &lt;div class="site-wrap"&gt;
  
      &lt;div class="site-mobile-menu site-navbar-target"&gt;
        &lt;div class="site-mobile-menu-header"&gt;
          &lt;div class="site-mobile-menu-close mt-3"&gt;
            &lt;span class="icon-close2 js-menu-toggle"&gt;&lt;/span&gt;
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="site-mobile-menu-body"&gt;&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  
      &lt;header class="site-navbar py-4 js-sticky-header site-navbar-target" role="banner"&gt;
  
        &lt;div class="container"&gt;
          &lt;div class="row align-items-center"&gt;
            &lt;div class="col-6 col-xl-2"&gt;
              &lt;h1 class="mb-0 site-logo"&gt;
                &lt;a href="index.html"&gt;
                  &lt;img src="images/logo/np.png" width="200" height="55" alt=""&gt;&lt;/img&gt;
                &lt;/a&gt;
              &lt;/h1&gt;
  
            &lt;/div&gt;
  
            &lt;div class="col-12 col-md-10 d-none d-xl-block"&gt;
              &lt;nav class="site-navigation position-relative text-right" role="navigation"&gt;
                &lt;ul class="site-menu main-menu js-clone-nav mr-auto d-none d-lg-block"&gt;
                  &lt;li&gt;&lt;a href="index.html" class="nav-link"&gt;Inicio&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="index.html#about-section" class="nav-link"&gt;Sobre Nós&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="portfolio.html" class="nav-link active"&gt;Portfolio&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="index.html#services-section" class="nav-link"&gt;Serviços&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="index.html#contact-section" class="nav-link"&gt;Contato&lt;/a&gt;&lt;/li&gt;
                &lt;/ul&gt;
              &lt;/nav&gt;
            &lt;/div&gt;
  
            &lt;div class="col-6 d-inline-block d-xl-none ml-md-0 py-3" style="position: relative; top: 3px;"&gt;&lt;a href="#"
                class="site-menu-toggle js-menu-toggle float-right"&gt;&lt;span class="icon-menu h3"&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>the js function that is giving error :</p>
  
  <pre><code>// navigation
    var OnePageNavigation = function() {
      var navToggler = $('.site-menu-toggle');
      $("body").on("click", ".main-menu li a[href^='#'], .smoothscroll[href^='#'],.main-menu li a[href^='index.html'], .smoothscroll[href^='index.html'], .site-mobile-menu .site-nav-wrap li a", function(e) {
        e.preventDefault();
  
        var hash = this.hash;
  
        $('html, body').animate({
          'scrollTop': $(hash).offset().top
        }, 300, 'easeInOutExpo', function(){
          window.location.hash = hash;
        });
  
      });
    };
    OnePageNavigation();
  
    var siteScroll = function() {
  
  
  
      $(window).scroll(function() {
  
          var st = $(this).scrollTop();
  
          if (st &gt; 100) {
              $('.js-sticky-header').addClass('shrink');
          } else {
              $('.js-sticky-header').removeClass('shrink');
          }
  
      }) 
  
    };
  </code></pre>