1

I am using JavaScripts lastIndexOf() which doesnt work in IE so I must write an alternative code.

I simply want an if statement to do something like:

if(IE = true){
  //run alternative to lastIndexOf()
}

But I am really struggling with finding a way to achieve this.

So far I have tried the following:

JQuery's .browser is deprecated.

.support seems to only detect certain facets or features of a browser.

Modifying .support with JavaScript, as here, wont work and throws errors.

The hasClass() method no longer works:

<!--[if lt IE 7]>      <html class="ie6"> <![endif]-->
<!--[if IE 7]>         <html class="ie7"> <![endif]-->
<!--[if IE 8]>         <html class="ie8"> <![endif]-->
<!--[if gt IE 8]><!--> <html>         <!--<![endif]-->
if ($('html').hasClass('ie7');

Would anyone know a simple way to just detect the browser type?

Community
  • 1
  • 1
MeltingDog
  • 14,310
  • 43
  • 165
  • 295
  • I can't find any jQuery documentation for `lastIndexOf`. Did you perhaps mean [`String.lastIndexOf`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf) or [`Array.prototype.lastIndexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf) – Phil Nov 29 '13 at 00:20
  • @Phil sorry, I thinks its just a JavaScript thing – MeltingDog Nov 29 '13 at 00:22

2 Answers2

4

There are cases when you have no choice but to use browser detection, but in this case it is better if you detect the function itself, and shim it in if it is not there.

Community
  • 1
  • 1
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • 1
    I always check MDN for shims / polyfills under the [compatibility](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf#Compatibility) sections – Phil Nov 29 '13 at 00:23
  • @Phil: very good point. – Amadan Nov 29 '13 at 00:25
  • Yeah do not browser sniff, feature detect. FWIW IE supports lastIndexOf, here is a copy from my console doing a quick example. – Chris Love Nov 29 '13 at 03:04
  • JavaScript Console is attached and accepting commands. var str = "Hello planet earth, you are a great planet."; var n = str.lastIndexOf("planet"); undefined n 36 – Chris Love Nov 29 '13 at 03:04
-2

Edit: The best way to fix your issue is given by MDN.


How can you detect the version of a browser? has a very nice way of getting the browser type and version as a string.

You might also find Detect IE version (prior to v9) in JavaScript helpful for IE-specific detection.

You can also try:

if (navigator.userAgent.indexOf("MSIE") > 0 )

I have not tested this however.

Community
  • 1
  • 1
JDong
  • 2,304
  • 3
  • 24
  • 42
  • I'm looking for the source, can't find it – JDong Nov 29 '13 at 00:26
  • 1
    http://stackoverflow.com/a/5918791/80860 – kennebec Nov 29 '13 at 00:28
  • Thanks, found it myself too. – JDong Nov 29 '13 at 00:29
  • `You might also find Detect IE version in Javascript helpful.` is exactly what I needed. Added the piece of code I used to your answer above – MeltingDog Nov 29 '13 at 00:38
  • 3
    -1 Browser detection is a flawed strategy and has been considered a very bad idea for a long time. Please don't suggest it, paticularly when there is a very simple way to detect support for `indexOf` and deal with cases where it's lacking. – RobG Nov 29 '13 at 00:56
  • I assumed that the asker wanted to detect browser type. I agree however that browser detection is not the best choice for this application. Not sure but http://stackoverflow.com/questions/1744310/how-to-fix-array-indexof-in-javascript-for-ie-browsers might be more helpful in that direction. – JDong Nov 29 '13 at 01:00