How to apply javascript to one specific browser (browser sniffer)


11 of April2008

What if you have JavaScript that only works in one browser? What if you’re using IE and Firefox. Well, you need to write one statement that will execute in IE, then another statement to run in the Firefox. This is also called a browser sniffer.
Here’s how you can do it.
First we’ll show you how to write a catch so it’ll run 1 statement if it is IE, and another if it’s any other browser.

<script type="text/javascript">
var browser = navigator.appName; //find the browser name

alert(browser); //popup with the browser name

if(browser == "Microsoft Internet Explorer"){
    alert("You are using IE");
}else{
    alert("you are using a different browser than IE");
}
</script>

Usually the code above suffices, because most JavaScript will either work in IE or the other browsers.

Your Words


  1. No 1 by
    Paul

    LOL, is it part of the style attribute now?

  2. @Paul: LOL. You’re absolutely right. I’ve fixed it. I guess I was in CSS mode that day or somethin’…

Add Your Own


bluehost.com ad


Recent Posts

  • Lessons learned from Apple&#39;s Design Process
  • On why I started another Redesign when I had just finished one.
  • Why I chose Bluehost for cheap, reliable hosting (review)
  • Break; and continue; in javascript loops
  • Writing a prototype function in JavaScript
  • -->