JavaScript location href property

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:

  1. 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;
  1. 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);
  1. 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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: