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.

6
  • Why do you think any IE user needs to allow scripts? What if there is a user running Firefox with javascript disabled? Commented May 3, 2011 at 18:07
  • Um... You want to use JavaScript to tell your users they need to activate JavaScript? Commented May 3, 2011 at 18:07
  • 1
    @Pekka: I believe they could use JavaScript to hide the message telling them they should turn it on. :) Commented May 3, 2011 at 18:09
  • Using a client-side script to detect if client-side scripts are enabled is bound for failure. Commented May 3, 2011 at 18:09
  • 1
    As @Groo mentions, create a static message and hide it if scripts are enabled. Commented May 3, 2011 at 18:12

3 Answers 3

8

It's simple!

$(document).ready(function() {
   if ($.browser.msie) {
     // create the error box here
   }
});
Sign up to request clarification or add additional context in comments.

5 Comments

ok, probably a stupid question, but to check if it's not ie I would use: "if(!$.browser.msie){ }" right?
@Patrick: Yes, jQuery has other variables that check for any type of browser, also.
I'm using this (link at end), but it's not hiding the box in firefox or chrome -- jsfiddle.net/VHJdR
This answer uses a deprecated feature (as of jQuery 1.3) and will not work in the latest version.
This is no longer a valid answer (for current versions of jQuery)
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.

1 Comment

Cool. That will probably work. Any idea how to add a cookie so it only shows up once per browsing session?
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).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.