The JavaScript location pathname property is used to get the pathname of the current URL. It does not take any arguments and returns a string value representing the pathname of the URL.
Here are some examples of how the pathname property can be used in JavaScript code:
- To get the pathname of the current URL, you can use the following code:
// get the pathname of the current URL
const pathname = location.pathname;
- To log the pathname of the current URL to the console, you can use the following code:
// log the pathname of the current URL to the console
console.log('Current URL pathname:', location.pathname);
- To compare the pathname of the current URL with a certain value, you can use the following code:
// define the pathname to compare
const pathname = '/path/to/page';
// compare the pathname of the current URL with the specified value
if (location.pathname === pathname) {
// do something...
}
Overall, the pathname property provides a convenient way to get the pathname of the current URL. It can be useful for tasks such as checking the current page or redirecting to a different page, or for other operations that require accessing the pathname of the URL.
Leave a Reply