Javascript 对象someUrl#someHash没有方法';包含';

Javascript 对象someUrl#someHash没有方法';包含';,javascript,google-chrome,dom,Javascript,Google Chrome,Dom,下面的代码在Chrome中给了我一个错误。似乎window.location.href不返回字符串,但这似乎很疯狂 代码如下: var theUrl = "" + window.location.href; var hashValue = theUrl.contains("#") ? theUrl.split('#')[1] : null; (This is line 6) 这会在Chrome中返回以下错误: Uncaught TypeError: Object someUrl#someHas

下面的代码在Chrome中给了我一个错误。似乎
window.location.href
不返回字符串,但这似乎很疯狂

代码如下:

var theUrl = "" + window.location.href;
var hashValue = theUrl.contains("#") ? theUrl.split('#')[1] : null; (This is line 6)
这会在Chrome中返回以下错误:

Uncaught TypeError: Object someUrl#someHash has no method 'contains' myFile.js:6

(anonymous function) faq.js:6
k jquery.min.js:2
l.fireWith jquery.min.js:2
p.extend.ready jquery.min.js:2
D
有什么想法吗


编辑:也尝试使用
document.URL
,但无效。

此时
String.contains
方法似乎仅受Firefox 19支持

该页面还提到了一些与MooTools不兼容的地方,也许您的问题与此相关。目前,您可以像这样检索哈希值

var hashValue = window.location.hash.substr(1) || null;

.indexOf
也可能有用,而不是
.contains

hashValue = theUrl.indexOf('#') > -1 ? ... : ...;

string对象没有名为“contains”的函数,但是您可以使用“indexOf”函数,如果在目标字符串中找到您感兴趣的字符串,则该函数将返回值>=0,否则为-1

还有一条注释:您可以使用window.location.hash获取哈希值,因此,您需要执行以下操作,而不是执行上述操作:


var hashValue=window.location.hash.substr(1)| | null

typeof(window.location.href)
返回什么?你能做一个可重复的jsfiddle.net测试用例吗?@mikkoohtama现在正试图。@lostsource它返回
string
所有小写。对于那些像我在控制台测试时一样困惑的人,stackoverflow向chrome公开了一个shiv For
.contains()
。你就是那个人!我来自Java背景,所以我假设所有Java
String
函数都是Javascript。显然不是一个好的假设。@Ben是的,这让人困惑。模拟
contains
方法的代码使用
indexOf
<代码>函数(f){return-1