To simplify my question, let’s say I have the following script:
var sPath = "/page/script/";
if (sPath.length != sPath.lastIndexOf("/"))
sPath = sPath + '/';
// do something else
Why it’s not getting into the conditional? Why it’s a diff value?
Because
sPath.lengthis13, but the last char of this is string (and thelastIndexOf('/')) is the12th, because Javascript vectors goes from0ton.For example, the string “ABC” has a length of
3, but its positions are0,1and2.However, this should work:
JSFiddle Example: http://jsfiddle.net/zGyAa/