JavaScript Location Hostnames and URL Examples

Sam Deering
Sam Deering
Share

Simple JavaScript code snippets to manipulate location URLs (you know that weird thing in the address bar!) to get the host, hostname, pathname, protocol, port and the regular expression to check if a string is a hostname.

console.log(window.location.href);
//output: https://www.jquery4u.com/javascript/javascript-location-hostnames-url-examples/

console.log(window.location.hostname);
//output: www.jquery4u.com
 
console.log(window.location.host);
//output: www.jquery4u.com
 
console.log(window.location.pathname);
//output: /javascript/javascript-location-hostnames-url-examples/
 
console.log(window.location.protocol);
//output: https:
 
console.log(window.location.port);
//output: (an empty string)
 
//regular expression to check if a string is a hostname.
console.log(window.location.hostname.match(/^.*?-?(w*)./)[1]);
//output: www
Tip: If you just type in window.location into firebug you can analyse all of the above.

Frequently Asked Questions (FAQs) about JavaScript Location, Hostnames, and URL Examples

What is the difference between location.host and location.hostname in JavaScript?

In JavaScript, both location.host and location.hostname are used to get the hostname of a URL. However, there is a slight difference between the two. The location.host property returns the hostname and port of a URL, if it exists. On the other hand, location.hostname only returns the hostname. For example, if the URL is http://www.example.com:8000, location.host will return www.example.com:8000, while location.hostname will return www.example.com.

How can I use the location object in JavaScript?

The location object in JavaScript is a part of the window object and is accessed through the window.location property. This object can be used to get the current page address (URL) and to redirect the browser to a new page. It contains information about the current URL and several methods for manipulating it.

What is the purpose of location.href in JavaScript?

The location.href property in JavaScript is used to get or set the entire URL of the current page. It is a string that represents the entire URL, including the protocol (http or https), hostname, port (if specified), path, query string, and fragment identifier. You can also use location.href to redirect the browser to a new page.

How can I get the protocol of a URL using JavaScript?

You can get the protocol of a URL in JavaScript using the location.protocol property. This property returns the web protocol used (http: or https:) as a string. For example, if the URL is http://www.example.com, location.protocol will return http:.

What is the use of location.pathname in JavaScript?

The location.pathname property in JavaScript is used to get or set the path portion of a URL. This includes everything after the hostname and before the query string or fragment identifier. For example, if the URL is http://www.example.com/path/name, location.pathname will return /path/name.

How can I redirect to a new page using JavaScript?

You can redirect to a new page in JavaScript using the location.href or location.replace() method. Setting location.href to a new URL will redirect to that URL and add it to the browser’s history. On the other hand, location.replace() replaces the current document with a new one in the same browser window and does not save the original document in the history.

How can I reload the current page using JavaScript?

You can reload the current page in JavaScript using the location.reload() method. This method reloads the current URL, similar to the browser’s refresh button. You can pass a boolean parameter to this method. If the parameter is true, it causes the page to always be reloaded from the server. If it is false or not specified, the page may be reloaded from the browser’s cache.

What is the difference between location.search and location.hash in JavaScript?

In JavaScript, location.search and location.hash are used to get the query string and fragment identifier of a URL, respectively. The location.search property returns the query string, including the leading question mark. For example, if the URL is http://www.example.com/?name=value, location.search will return ?name=value. On the other hand, location.hash returns the fragment identifier, including the leading hash sign. For example, if the URL is http://www.example.com/#section1, location.hash will return #section1.

How can I get the port of a URL using JavaScript?

You can get the port of a URL in JavaScript using the location.port property. This property returns the port number of the URL as a string. If the port number is not specified in the URL, this property returns an empty string.

What is the use of location.assign() in JavaScript?

The location.assign() method in JavaScript is used to load a new document. It takes one argument, which is the new URL to be loaded. This method works similarly to setting location.href to a new URL. However, unlike location.replace(), location.assign() adds the new URL to the browser’s history.