Prüfen, ob eine URL einen String aus einem Array enthält

Einfaches JS-Script, um zu prüfen, ob ein String aus einem Array Teil einer URL ist

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
let contains = false;

const array = ['imprint', 'data-protection', 'terms-of-use'];

const currentUrl = '/imprint'.toUpperCase();
// mit window.location.href ersetzen

array.forEach(function (item) {
    let currentItem = item.toUpperCase();

    if (currentUrl.includes(currentItem)) {
        contains = true;
    }
});

console.log(contains);

CodePen

Erstellt mit Hugo
Theme Stack gestaltet von Jimmy