Happy Javascript year everybody - How to detect mobile devices with Javascript - MrLiambi's blog

Breaking

My tweets

Advertisement

Tuesday, 10 January 2012

Happy Javascript year everybody - How to detect mobile devices with Javascript

Sorry it is the 10th, and i couldn't post very much on this blog. Cause the begging of the year was really busy. But i promise in the coming hours i will post everything about my new year resolutions, how did the end of the year party go, and all crazy stuff like that. But for the moment this is just a quick entry to help anyone that might have encountered the problem i am just about to describe.

P.S : If you are not a geek or at least a nerd, this post won't be of much interest for you, so you better stop reading.

Last year (2011) i was goggling a lot about how to detect mobile devices using JavaScript.  I know this is very easy using server side languages, like PHP. But sometimes, somehow you need a client side script that might help you detecting whether or not a user is browsing your site using a mobile device or not on a static html page.

The answer came after i had already gave up. If you are familiar with content management system SPIP, there is a plugin called iTwX-Mobile (http://www.spip-contrib.net/iTwX-Mobile) that help the CMS detect and redirect users with mobile devices to specially formatted pages.

I don't usually install plugins to my website without overlooking the inside code of it. And the principle behind this specific plugin is very simple. A bit of Javascript will detect the user browser, determin if it is  iphone, ipod, htc, blackberry, android, .... and redirect the user to a specific url :

Without further ado here is the code that made me happy today afternoon. (How could code made anyone happy, don't ask. But any programmer knows) :



<script type="text/javascript">
function iPhoneAlert() 
{
 if(
     (navigator.userAgent.match(/iPhone/i))
  ||(navigator.userAgent.match(/iPod/i))
  )
 {
 var question = confirm("Souhaitez-vous naviguer sur le site optimisé pour iPhone/iPod?")
  if (question)
  {window.location = "http://monsite.fr/spip.php?page=iphone_sommaire"; }
  else{}
 }
};
function BlackBerryAlert() 
{
 if(
  (navigator.userAgent.match(/BlackBerry/i))
  ||(navigator.userAgent.match(/HTC/i))
  )
 {
 var question = confirm("Souhaitez-vous naviguer sur le site optimisé pour Smartphone?")
  if (question)
  {window.location = "http://monsite.fr/spip.php?page=bberry_sommaire"; }
  else{}
 }
};

function MobileAlert() 
{
 if(
  (navigator.userAgent.match(/Nokia/i)) 
  ||(navigator.userAgent.match(/Samsung/i))
  ||(navigator.userAgent.match(/LG/i))
  ||(navigator.userAgent.match(/MOT/i))
  ||(navigator.userAgent.match(/SonyEricsson/i))
  )
 {
 var question = confirm("Souhaitez-vous naviguer sur le site optimisé pour Mobile?")
  if (question)
  {window.location = "http://monsite.fr/spip.php?page=itwx_sommaire"; }
  else{}
 }
}
</script>


Call this between <head> </head> and don't forget to add the following on your page's <body> tag:

<body onLoad="iPhoneAlert();BlackBerryAlert();MobileAlert();">
\


There you are, now you can redirect mobile phones to your specially created page for mobile devices, using Javascript.


Have a great one!

4 comments:

  1. The blog is really nice and interesting to read about various ways. we provide fitbit charge 2 bands nz at affordable prices. for more info visit our website.

    ReplyDelete
  2. The blog is pretty great, and it's fun to read about different techniques on it.
    five nights at freddy's

    ReplyDelete
  3. Good post! I am very impressed by your work. Thank you. and Check this Also - used tractors for sale

    ReplyDelete
  4. Hello friends. I am an enthusiast with role-playing horror games, I have played many games in which I feel five nights at freddy's is an extremely interesting game and is my favorite game. fnaf game has countless levels with rich story content. What I like most about fnaf game is that I can play it easily on my mobile device or on my PC. I often play fnaf games in fnaf world/a> here there are many levels of fnaf games and are regularly updated with new levels of the publisher.

    ReplyDelete