Results 1 to 8 of 8

Thread: Using navigator.userAgent to detect MSIE version and display a notice

  1. #1
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Default Using navigator.userAgent to detect MSIE version and display a notice

    Just by using JavaScript and not conditional statements, I would like to detect the users version of IE and then display a message if it is any version
    less than IE 9. This is what I have so far

    Code:
    if(IE){
    
    	$(function(){
    		
    		$("<div>")
    			.css({
    				'position': 'absolute',
    				'top': '0px',
    				'left': '0px',
    				backgroundColor: 'black',
    				'opacity': '0.75',
    				'width': '100%',
    				'height': $(window).height(),
    				zIndex: 5000
    			})
    			.appendTo("body");
    			
    		$("<div><img src='no-ie.png' alt='' style='float: left;'/><p><br /><strong>Sorry! This page doesn't support Internet Explorer.</strong><br /><br />If you'd like to read our content please <a href='http://browsehappy.com/'>upgrade your browser</a></p>")
    			.css({
    				backgroundColor: 'white',
    				'top': '50%',
    				'left': '50%',
    				marginLeft: -210,
    				marginTop: -100,
    				width: 410,
    				paddingRight: 10,
    				height: 200,
    				'position': 'absolute',
    				zIndex: 6000
    			})
    			.appendTo("body");
    	});		
    }
    Also there is this code, it looks cool but I am not really sure what the heck it does.

    Code:
    var ie = (function(){
        var undef, v = 3, div = document.createElement('div');
    
        while (
            div.innerHTML = '<!--[if gt IE '+(++v)+']><i></i><![endif]-->',
            div.getElementsByTagName('i')[0]
        );
    
        return v> 4 ? v : undef;
    }());
    An inline div is a freak of the web and should be beaten until it becomes a span

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    What that code does (or at least what it appears to be doing/trying to do) is create a div with an IE proprietary comment inside of it, which in turn has an i tag in it. As the version numbers are increased, if the i tag no longer exists, the version number has been found. This is unnecessary and will break down with IE 10 which doesn't use and no longer supports these proprietary comments. It will return undefined though, so should appear to be a non-IE browser. That's not true, but in many cases would be fair enough for most things. IE 10 behaves pretty much like a non-IE browser. If you want to use the navigator.userAgent string, here's a handy way of doing that:

    Code:
    var ie = /MSIE (\d+)/.exec(navigator.userAgent);
    ie = ie? ie[1] : null;
    If the browser is IE, ie will be its version number. For all other browsers ie will be null.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Default

    The code returns an error:

    Message: 'IE' is undefined
    Line: 5
    Char: 1
    Code: 0


    Heres what was changed, also I am working with jQuery actually (sorry for not point that out in the original post).
    Code:
    <script type="text/javascript" src="jquery-1.2.6.min.js"></script> 
    <script>
    var ie = /MSIE (\d+)/.exec(navigator.userAgent);
    ie = ie? ie[1] : null;
    if(IE){
    
    	$(function(){
    		
    		$("<div>")
    			.css({
    				'position': 'absolute',
    				'top': '0px',
    				'left': '0px',
    				backgroundColor: 'black',
    				'opacity': '0.75',
    				'width': '100%',
    				'height': $(window).height(),
    				zIndex: 5000
    			})
    			.appendTo("body");
    .......
    An inline div is a freak of the web and should be beaten until it becomes a span

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Upper and lower case matter -

    Not:

    Code:
    var ie = /MSIE (\d+)/.exec(navigator.userAgent);
    ie = ie? ie[1] : null;
    if(IE){ . . .
    Rather:

    Code:
    var ie = /MSIE (\d+)/.exec(navigator.userAgent);
    ie = ie? ie[1] : null;
    if(ie){ . . .
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Default

    Thank you, and this should do what was originally planned?
    Quote Originally Posted by FrickenTrevor View Post
    I would like to detect the users version of IE and then display a message if it is any version less than IE 9
    An inline div is a freak of the web and should be beaten until it becomes a span

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    ie will now either be a number or null. If it's a number it will be the version number of the IE browser. If it's null that will indicate a non-IE browser. Unless you have trouble coding what you want to have happen after that, this question is answered.

    I think you wanted to target all IE less than 9 and tell them to get a better browser. To determine that would be:

    Code:
    var ie = /MSIE (\d+)/.exec(navigator.userAgent);
    ie = ie? ie[1] : null;
    if(ie && ie < 9){
    	do whatever here to tell them about it
    {
    If you need help with the green part, let me know
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Default

    Thank you! The only question I have left is trying to get this error to be resolved

    Message: Object expected
    Line: 6
    Char: 2


    Here is the full code

    Code:
    <script src="http://code.jquery.com/jquery-1.10.3.min.js"></script>
    <script>
    var ie = /MSIE (\d+)/.exec(navigator.userAgent);
    ie = ie? ie[1] : null;
    if(ie && ie < 9){
    	$(function(){
    		$("<div>")
    			.css({
    				'position': 'absolute',
    				'top': '0px',
    				'left': '0px',
    				backgroundColor: 'black',
    				'opacity': '0.75',
    				'width': '100%',
    				'height': $(window).height(),
    				zIndex: 5000
    			})
    			.appendTo("body");
    			
    		$("<div><img src='no-ie.png' alt='' style='float: left;'/><p><br /><strong>Sorry! This page doesn't support Internet Explorer.</strong><br /><br />If you'd like to read our content please <a href='http://browsehappy.com/'>upgrade your browser</a></p>")
    			.css({
    				backgroundColor: 'white',
    				'top': '50%',
    				'left': '50%',
    				marginLeft: -210,
    				marginTop: -100,
    				width: 410,
    				paddingRight: 10,
    				height: 200,
    				'position': 'absolute',
    				zIndex: 6000
    			})
    			.appendTo("body");
    	});		
    }
    </script>
    An inline div is a freak of the web and should be beaten until it becomes a span

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    http://code.jquery.com/jquery-1.10.3.min.js

    Is a 404 Not Found.

    http://code.jquery.com/jquery-1.10.2.min.js

    Is the latest stable version available there. That's the only problem I see. However, I would suggest making the ie variable local to the document ready call:

    Code:
    <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script>
    $(function(){
    	var ie = /MSIE (\d+)/.exec(navigator.userAgent);
    	ie = ie? ie[1] : null;
    	if(ie && ie < 9){
    		$("<div>")
    			.css({
    				'position': 'absolute',
    				'top': '0px',
    				'left': '0px',
    				backgroundColor: 'black',
    				'opacity': '0.75',
    				'width': '100%',
    				'height': $(window).height(),
    				zIndex: 5000
    			})
    			.appendTo("body");
    			
    		$("<div><img src='no-ie.png' alt='' style='float: left;'/><p><br /><strong>Sorry! This page doesn't support Internet Explorer.</strong><br /><br />If you'd like to read our content please <a href='http://browsehappy.com/'>upgrade your browser</a></p>")
    			.css({
    				backgroundColor: 'white',
    				'top': '50%',
    				'left': '50%',
    				marginLeft: -210,
    				marginTop: -100,
    				width: 410,
    				paddingRight: 10,
    				height: 200,
    				'position': 'absolute',
    				zIndex: 6000
    			})
    			.appendTo("body");
    	}		
    });
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Replies: 5
    Last Post: 09-28-2012, 01:31 AM
  2. Navigator Object
    By jscheuer1 in forum Coding tips & tutorials threads
    Replies: 0
    Last Post: 06-10-2008, 05:07 AM
  3. Detect Browser type and Display Text message
    By SayJumner in forum Looking for such a script or service
    Replies: 4
    Last Post: 09-25-2007, 07:46 PM
  4. How to make Navigator bar like DD home
    By kennyssp in forum CSS
    Replies: 2
    Last Post: 03-19-2006, 06:58 AM
  5. Replies: 2
    Last Post: 01-18-2006, 11:17 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •