您可以使用从withing JavaScript中location.hash(URL的#后的部分)设置 “硬链接”。这不会导致页面重新加载,就像更改location.href一样,但是您可以获取该值并在JavaScript中使用它。
考虑这个例子:
Hash testwindow.onload = function() {
// The substring(1) removes the # from the hash.
var hash = window.location.hash.substring(1);
// Use a default value if no has was passed.
if(hash == '') {
hash = 'Nothing';
}
// Use the value.
document.getElementById('selection').innerHTML = hash;
}
/** Sets the hash and updates the page value */
function setHash(newHash) {
window.location.hash = newHash;
document.getElementById('selection').innerHTML = newHash;
}
Page 1 |
Page 2 |
Page 3
You have selected: ...