javascript - Object someUrl#someHash has no method 'contains' - Stack Overflow

The following code is giving me an error in Chrome. It seems that window.location.href does not return

The following code is giving me an error in Chrome. It seems that window.location.href does not return a String, but that seems crazy.

Here is the code:

var theUrl = "" + window.location.href;
var hashValue = theUrl.contains("#") ? theUrl.split('#')[1] : null; (This is line 6)

This returns the following error in 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

Any ideas?

EDIT: also attempted with document.URL to no avail.

The following code is giving me an error in Chrome. It seems that window.location.href does not return a String, but that seems crazy.

Here is the code:

var theUrl = "" + window.location.href;
var hashValue = theUrl.contains("#") ? theUrl.split('#')[1] : null; (This is line 6)

This returns the following error in 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

Any ideas?

EDIT: also attempted with document.URL to no avail.

Share Improve this question edited Feb 5, 2013 at 14:53 Mikko Ohtamaa 84k61 gold badges288 silver badges468 bronze badges asked Feb 4, 2013 at 21:52 thatidiotguythatidiotguy 9,01114 gold badges65 silver badges107 bronze badges 4
  • what does typeof(window.location.href) return ? – lostsource Commented Feb 4, 2013 at 22:04
  • Can you make a repeatable jsfiddle test case? – Mikko Ohtamaa Commented Feb 4, 2013 at 22:06
  • @MikkoOhtamaa Trying to now. – thatidiotguy Commented Feb 4, 2013 at 22:10
  • @lostsource It returns string all lower case. – thatidiotguy Commented Feb 4, 2013 at 22:10
Add a ment  | 

3 Answers 3

Reset to default 4

At the moment the String.contains method appears to be only supported by Firefox 19

String.contains - JavaScript | MDN

That page also mentions some inpatibilities with MooTools, maybe your problem is related. For the time being you can retrieve the hash value like this

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

.indexOf might also be useful instead of .contains

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

The string object does not have a function called "contains", what you can however use is the "indexOf" function which will return a value >= 0 if the string of your interest is found in the target string, -1 otherwise.

One more ment: You can get the hash value using window.location.hash, so instead of doing whatever you are doing above, you need to do something like this:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745160092a4614349.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信