开发者

JQuery if browser = ie, creating warning box [duplicate]

This question already has answers here: 开发者_如何学编程 Closed 11 years ago.

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.


It's simple!

$(document).ready(function() {
   if ($.browser.msie) {
     // create the error box here
   }
});


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.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜