Javascript Reference - Location pathname Property








The pathname property sets or gets the path of a URL.

Browser Support

pathname Yes Yes Yes Yes Yes

Syntax

Return the pathname property.

var v = location.pathname

Set the pathname property.

location.pathname=path

Parameter Values

Value Type Description
path String Set the URL path




Return Value

A String type value representing the path

Example

The following code shows how to get the path of the current URL.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--   w  w  w.j  a v a 2  s .  co  m-->
<p id="demo"></p>

<script>
function myFunction() {
    var x = location.pathname;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: