1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Detecting Browser

Discussion in 'JavaScript' started by NetworkTown.Net, Mar 20, 2007.

  1. #1
    Hi

    I want a code that can detect the browser. all i want it to do is something like:

    if IE7 { something} else { something }

    Any one know how i can do this.
     
    NetworkTown.Net, Mar 20, 2007 IP
  2. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
  3. NetworkTown.Net

    NetworkTown.Net Well-Known Member

    Messages:
    2,022
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    165
    #3
    INtreasting but they dont telll you how to echo something when it is true.
     
    NetworkTown.Net, Mar 20, 2007 IP
  4. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #4
    Well, that's the easy part:
    
    <html>
    <head>
    
    <script type="text/javascript">
    var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;
    if (ie7==true)
    {
      document.write( "Is IE7" );
    }
    else
    {
      document.write( "Is NOT IE7" );
    }
    </script>
    </head>
    <body>
    </body>
    </html>
    
    Code (markup):
    Of course, you can use the other methods explained in the above pages to set the ie7 variable
    .
     
    ajsa52, Mar 20, 2007 IP
  5. NetworkTown.Net

    NetworkTown.Net Well-Known Member

    Messages:
    2,022
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    165
    #5
    THANKS alot, btw is there anyway of using php in javascript?
     
    NetworkTown.Net, Mar 20, 2007 IP
  6. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #6
    I would suggest you to not go for browser detection. Instead use object detection. That will be much more reliable.
     
    Aragorn, Mar 20, 2007 IP
  7. NetworkTown.Net

    NetworkTown.Net Well-Known Member

    Messages:
    2,022
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    165
    #7
    Do you have any links to where i can get it.
     
    NetworkTown.Net, Mar 20, 2007 IP
  8. SeLfkiLL

    SeLfkiLL Active Member

    Messages:
    85
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    50
    #8
    I believe ajsa52 is using object detection in his code above, but here is a guide to it: http://javascriptkit.com/javatutors/objdetect3.shtml
     
    SeLfkiLL, Mar 20, 2007 IP
  9. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #9
    Object detection means checking for the existence of a method or property rather than checking for the browser. For example take example of httprequest for ajax.
    In browser detection, you try to find out which is the browser and then create the httprequest accordingly (ie use new XMLHttpRequest() for Fx or new ActiveXObject('MSXML2.XMLHTTP') for IE).

    But in object detection you check for the availability of XMLHttpRequest and if it is available we create an object of it, else we try for the other.
    eg:
    
    if(window.XMLHttpRequest) {
    	req = new XMLHttpRequest();
    }else{
    	try{ 
    		req = new ActiveXObject('MSXML2.XMLHTTP');
    	}catch(e){ 
    		try { 
    			req = new ActiveXObject('Microsoft.XMLHTTP');
    		}catch(e){
    			return false;
    		} 
    	} 
    }
    
    Code (markup):
     
    Aragorn, Mar 21, 2007 IP