开发者

Syntax help on window.location.hash

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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜