سؤال

i have this situation:

if (image.indexOf("/bob/") != -1 || image.indexOf("/grabs/") != -1 || image.indexOf("/") == image.lastIndexOf("/")) {
    alert('success');
}

in IE8 i get Object doesn't support property or method 'indexOf'

i could probably use something like $.inArray("/bob/", image), but im not sure about lastIndexOf

any ideas how can i solve this?

هل كانت مفيدة؟

المحلول

Try using a regex, something like

if(/\/(bob|ginger|grabs)\//.test(image) || /^[^\/]*\/$/.test(image)){
}

نصائح أخرى

If you want to solve it with jQuery, you could do

$.inArray("/", image, $.inArray("/", image)) === -1

i.e. look for the next occurrence of / after the first occurrence. This assumes that / is always present in the array. If not, then the equivalent would be

var index =  $.inArray("/", image);
if (.... || (index === -1 ||  $.inArray("/", image, index) === -1)) {
   // ...
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top