Using window.location.hash I get this:
http://example.com --> ''
http://example.com# --> ''
http://example.com#ahihah --> '#ahihah'
However, I need to distinguish between these two cases:
http://example.com --> ''
http://example.com# --> '#'
http://example.com#ahihah --> '#ahihah'
So when there is no hash in the url (first case) it's ok to have an empty string but I want to have the value '#' if there is the hash sign. Is this possible in JavaScript?
I already tried with this.
var hash = window.location.hash;
var href = window.location.href;
if (hash !== '') {
return link.hash;
}
if (href.slice(-1) === '#') {
return '#';
}
return '';
But looks like an error-prone method. Could be that a query string in the url ends with # and the function will fall into the second if returning '#' instead of ''?
#, that would mean there's a location hash after the querystring ?http://example.comand I go tohttp://example.com(e.g. clicking a link) the page will be reloaded while if I go tohttp://example.com#the page will scroll on top. I don't know if I answered your question? I just need to do two different things in the first and the second case.