NodeJS: Server-side browser detection


/ Published in: JavaScript
Save to your folder(s)

Browser detection before sending any resources to the client.


Copy this code and paste it in your HTML
  1. var ua = request.headers['user-agent'],
  2. $ = {};
  3.  
  4. if (/mobile/i.test(ua))
  5. $.Mobile = true;
  6.  
  7. if (/like Mac OS X/.test(ua)) {
  8. $.iOS = /CPU( iPhone)? OS ([0-9\._]+) like Mac OS X/.exec(ua)[2].replace(/_/g, '.');
  9. $.iPhone = /iPhone/.test(ua);
  10. $.iPad = /iPad/.test(ua);
  11. }
  12.  
  13. if (/Android/.test(ua))
  14. $.Android = /Android ([0-9\.]+)[\);]/.exec(ua)[1];
  15.  
  16. if (/webOS\//.test(ua))
  17. $.webOS = /webOS\/([0-9\.]+)[\);]/.exec(ua)[1];
  18.  
  19. if (/(Intel|PPC) Mac OS X/.test(ua))
  20. $.Mac = /(Intel|PPC) Mac OS X ?([0-9\._]*)[\)\;]/.exec(ua)[2].replace(/_/g, '.') || true;
  21.  
  22. if (/Windows NT/.test(ua))
  23. $.Windows = /Windows NT ([0-9\._]+)[\);]/.exec(ua)[1];

URL: http://stackoverflow.com/questions/6163350/server-side-browser-detection-node-js

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.