2

Can someone tell me how I can determine (using JavaScript etc.) what browser is being used to visit my site?

I want to redirect to a different page based on these scenarios:

  • Wheater the user is browsing using Safari on the iPhone.
  • Using IE on a desktop
  • Using IE on a WP7 mobile device.

Thanks.

Jimmy Collins
  • 3,294
  • 5
  • 39
  • 57
  • You could do this in JavaScript but that won't really help you much, as you will need to store that information somewhere to get it. What server side language/s can you use? – Pekka Dec 28 '10 at 18:08
  • Server side language is PHP - all I need to do is ensure I present the mobile version to users browsing on a mobile device. – Jimmy Collins Dec 28 '10 at 18:09
  • include that information in your post next time. Also, add the PHP tag. – Brad Dec 28 '10 at 18:13
  • 2
    http://stackoverflow.com/questions/1005153/auto-detect-mobile-browser-via-user-agent http://stackoverflow.com/questions/3632165/detecting-mobile-browsers-on-the-web – Brad Dec 28 '10 at 18:14

1 Answers1

6

Check out the Javascript Navigator object: http://www.w3schools.com/js/js_browser.asp

alert(navigator.userAgent);

This script may help you for more complicated detection: http://www.quirksmode.org/js/detect.html

You should be handling these redirections server-side though. This is particularly true if you have a mobile version of your site. Most mobile browsers do support Javascript just fine, but there are quirks.

Server-side, you can send a redirect by sending the 302 status code, and the location: header. See http://en.wikipedia.org/wiki/HTTP_location

Edit: Since you say that you need to detect mobile browsers now in your comments, just use this script: http://detectmobilebrowsers.mobi/

Brad
  • 159,648
  • 54
  • 349
  • 530
  • Not working now. I tested with Internet Explorer, Firefox, and Chrome, they all showed "Netscape" – Quan Jul 14 '16 at 15:52
  • @Quan Check out `navigator.userAgent`. – Brad Jul 14 '16 at 16:37
  • I went to this link on Chrome http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_nav_all , and the result was "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36" – Quan Jul 14 '16 at 19:11