0

I want an alert message to display if someone visits the page with any version of Internet Explorer. All the posts I have read say to use , but it seems to not be working for me.

Here's my code:

 <head>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
 .
 .
 .
  <!--[if IE]>
  <script>
    $(document).ready(function(){
        alert('This website is not compatible with internet explorer. 
        Please visit the page with any other browser.');
    });
  </script>
  <![endif]-->
 </head>

Am I doing something wrong?

agassi0430
  • 1,155
  • 1
  • 13
  • 31
  • 3
    _"Am I doing something wrong?"_. I think so, why would you block your page for _all_ IE users? IE9 an IE10 will work fine... You're basically cutting your audience in half. – elclanrs Apr 27 '13 at 23:47
  • Tried testing in IE10 and nothing seems to work in it either. Probably my code is bad, I know, which is why I am asking to display the message for all IE users. – agassi0430 Apr 27 '13 at 23:51
  • @elclanrs An alert is not enough to block them. Still, if we ignore the point about blocking IE users and just center on the question of how to run an script just in IE, this is not a bad question. – Pablo Apr 27 '13 at 23:53
  • 1
    Not a bad question at all. Just a bad idea. – elclanrs Apr 27 '13 at 23:54
  • 2
    I don't think IE10 supports conditional comments. – Musa Apr 27 '13 at 23:54
  • To add to Musa's comment: http://www.sitepoint.com/microsoft-drop-ie10-conditional-comments/ – elclanrs Apr 27 '13 at 23:56
  • I know this is a bad idea, but I need to get the website live. This is just a temporary solution while I figured out the problems. – agassi0430 Apr 27 '13 at 23:57
  • If you need a fast solution that will block it for all IE users try User Agent sniffing. – elclanrs Apr 27 '13 at 23:58
  • 1
    Are you sure you included ` ` at the start of your file? If you didn't, this may be why a lot of things aren't working. – Niet the Dark Absol Apr 28 '13 at 00:11
  • yes, is included. – agassi0430 Apr 28 '13 at 00:22

3 Answers3

3

I have come to a simple, short and fast method to detect the actual browser being used via javascript, regardless of whether the user-agent is being overridden manually or not, which is even more reliable than the (now deprecated) jQuery.browser sniffing method. This one relies on looking at the window.navigator.productSub property.

In your case, for detecting IE only, this should work in all modern browsers:

if(!window.navigator.productSub && window.navigator.appName !== "Opera")

It works because only IE and Opera have navigator.productSub as "undefined". And the Blink engine has the same as Webkit. Konqueror and all branches of Safari have navigator.productSub = 20030107.

While I have yet to fully determine is this navigator.productSub is reliable for ALL browser, the following information suggest it's a safe one for 99% of cases, even for good old Netscape: https://developer.mozilla.org/en-US/docs/DOM/window.navigator.productSub http://www.billpegram.com/JavaScript/navigatorproperties.html

PS: And for some more history on navigator.productSub timestamps: Why does navigator.productSub always equals '20030107' on Chrome and Safari?

Community
  • 1
  • 1
hexalys
  • 5,177
  • 3
  • 31
  • 56
1

Your code is fine if you are truly seeking to run it for all Internet Explorer users using Internet Explorer versions five through nine. These are the only versions where conditional comments are supported.

An alternative solution, and one of my favorite answers on this site can be found here. It's been tested in Internet Explorer versions six through ten, it looks for an Internet Explorer-specific function and if the function exists, it will execute the script.

Community
  • 1
  • 1
henryaaron
  • 6,042
  • 20
  • 61
  • 80
0

Try something like this

<!--[if = IE 6]>
<script>
  alert("6");
</script>
<[endif]-->

<!--[if = IE 7]>
<script>
  alert("7");
</script>
<[endif]-->

<!--[if = IE 8]>
<script>
 alert("8");
</script>
<[endif]-->

I don't have IE so can't test it, sorry

Apparently this doesn't work for the latest version of IE.

jQuery have also removed jQuery.browser and recommend feature detection using jQuery.support

Apparently IE will return false to certain feature when you try to detect them.

Microsoft used to maintain a page (Detecting Windows Internet Explorer More Effectively) explaining how to detect ie, but that is not longer maintained since 2011

You could use Modernizr for your feature detection and make your decision based on what features the browser supports.

That's the best that I have on the subject I'm afraid.

Xotic750
  • 22,914
  • 8
  • 57
  • 79