The pathname
property sets or gets the path
name part of the href attribute value.
pathname |
Yes | Yes | Yes | Yes | Yes |
Return the pathname property:
var v = anchorObject.pathname
Set the pathname property:
anchorObject.pathname = path;
Value | Description |
---|---|
path | Set the path name of a URL |
A String representing the path name of the URL.
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:
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: