Posted By

chrisaiv on 12/26/12


Tagged

javascript detection nodejs


Versions (?)

NodeJS: Server-side browser detection


 / Published in: JavaScript
 

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

Browser detection before sending any resources to the client.

  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];

Report this snippet  

You need to login to post a comment.