javascriptvue.jsanchor-scroll

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


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


Solution

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

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