1

Possible Duplicate:
Detecting IE using jQuery

Hello,

I'm trying to figure out how to detect the browser type on page load, and then, if the browser is internet explorer, create a warning box telling that user that it will be necessary for them to allow scripts for my galleries to work.

I know I need to use $jquery.support to accomplish this, but I do not know the syntax, or how to make it do it only once when the page finishes loading. Is there a way I can create a cookie for the page saying that they've received the warning so that they won't get the warning every time they go to my main page?

Thanks in advance.

Community
  • 1
  • 1
Patrick Reynolds
  • 243
  • 1
  • 5
  • 18
  • Why do you think any IE user needs to allow scripts? What if there is a user running Firefox with javascript disabled? – vgru May 03 '11 at 18:07
  • Um... You want to use JavaScript to tell your users they need to activate JavaScript? – Pekka May 03 '11 at 18:07
  • 1
    @Pekka: I believe they could use JavaScript to *hide* the message telling them they should turn it on. :) – vgru May 03 '11 at 18:09
  • Using a client-side script to detect if client-side scripts are enabled is bound for failure. – Grant Thomas May 03 '11 at 18:09
  • ... yeah... hadn't thought of that.... egh.... Any thoughts as to how I could accomplish this, short of a persistent warning on the frontpage... – Patrick Reynolds May 03 '11 at 18:10
  • 1
    As @Groo mentions, create a static message and hide it if scripts _are_ enabled. – Grant Thomas May 03 '11 at 18:12

3 Answers3

8

It's simple!

$(document).ready(function() {
   if ($.browser.msie) {
     // create the error box here
   }
});
David Titarenco
  • 32,662
  • 13
  • 66
  • 111
6

The HTML5BoilerPlate project ( http://html5boilerplate.com/ ) employs a great method for handling browser-specific tweaks. Basically, the html tag will have a CSS class called ie6/ie7/ie8, etc. You can use CSS selectors to do custom overrides for IE.

Example:

<div class="ie_warning">Oh No, it is IE!</div>
<style>
  div.ie_warning { display:none; }
  .ie5 div.ie_warning, .ie7 div.ie_warning, .ie8 div.ie_warning { display:block !important; }
</style>

This works without javascript and it doesn't require jQuery or any other javascript library.

Teddy
  • 18,357
  • 2
  • 30
  • 42
3

If you want to detect if JavaScript is enabled:

Check out the noscript tag. You can use it to add HTML which will be shown to users with JavaScript disabled. Alternatively, you can create a div containing a warning message, and then hide it using jQuery on page load:

<div class="noScriptWarning">Scripts are disabled!</div>
<script>
    // no, wait, they are enabled after all
    $(".noScriptWarning").hide();
</script>

Also, if JavaScript is disabled, you won't be able to access client-side cookies, but there are server-side solutions to transferring state (at least during a session).

vgru
  • 49,838
  • 16
  • 120
  • 201