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?

2
  • I can't find any jQuery documentation for lastIndexOf. Did you perhaps mean String.lastIndexOf or Array.prototype.lastIndexOf Commented Nov 29, 2013 at 0:20
  • @Phil sorry, I thinks its just a JavaScript thing Commented Nov 29, 2013 at 0:22

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I always check MDN for shims / polyfills under the compatibility sections
@Phil: very good point.
Yeah do not browser sniff, feature detect. FWIW IE supports lastIndexOf, here is a copy from my console doing a quick example.
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
-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.

6 Comments

I'm looking for the source, can't find it
Thanks, found it myself too.
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
-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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.