JavaScript location.hostname

location.hostname returns the hostname of the current page. It is a property, not a function, so do not add parentheses.

const hostname = location.hostname;
console.log(hostname);

For this page, the current value is:

hostname vs host Top ↑

hostname contains only the hostname. host can also contain an explicit port.

console.log(location.hostname);
console.log(location.host);

Original source pattern and modern replacement Top ↑

The older example used document.write(). For normal page output, update an existing element instead.

const output = document.getElementById('hostnameOutput');
output.textContent = location.hostname;

JavaScript location Full URL Refresh the page




Subscribe to our YouTube Channel here



plus2net.com










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer