I am looking for the correct syntax to detect a specific hash value in order to fire a function. Something like:
if (window.location.hash = 'this') { Do This }
Thanks. I've have success with detecting an unspecified hash using the following, however I want to be more succinct.开发者_开发知识库
if (!!window.location.hash) { Do This }
window.location.hash returns "#this" so you need to remove the hash character before performing the string comparison.
Here's what I've used...
var hash = escape(window.location.hash.replace( /^#/, '')); // escape used to prevent injection attacks
if (hash == 'this') {
doSomethingWithThis();
}
精彩评论