0

I have this base code on my website's home page (myexample.com):

setTimeout(function(){
  document.getElementById("pizza3").scrollIntoView( {behavior: 'smooth'} );
}, 2000);

Now as it stands, the scrolling effect will fire on every call to the home page (myexample.com). I actually want this script snippet to fire, AND ONLY FIRE, when the home page's url string explicitly reads: myexample.com#pizza3

How might the base code be modified to achieve?

Cheers!

CMR
  • 13
  • 5

1 Answers1

0

This should catch the last 7 digits of your URL and check them for the pizza tag:

let url = window.location.href;
let tagStr = url.substr(url.length - 7);
if (tagStr == '#pizza3') {

setTimeout(function(){
    document.getElementById("pizza3").scrollIntoView( {behavior: 'smooth'} );
}, 2000);
      }
jpmc
  • 1,147
  • 7
  • 18
  • I appreciate your willingness to answer, James (^5). But unfortunately, your modified code version did not produce the desired effect. Upon accessing myexample.com#pizza3, the page remained static, and did not automatically scroll to the page element with id="pizza3". Google Chrome's console revealed the following: **Uncaught SyntaxError: Unexpected identifier: if (tagStr == ''#pizza3') {** – CMR Dec 23 '19 at 12:24
  • Bummer! Still does not scroll to the tag element. The page remains at the top -- when the url string is defined with **myexample.com#pizza3** Thank you for trying, though.. :) – CMR Dec 23 '19 at 12:34
  • It works in my test example... What exactly are you typing into the URL bar? Obviously not myexample.com, can you copy paste what you are doing. – jpmc Dec 23 '19 at 12:39
  • You totally rock!! Thank you! It works like a champ.. *I performed a typo myself, lol... when entering the url string. – CMR Dec 23 '19 at 12:51