This code is used on the website for smooth scrolling along an anchor.

$('a[href*="#"]') .not('[href="#"]') .not('[href="#0"]').on('click', function(event) { if ($(this).closest('ul').hasClass('story-anchors')) { var windowHeight = $(window).height(), countWindows = $(this).attr("data-windowCount"); clickAnchor(this, 0, 0, 1200, 0, windowHeight, countWindows); } else if ($(this).closest('ul').hasClass('anchors-list')) { var navHeight = $('.anchors-list').outerHeight() - 2; clickAnchor(this, navHeight, 1, 500, 1, 0, 0); } else { clickAnchor(this, 0, 1, 500, 1, 0, 0); } }); function clickAnchor(ob, heightNav, attrHeaderHeight, speed, attrTop, windowHeight, countWindows) { event.preventDefault(); var headerHeight = $('.header').height(); $('html, body').animate({ scrollTop: (windowHeight * countWindows) + ($($.attr(ob, 'href')).offset().top * attrTop) - (headerHeight * attrHeaderHeight) - heightNav }, speed); } 

On one of the pages is a link to the anchor, which is located on another page. When clicking on such a link in the console, it knocks out:

Uncaught TypeError: Cannot read property 'top' of undefined

The function in this case uses the last else.

Is this somehow fixed?

Raised to the main page by a community spirit member 9 hours ago .

This question contains answers that can be both good and bad; the system offered them for verification.

    2 answers 2

    In general, I rendered the event object to an external function - everything seems to be working

     $('a[href*="#"]') .not('[href="#"]') .not('[href="#0"]').on('click', function(event) { if ($(this).closest('ul').hasClass('story-anchors')) { event.preventDefault(); var windowHeight = $(window).height(), countWindows = $(this).attr("data-windowCount"); clickAnchor(this, 0, 0, 1200, 0, windowHeight, countWindows); } else if ($(this).closest('ul').hasClass('anchors-list')) { event.preventDefault(); var navHeight = $('.anchors-list').outerHeight() - 2; clickAnchor(this, navHeight, 1, 500, 1, 0, 0); } else { clickAnchor(this, 0, 1, 500, 1, 0, 0); } }); function clickAnchor(ob, heightNav, attrHeaderHeight, speed, attrTop, windowHeight, countWindows) { var headerHeight = $('.header').height(); $('html, body').animate({ scrollTop: (windowHeight * countWindows) + ($($.attr(ob, 'href')).offset().top * attrTop) - (headerHeight * attrHeaderHeight) - heightNav }, speed); } 
    • here the word "like" is the key. You did not fix what did not work. - Igor
     function clickAnchor(ob, heightNav, attrHeaderHeight, speed, attrTop, windowHeight, countWindows) { // откуда здесь берется объект event? //event.preventDefault(); var headerHeight = $('.header').height(); // проверьте, что количество элементов - один, и jQuery может вычислить его положение console.log($.attr(ob, 'href'), $($.attr(ob, 'href')).length, $($.attr(ob, 'href')).offset()); if ($($.attr(ob, 'href')).offset()) { $('html, body').animate({ scrollTop: (windowHeight * countWindows) + ($($.attr(ob, 'href')).offset().top * attrTop) - (headerHeight * attrHeaderHeight) - heightNav }, speed); } } 
    • on .length > 0 you can not check - Grundy