[wp-trac] [WordPress Trac] #53619: JS Error for Anchors that load custom modals or react hash routes

WordPress Trac noreply at wordpress.org
Wed Jul 7 13:43:57 UTC 2021


#53619: JS Error for Anchors that load custom modals or react hash routes
---------------------------+-----------------------------
 Reporter:  rixeo          |      Owner:  (none)
     Type:  defect (bug)   |     Status:  new
 Priority:  normal         |  Milestone:  Awaiting Review
Component:  Bundled Theme  |    Version:
 Severity:  normal         |   Keywords:
  Focuses:  javascript     |
---------------------------+-----------------------------
 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);
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/53619>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list