You can use location.hash(the part of the URL after #) to set up "hard links" using JavaScript. This does not reload the page as a change location.href, but you can get the value and use it in JavaScript.
Consider the following example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head>
<title>Hash test</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
window.onload = function() {
var hash = window.location.hash.substring(1);
if(hash == '') {
hash = 'Nothing';
}
document.getElementById('selection').innerHTML = hash;
}
function setHash(newHash) {
window.location.hash = newHash;
document.getElementById('selection').innerHTML = newHash;
}
</script>
</head>
<body>
<div>
<div>
<a href="javascript: void();" onclick="setHash('page1');">Page 1</a> |
<a href="javascript: void();" onclick="setHash('page2');">Page 2</a> |
<a href="javascript: void();" onclick="setHash('page3');">Page 3</a>
</div>
<div>
You have selected: <span id="selection">...</span>
</div>
</div>
</body>
</html>
When you click on one of the page links, it changes location.hash, the browser URL is updated and the value used on the page changes. If the user then copies the URL directly from the address bar or bookmarks, the selected page will reload when the URL is requested again.
source
share