Detecting mobile browsers in Javascript

18 / Dec / 2011 by Vishal Sahu 1 comments

In my recent grails project, I was working on mobile version of the application and needed to redirect users to the mobile version of our application, if they are accessing the application from any mobile device. I looked at various techniques/codes to detect the mobile device and redirect users to the specific URL.


I encountered the simplest piece of code which worked in my case and thought it worth sharing.


Just put this code in the head section of your webpage and it will redirect it to the specified URL if the device is a mobile browser.

Code is:

[html]
<script type="text/javascript">

if (screen.width <= 699) {
document.location = "http://www.example.com/mobile-version.html";
}

</script>
[/html]


 


This worked for me.
Hope it helps

FOUND THIS USEFUL? SHARE IT

comments (1 “Detecting mobile browsers in Javascript”)

  1. Idel

    Look also my project called Apache Mobile Filter is an open source, and has a professional approach for Device Detection.
    Also has useful module to adopt the images to the screen size.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *