Skip to content

Instantly share code, notes, and snippets.

@christopherdebeer
Created September 7, 2011 09:22
Show Gist options
  • Select an option

  • Save christopherdebeer/1200142 to your computer and use it in GitHub Desktop.

Select an option

Save christopherdebeer/1200142 to your computer and use it in GitHub Desktop.
Node.js Express - Mobile detection
app.get('/', function(req, res){
var ua = req.header('user-agent');
if(/mobile/i.test(ua)) {
res.render('mobile.html');
} else {
res.render('desktop.html');
}
});
@zwigby

zwigby commented May 19, 2013

Copy link
Copy Markdown

You could break out the user agent even more to tell you more specifically what OS. Similar to detection like, http://www.labnol.org/tech/ipad-user-agent-string/13230/.

@jm921106

jm921106 commented Sep 21, 2016

Copy link
Copy Markdown

where is /mobile/i.test() from ???

javascript ? jquery ?

@gandhirahul

Copy link
Copy Markdown

Simple and Precise. Thanks :)
@jm921106 That is native javascript.You can read more about it here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment