Browser Detection

Discussion in 'JavaScript' started by missyp, Mar 23, 2007.

  1. #1
    I have the following 2 statements, and would like to combine them into one statement:

    if(navigator.userAgent.indexOf("Firefox")!=-1){
    var versionindex=navigator.userAgent.indexOf("Firefox")+8
    if (parseInt(navigator.userAgent.charAt(versionindex))>=2)
    alert("You are using Firefox 2.x or above")
    }


    version=0
    if (navigator.appVersion.indexOf("MSIE")!=-1){
    temp=navigator.appVersion.split("MSIE")
    version=parseFloat(temp[1])
    }

    if (version>=7) //NON IE browser will return 0
    alert("You're using IE7+")


    Basically, what I would like this to do is the following:

    IF the browser is either Firefox 2 or higher, OR IE7 or higher, do NOTHING. Else, alert the user.

    Could anybody please help me? I've been trying for hours, and no luck :(

    Thank you!
     
    missyp, Mar 23, 2007 IP
  2. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2
    You and your site would look less bad if you didn't. Why don't you explain why you think you need to exclude other browsers?
     
    Logic Ali, Mar 23, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
  4. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Hope this is what you are looking for
    
    version=0
    if(navigator.userAgent.indexOf("Firefox")!=-1){
    	var versionindex=navigator.userAgent.indexOf("Firefox")+8
    	if (parseInt(navigator.userAgent.charAt(versionindex))>=2)
    		alert("You are using Firefox 2.x or above")
    }else if (navigator.appVersion.indexOf("MSIE")!=-1){
    	temp=navigator.appVersion.split("MSIE")
    	version=parseFloat(temp[1])
    	if (version>=7) //NON IE browser will return 0
    		alert("You're using IE7+")
    }
    
    Code (markup):
     
    Aragorn, Mar 23, 2007 IP
  5. -NB-

    -NB- Peon

    Messages:
    153
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    How about this. You recode your website and make it so it's compliant and nice with all browsers? People hate websites that only work with one, and I guarantee they will hate being alerted and excluded.
     
    -NB-, Mar 24, 2007 IP
  6. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #6
    Logic Ali, Mar 24, 2007 IP
  7. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #7
    Aragorn, Mar 25, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Your website doesn't work with upper case characters.

    http://www.urlsmall.com/TEST2/
     
    nico_swd, Mar 25, 2007 IP
  9. missyp

    missyp Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Um, ok. To all you people who think I should re-code my website: this is for a couple of specific pages on my companie's website that use 3D models that only work in those 2 browsers.
     
    missyp, Mar 26, 2007 IP
  10. missyp

    missyp Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hello, thank you so much for this. It is ALMOST what I'm looking for. But how do I do it "the other way around".

    If a visitor uses Firefox 2 OR IE7, don't alert. If they use anything else, alert.

    Again, as I mentioned in a seperate post, this is for a couple of pages of our website where we show specific 3D models that do not work properly in any other browser...

    Thank you!


     
    missyp, Mar 26, 2007 IP
  11. poemind

    poemind Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Hi, I think this will work. Just comment out the other alerts and add another else. See if works for ya.


    version=0
    if(navigator.userAgent.indexOf("Firefox")!=-1){
    	var versionindex=navigator.userAgent.indexOf("Firefox")+8
    	if (parseInt(navigator.userAgent.charAt(versionindex))>=2)
    		//alert("You are using Firefox 2.x or above")
    }else if (navigator.appVersion.indexOf("MSIE")!=-1){
    	temp=navigator.appVersion.split("MSIE")
    	version=parseFloat(temp[1])
    	if (version>=7) //NON IE browser will return 0
    		//alert("You're using IE7+")
    }
    else{
      alert("You are using an incompatible browser")
    }
    Code (markup):
     
    poemind, Mar 26, 2007 IP
  12. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #12
    If by 'work' you mean slander the capabiliites of other browsers, then probably yes.

    
    else{
      alert("I have decided that you are using an incompatible browser, despite not having perfomed any tests to establish its actual capabilities.")
    }
    Code (markup):
     
    Logic Ali, Mar 26, 2007 IP