check href javascript 1

check href javascript

// CHECK HREF
const navLinks = document.querySelector(".nav_link");
const navLink = navLinks.getAttribute("href");
const browserHref = window.location.href; // browserHref.search(navLink)

function sliceLink() {
   if (browserHref.includes(navLink)) {
     let searchLink = browserHref.search(navLink); // get position
     let slicedString = browserHref.slice(searchLink); // result
     return slicedString;
   }
}

Here is what the above code is Doing:
1. It’s getting the href attribute of the nav_link class.
2. It’s getting the href of the browser.
3. It’s checking if the browser href includes the nav_link href.
4. If it does, it’s getting the position of the nav_link href in the browser href.
5. It’s slicing the browser href from the position of the nav_link href.
6. It’s returning the sliced string.

Similar Posts