6

I have this url http://localhost:8080/?london and it needs to load to the id of london in <section id="london">.- which is on the page. I cannot use http://localhost:8080/#london, even though that will work!

To make things more complicated I am using a vue framework and they want the code inside master-origin/src/assets/js/mixins/Anchor.js - therefore I can't install jquery, has to be vanilla js.

I have tried

var el = [];
el = window.location.href.split("/?")[1];
console.log("el: " + el);
el.scrollIntoView();

Which grabs the value after the /?, but I when I tried to scrollIntoView, I get this message in the console

TypeError: el.scrollIntoView is not a function at VM10022 IE work:4

Why? I am following w3 schools guide https://www.w3schools.com/jsref/met_element_scrollintoview.asp

artworkjpm
  • 1,255
  • 2
  • 19
  • 40

3 Answers3

17

You try to call .scrollIntoView() on a string.

Try this: document.getElementById(el).scrollIntoView();

Son Tr.
  • 776
  • 6
  • 18
9

Works great for me

const el = document.getElementById('item');
      el.scrollIntoView({behavior: "smooth"});
1
  window.scrollTo({
        top: document.getElementById("allCars").offsetTop,
        left: 0,
        behavior: "smooth",
      });
Caro Pérez
  • 476
  • 4
  • 4