WordPress.org

Make WordPress Core

Opened 2 months ago

Last modified 2 months ago

#53619 new defect (bug)

Twenty Twenty-One: JS Error for Anchors that load custom modals or react hash routes

Reported by: rixeo Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Bundled Theme Keywords:
Focuses: javascript Cc:

Description

When using hash urls that load custom modals or react hash routes in the front-end from the primary navigation, the script expects an anchor to exist for the hash.
The error reproduced is:

Uncaught TypeError: anchor is null
    navMenu /wp-content/themes/twentytwentyone/assets/js/primary-navigation.js?ver=1.3:165

The code in specific is :

setTimeout(function () {
  var anchor = document.getElementById(event.target.hash.slice(1));
  anchor.scrollIntoView();
}, 550);

Which should be changed to check if the anchor actually exists as :

setTimeout(function () {
  var anchor = document.getElementById(event.target.hash.slice(1));
  if ( anchor ) {
     anchor.scrollIntoView();
  }
}, 550);

Change History (1)

#1 @rixeo
2 months ago

  • Summary changed from JS Error for Anchors that load custom modals or react hash routes to Twenty Twenty-One: JS Error for Anchors that load custom modals or react hash routes
Note: See TracTickets for help on using tickets.