{"id":4772,"date":"2011-12-18T19:41:12","date_gmt":"2011-12-18T14:11:12","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=4772"},"modified":"2017-05-01T15:37:08","modified_gmt":"2017-05-01T10:07:08","slug":"detecting-mobile-browsers-in-javascript","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/detecting-mobile-browsers-in-javascript\/","title":{"rendered":"Detecting mobile browsers in Javascript"},"content":{"rendered":"<p>In my recent <a title=\"Grails App Development\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">grails project<\/a>, 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.<\/p>\n<pre><\/pre>\n<p>I encountered the simplest piece of code which worked in my case and thought it worth sharing.<\/p>\n<pre><\/pre>\n<p>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.<\/p>\n<p>Code is:<\/p>\n<p>[html]<br \/>\n&lt;script type=&quot;text\/javascript&quot;&gt;<\/p>\n<p>if (screen.width &lt;= 699) {<br \/>\ndocument.location = &quot;http:\/\/www.example.com\/mobile-version.html&quot;;<br \/>\n}<\/p>\n<p>&lt;\/script&gt;<br \/>\n[\/html]<\/p>\n<pre><\/pre>\n<p>&nbsp;<\/p>\n<pre><\/pre>\n<p>This worked for me.<br \/>\nHope it helps<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[723,721,722,725,724],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4772"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=4772"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4772\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=4772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=4772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=4772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}