A Digital Age Deserves A Digital Leader

javascript browser sniffer?

javascript browser sniffer?

Postby isus » Wed Oct 11, 2006 8:35 pm

does anyone have a simple javascript browser sniffer? I want a user to be able to go to my index.html and have the script detect their screen res and browser type/version, and pick the appropriate css to apply to the index.html.

thanks.

Oh one more thing...can this be done within a separate .js file? It should be able to right?
PRO Level 4
Posts: 101
Joined: Wed Sep 06, 2006 8:33 pm
Location: USA, MI

Postby NoReflex » Wed Oct 11, 2006 11:06 pm

Lookd likr there are lots of sniffers for this :
http://www.google.com/search?lr=&ie=UTF ... %20sniffer
I would first try the one at SourceForge.net
PRO Level 10
User avatar
Posts: 431
Joined: Wed Nov 17, 2004 8:41 pm
Location: Romania

Postby NoReflex » Sat Oct 14, 2006 11:47 pm

I noticed you didn't replied whether you have succeded with the javascripts so I figured since I have some spare time now to properly answer your question

So, for the browser type detection you can use this :
Code: Select all
<script language="JavaScript" type="text/javascript">
var uA = navigator.userAgent;
var browserType = "unknown";
if (uA.indexOf("Opera") > -1) {
  browserType = "Opera";
} else if (uA.indexOf("Safari") > -1) {
  browserType = "Safari";
} else if (uA.indexOf("Konqueror") > -1) {
  browserType = "Konqueror";
} else if (uA.indexOf("Gecko") > -1) {
  browserType = "Mozilla";
} else if (uA.indexOf("MSIE") > -1) {
  browserType = "Internet Explorer";
}
window.alert(browserType);
</script>


For the screen resolution you can use this :
Code: Select all
window.alert(screen.availWidth + " x " +
             screen.availHeight + " pixels");


In case you also need to detect the window dimensions try this :
Code: Select all
<script language="JavaScript" type="text/javascript">
window.onload = function() {
  if (window.innerWidth) {
    window.alert("inner: " + window.innerWidth +
      " x " + window.innerHeight +
      " pixels\nouter: " + window.outerWidth +
      " x " + window.outerHeight + " pixels");
  } else {
    with (document.body) {
      window.alert(clientWidth + " x " +
        clientHeight + " pixels");
    }
  }
}
</script>


All these scripts will popup a window for you to check they really work - in the actual page you won't probably need the window.alert(...) instruction.
Hope this helps you and I apologize for the delay!
PRO Level 10
User avatar
Posts: 431
Joined: Wed Nov 17, 2004 8:41 pm
Location: Romania

Postby isus » Mon Oct 16, 2006 4:09 am

Thanks for the workup. Sorry I haven't replied the past few days have been really hectic.
PRO Level 4
Posts: 101
Joined: Wed Sep 06, 2006 8:33 pm
Location: USA, MI

Return to HTML, CSS, and Scripts

Who is online

Users browsing this forum: No registered users and 6 guests

cron
cron