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.
-
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
3 Answers
Reset to default 4At 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;