check href javascript 1

check href javascript

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

function sliceLink() {
   if (browserHref.includes(navLink)) {
     let searchLink =; // 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