2

Within jQuery Address target[1] can be Films-by-Name-C7. I use the following to extract 7

(target[1].lastIndexOf('?') > 0 ?target[1].slice(0, target[1].indexOf("?")).substr((target[1].lastIndexOf('C')+1)):target[1].substr(-(target[1].length-target[1].lastIndexOf('C')-1)))

This works on all browsers except IE8. I don't know why it does this. When I try it out in Fiddle it also doesn't work. So how can i extract 7 from Films-by-Name-C7 in ie8?

bicycle
  • 8,315
  • 9
  • 52
  • 72

2 Answers2

2

IE<9 doesn't have an .indexOf() function for Array, to define the exact spec version, run this before trying to use it:

Check this link

As you need seven this can be the solution

Community
  • 1
  • 1
Pratik Bhatt
  • 871
  • 1
  • 8
  • 21
  • Check the fiddle. There it isn't on an array but on a variable and it still doesn't work. – bicycle Feb 01 '13 at 09:39
  • .indexOf is not there in IE <9, its not abount for array that can be for variable also....!! – Pratik Bhatt Feb 01 '13 at 09:40
  • So there is no way i can extract `7` from `Films-by-Name-C7` then? `alert((typeof test.lastIndexOf('?') !== undefined ?test.slice(0, test.lastIndexOf("?")).substr((test.lastIndexOf('C')+1)):test.substr(-(test.length-test.lastIndexOf('C')-1))));` just gives nothing – bicycle Feb 01 '13 at 09:45
  • or `alert((testvar.lastIndexOf('?') != "-1" ?testvar.slice(0, testvar.lastIndexOf("?")).substr((testvar.lastIndexOf('C')+1)):testvar.substr(-(testvar.length-testvar.lastIndexOf('C')-1))));` same thing. I guess lastIndexOf isn't supported then as well – bicycle Feb 01 '13 at 09:51
  • somehow it should be possible to extract `7` from `Films-by-Name-C7` it's the last thing to make this site work for ie8 – bicycle Feb 01 '13 at 09:54
  • but unfortunately a link can-also-be-a-very-long-one-with-many-dashes or a short-one :) – bicycle Feb 01 '13 at 10:09
  • than buddy.... you need to make some base function that can handle this.... !! :) .... you can user regex and some other option to detect your format and than splitting them.....!! – Pratik Bhatt Feb 01 '13 at 10:11
1

The problem with your code is the negative value of substr function

(target[1].lastIndexOf('?') > 0 ? 
    target[1].slice(0, target[1].indexOf("?")).substr((target[1].lastIndexOf('C')+1)) :
    target[1].substr(-(target[1].length - target[1].lastIndexOf('C')-1)))

The negative value of substr won't work in browsers < IE9 as per W3C standards

Fedor
  • 1,548
  • 3
  • 28
  • 38
amit1310
  • 8,825
  • 2
  • 12
  • 12
  • Depends on your requirement. If your string always ends with the number and you always require the last digit then you can use substr(target[1].length-1,target[1].length) – amit1310 Feb 01 '13 at 10:19