最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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;

发布评论

评论列表(0)

  1. 暂无评论