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!
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.
ReplyDeleteThe blog is pretty great, and it's fun to read about different techniques on it.
ReplyDeletefive nights at freddy's
Good post! I am very impressed by your work. Thank you. and Check this Also - used tractors for sale
ReplyDeleteHello 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