English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Location pathname attribute

JavaScript Location  Object

pathnameSets or returns a URL's pathname.

The pathname is a string that starts with the hostname, containing an initial "/Followed by the URL path.

Syntax:

Return the pathname attribute:

location.pathname

Set the pathname attribute:

location.pathname = path
var x = location.pathname;
document.querySelector("#output").innerHTML = x;
Test and See‹/›

Browser compatibility

All browsers fully support the pathname attribute:

Attribute
pathnameIsIsIsIsIs

Attribute value

ValueDescription
pathA string specifying the URL path

Technical details

Return value:A string representing the URL path; if there is no path, it is an empty string

More examples

This example displays all location attributes:

var txt = "";
txt += "<p>Host: " + location.host + "</p>";
txt += "<p>Hostname: " + location.hostname + "</p>";
txt += "<p>Href: " + location.href + "</p>";
txt += "<p>Origin: " + location.origin + "</p>";
txt += "<p>Pathname: " + location.pathname + "</p>";
txt += "<p>Protocol: " + location.protocol + "</p>";
txt += "<p>Search: " + location.search + "</p>";
document.write(txt);
Test and See‹/›

Related References

Location Reference:location.href property

Location Reference:location.host property

Location Reference:location.origin property

Location Reference:location.protocol property

JavaScript Location  Object