Results 1 to 4 of 4

Thread: How can JavaScript be used to display things differently in two different browsers?

  1. #1
    Join Date
    Mar 2012
    Location
    Gardena,California,USA
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can JavaScript be used to display things differently in two different browsers?

    Hey everyone, I started programming and was wondering how JavaScript can be used to display things differently in two different browsers? Thanks for all help!

  2. #2
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,875
    Thanks
    49
    Thanked 263 Times in 255 Posts
    Blog Entries
    56

    Default

    You'll need browser detection. What browsers do you have in mind?
    Arie Molendijk.
    ===

  3. #3
    Join Date
    Mar 2011
    Posts
    1,919
    Thanks
    59
    Thanked 105 Times in 103 Posts
    Blog Entries
    4

    Default

    Two more things:

    One- Do you need to be able to detect diferent browser versions

    Two-

    You can always use navigator.appName and if clauses.

    Example:
    Code:
    <script type="text/javascript"> 
    var browser=navigator.appName; 
    if(browser == "Microsoft Internet Explorer") 
      { 
    alert("Browser is IE");
      } 
    </script>
    Keyboard1333

    Edit: Fixed script
    Last edited by keyboard; 03-14-2012 at 09:50 PM.

  4. #4
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,875
    Thanks
    49
    Thanked 263 Times in 255 Posts
    Blog Entries
    56

    Default

    That code is not correct. It'll produce Browser is IE for all browsers.
    You could try something like this (the alerts are just examples; you can add other alerts, experiment with it):
    Code:
    <script type="text/javascript"> 
    var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/; //detect ie6
    var detectie6=document.all && !window.XMLHttpRequest; //detect IE6
    var IE7 = false /*@cc_on || @_jscript_version == 5.7 @*/; //detect ie7
    var gteIE7 = false /*@cc_on || @_jscript_version >= 5.7 @*/; //detect ie-versions 7 and higher
    var isMSIE = /*@cc_on!@*/false; //detect ie
    var NonIE = /*@cc_on!@*/true; //detect non-ie
    var detectwebkit=navigator.userAgent.toLowerCase().indexOf("applewebkit")!=-1; //detect WebKit browsers (Safari, Chrome etc)
    var opera=window.opera
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
    var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
    /* With this, IE versions can be captured by conditionals like if (ieversion>=8), if (ieversion>=7), if (ieversion>=6), if (ieversion>=5), if (ieversion==8) etc.  */
    }
    
    if(detectie6){alert("You're using IE6")}
    if (ieversion>=6){alert("You're using IE6 or higher")}
    if(opera){alert('opera')}
    if(detectwebkit){alert("You're using Chrome, Safari or another Webkit browser")}
    if(!opera && NonIE && !detectwebkit){alert('Firefox')}
    </script>
    You should be aware of the fact that browser detection using javascript may sometimes produce false results (although I think the script above is rather safe).
    If you want to distinguish between IE-versions, you can use conditional statements; it's better.
    ===
    Arie.

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
  •