Javascript Reference - HTML DOM Anchor pathname Property








The pathname property sets or gets the path name part of the href attribute value.

Browser Support

pathname Yes Yes Yes Yes Yes

Syntax

Return the pathname property:

var v = anchorObject.pathname 

Set the pathname property:

anchorObject.pathname = path;

Property Values

Value Description
path Set the path name of a URL




Return Value

A String representing the path name of the URL.

Example

The following code shows how to gets the path name of a link:


<!DOCTYPE html>
<html>
<body>
<!--from w  ww .ja  v  a2s . co m-->
<p><a id="myAnchor" href="http://www.example.com:8080/test.htm#part2">Example link</a></p>
<button onclick="myFunction()">test</button>

<p id="demo"></p>

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

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the path name of a link.


<!DOCTYPE html>
<html>
<body>
<!-- w w  w .  jav a 2 s.  co m-->
<p><a id="myAnchor" href="http://www.example.com:80/test.htm#part2">Example link</a></p>
<button onclick="myFunction()">test</button>

<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("myAnchor").pathname = "newpathname";
    document.getElementById("demo").innerHTML = "changed";
}
</script>

</body>
</html>

The code above is rendered as follows: