window.location.hash上的语法帮助

时间:2011-03-18 17:41:25

标签: syntax hash

我正在寻找正确的语法来检测特定的哈希值以触发函数。类似的东西:

if(window.location.hash ='this'){ 做这个 }

感谢。我已成功使用以下方法检测未指定的哈希,但我希望更简洁。

if(!! window.location.hash){ 做这个 }

1 个答案:

答案 0 :(得分:1)

window.location.hash返回“#this”,因此您需要在执行字符串比较之前删除哈希字符。

这是我用过的......

var hash = escape(window.location.hash.replace( /^#/, '')); // escape used to prevent injection attacks

if (hash == 'this') {
    doSomethingWithThis();
}
相关问题