I've noticed some strange behaviour in JS
Any help?
Answer is:
window.location.hash = '';
var hash = window.location.hash;
alert(hash + ' = ' + hash.length);
//outputs: ' = 0'
window.location.hash = '#';
var hash = window.location.hash;
alert(hash + ' = ' + hash.length);
//outputs: ' = 0'
window.location.hash = '_';
hash = window.location.hash;
alert(hash + ' = ' + hash.length);
//outputs: '_ = 2'
basically I want to trigger three conditions - no hash
- just hash
- hash with text
Any help?
Answer is:
- Once the hash is set, you cannot remove it altogether (eg, remove the
#
sign) without causing a page reload; this is normal behavior. - Setting an empty/null hash and setting the hash to the default hash (
#
) are treated the same; this is just internal behavior. Not sure if all browsers handle it consistently, but IIRC that is the case.
document.location.href = document.location.href
, to reload the page (window.location.reload()
would keep the hash).
0 comments:
Post a Comment