Change the path name of a link:
document.getElementById("myAnchor").pathname = "newpathname";
Click the button to change the pathname of the link.
<!DOCTYPE html> <html> <body> <p><a id="myAnchor" href="http://www.java2s.com:80/index.html#part1">Example link</a></p> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {//from w w w .j av a 2 s.co m document.getElementById("myAnchor").pathname = "newpathname"; document.getElementById("demo").innerHTML = "The pathname was changed."; } </script> </body> </html>