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