I'd like to make the following code more generic/page-independent so that I can use it for other projects too.
The code is checking if a certain value in the localCache exists and if the location.pathname has a certain string. If so, it should scroll to top etc.
Now it looks like this:
if (referrer == "home" && location.pathname == "projects"){
window.scrollTo(0,0);
localStorage.setItem('referrer', 'projects');
}
I'd like to make more generic so that it's not depending on the exact amount of strings of the location.pathname and so that it can refer to an X amount of location.pathnames.
I tried to make an object out of the different strings like this:
let object = {
homePage: "home",
noScrollPages: {[
"projects",
"about",
"info"
]}
}
if (referrer == homePage && (location.pathname == "/" + noScrollPages[0]) || location.pathname == "/" + noScrollPages[1] || location.pathname == "/" + noScrollPages[2]){
window.scrollTo(0,0);
localStorage.setItem("referrer", noScrollPages[0, 1, 2 or X, whichever resolves as true in the if statement]);
console.log(localStorage.getItem("referrer"));
}
So, basically I don't know how code it so that it doesn't rely on the explicit declaration of [0] or [1], because it could be X number of pages.
Thanks for any tips!
Aucun commentaire:
Enregistrer un commentaire