When the hash has been changed, get the URL we navigated away from:
event.oldURL;
Click the button to change the anchor part of the current URL to #part5.
The oldURL property returns the URL of the document we navigated away from.
The newURL property returns the URL we are navigating to.
<!DOCTYPE html> <html> <body onhashchange="myFunction(event)"> <button onclick="changePart()">Test</button> <p id="demo"></p> <script> // Using the location.hash property to change the anchor part function changePart() {//from w w w . ja v a2 s.com location.hash = "part5"; } function myFunction() { document.getElementById("demo").innerHTML = "Previous URL: " + event.oldURL + "<br>New URL: " + event.newURL; } </script> </body> </html>
The oldURL property returns the URL of the document, before the hash was changed.
This is the URL that was navigated away from.
To get the URL that was navigated to, use the newURL property.
This property is read-only.
To set or return the hash of a URL, use the location.hash property.