scroll to id element when using "?" in url, vue.js
Asked Answered
N

3

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

Nord answered 17/11, 2018 at 14:25 Comment(0)
C
17

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

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

Chivers answered 17/11, 2018 at 14:35 Comment(0)
E
9

Works great for me

const el = document.getElementById('item');
      el.scrollIntoView({behavior: "smooth"});
Echinoid answered 16/8, 2020 at 19:13 Comment(0)
F
1
  window.scrollTo({
        top: document.getElementById("allCars").offsetTop,
        left: 0,
        behavior: "smooth",
      });
Fervidor answered 28/8, 2022 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.