0

I want to write a function that will display the path in the Browser Console when I click on a link of the menu on the sub-category, the menu looks like this (https://www.sephora.fr/) in this e-commerce website

For example : Parfum => ForMen => Cologne

How to get the path when I click on Cologne

Thanks

David Jay
  • 345
  • 1
  • 2
  • 15
  • Like, breadcrumbs in the console log? – Odiin Apr 06 '21 at 06:42
  • Does this answer your question? [How can I get pathname values from url in JavaScript?](https://stackoverflow.com/questions/59966711/how-can-i-get-pathname-values-from-url-in-javascript) – Masood Apr 06 '21 at 07:32

2 Answers2

2

If you're just looking to get the path without the domain name, you can use this:

function getUrlWithoutDomain() {
   return window.location.pathname;
}

If the domain name is also important, this will get you the full url:

function getFullUrl() {
   return window.location.href;
}
Ran Turner
  • 14,906
  • 5
  • 47
  • 53
1

Not sure if this is what you are looking for, but this will return the relative path for the website whenever an anchor tag is clicked on.

const anchors = document.querySelectorAll('a');

anchors.forEach(el => el.addEventListener('click', event => {
  console.log(event.target.getAttribute("href"));
}));

Also check out JavaScript click event listener on class for a similar question.

Will
  • 122
  • 1
  • 9
  • 1
    Side note: this is not a persistent solution, since one you leave the page you click on, you would have to execute this code in the browser console again. – Will Apr 06 '21 at 06:40