browser-is

Simple yet powerful way to detect browsers with callbacks!

npm install browser-is
8 downloads in the last month

Still under development

Agent-detect

A small node.js module. Simple yet powerful way to detect browsers with callbacks or in an if statment! and guess what ? it can be used in an if statment easily.

Why would you use it ?

It's great when you want to render a specific view to Chrome and/or Firefox only and another one for the crappy IE6 it always return a value so you can use it in an if statment also.

Usage

agent-detect depends on two functions

With Callback

Agent.is( req, browser, callback(agent) )

if the user agent equals the browser argument - ex. chrome - then it will fire the callback.

  var http = require('http');
  var Agent = require('Agent-detect');
  http.createServer(function (req, res) {

    Agent.is(req,'chrome', function(err, data) {

      if(err){
          res.end(err.message);
      }
      res.end(data.name);
    });

  }).listen(3000, '127.0.0.1');
  console.log('Server running at http://127.0.0.1:3000');

browser argument accept arrays too. in this case as soon as it gets a match will fire the callback and return true.

  var http = require('http');
  var Agent = require('Agent-detect');
  http.createServer(function (req, res) {

    Agent.is(req,['chrome','firefox'], function(err, data) {
      if(err){
          res.end(err.message);
      }
      res.end(data.name);
    });

  }).listen(3000, '127.0.0.1');
  console.log('Server running at http://127.0.0.1:3000');

Agent.isnot( req, browser, callback(agent) )

This function is the opposite of Agent.is(). if it didn't get any match it will fire the callback and return true. otherwise it won't fire the callback.

  var http = require('http');
  var Agent = require('Agent-detect');
  http.createServer(function (req, res) {

    Agent.is(req,'chrome', function(err, data) {
      if(err){
          res.end(err.message);
      }
      res.end(data.name);
    });

  }).listen(3000, '127.0.0.1');
  console.log('Server running at http://127.0.0.1:3000');

browser argument can be an array too.

Agent-detect in If Statment

Agent.is( req, browser )

if we got a match it will return true. if not it will return false.

  var http = require('http');
  var Agent = require('Agent-detect');
  http.createServer(function (req, res) {

    if (Agent.is(req, ['chromee', 'firefox', 'safari', 'chrome'])) {
       res.send(Agent.name); // prints the user agent
    }else {
       res.send('No match'); // hint : you can also print the user agent too ;)
    }

  }).listen(3000, '127.0.0.1');
  console.log('Server running at http://127.0.0.1:3000');
npm loves you