Reputation: 4128
The book I am reading tells me to open up the JavaScript console and try the code "foo: bar".indexOf(":")
. I've tried it in many ways. I tried removing quotation marks, putting it inside a show()
and alert()
function. I just can't seem to tease anything out.
Has something changed in JavaScript? Has the author made a mistake? Am I supposed to get no return? Do I need to append document.write, perhaps? Any help greatly appreciated.
Upvotes: 0
Views: 161
Reputation: 178413
Yes something changed in Firefox 5+ However the console (ctrl-shift-k) still works
In the error console (ctrl-shift-J) you will need to wrap it in alert:
Upvotes: 1
Reputation: 11844
try like follows, it should work. Generally the indexOf() will return -1 if the value to search for never occurs.
var str="foo:bar";
document.write(str.indexOf(":") + "<br />");
The output should be 3
Upvotes: 1
Reputation: 5620
foo:bar is a property definition in json, and indexOf is supposed to deal with a left value (string variable, constant, or at least something that can have characters in it. I don't know why the book you are reading wants you to do this, but it doesn't seem to be correct. The correct way to use indexOf would be :
var myObject = {
foo:"bar"
}
alert(myObject.foo.indexOf("a"));
Upvotes: 1