From 0109c768584f2299e6de5a23c57c395bbd8f904a Mon Sep 17 00:00:00 2001 From: Faisal Salman Date: Thu, 7 Feb 2013 23:15:38 +0700 Subject: [PATCH] Automatically create $.ua object if jQuery is present --- package.json | 4 ++-- readme.md | 26 ++++++++++++++++++++++++-- ua-parser-js.jquery.json | 31 +++++++++++++++++++++++++++++++ ua-parser.js | 13 ++++++++++--- ua-parser.min.js | 6 +++--- 5 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 ua-parser-js.jquery.json diff --git a/package.json b/package.json index bc36f96..6cfb97c 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "title": "UA-Parser.JS", "name": "ua-parser-js", - "version": "0.5.3", + "version": "0.5.11", "author": "Faisal Salman (http://faisalman.com)", "description": "Lightweight JavaScript-based user-agent string parser", "keywords": [ - "user agent", + "user-agent", "parser", "browser", "engine", diff --git a/readme.md b/readme.md index 0e44ec5..8531756 100644 --- a/readme.md +++ b/readme.md @@ -11,7 +11,7 @@ Lightweight JavaScript-based User-Agent string parser ## Features -Extract detailed type of web browser, layout engine, operating system, and device purely from user-agent string. +Extract detailed type of web browser, layout engine, operating system, and device purely from user-agent string with relatively lightweight footprint (~7KB minified / ~3KB gzipped). ![It's over 9000](https://pbs.twimg.com/media/A9LpEG6CIAA5VrT.jpg) @@ -91,9 +91,31 @@ console.log(parser.getOS()) // {name: "RIM Tablet OS console.log(parser.getEngine().name); // "WebKit" ``` +## Using jQuery + +If you're using jQuery, `$.ua` object will be created automatically based on container's user-agent. To change different user-agent use `$.setUA(uastring)`. In case you need, `UAParser` is still present in global though. + +```js +// In browser with default user-agent: 'Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Sprint APA7373KT Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0': + +// Do some tests +console.log($.ua.device); // {vendor: "HTC", model: "Evo Shift 4G", type: "mobile"} +console.log($.ua.os); // {name: "Android", version: "2.3.4"} +console.log($.ua.os.name); // "Android" + +// reset to custom user-agent +$.setUA('Mozilla/5.0 (Linux; U; Android 3.0.1; en-us; Xoom Build/HWI69) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13'); + +// Test again +console.log($.ua.device); // {vendor: "Motorola", model: "Xoom", type: "tablet"} +console.log($.ua.engine.name); // "Webkit" +console.log($.ua.browser.version); // "4.0" +console.log(parseInt($.ua.browser.version.split('.')[0], 10)); // 4 +``` + ## License -Copyright © 2012 Faisalman <> +Copyright © 2012-2013 Faisalman <> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/ua-parser-js.jquery.json b/ua-parser-js.jquery.json new file mode 100644 index 0000000..024c095 --- /dev/null +++ b/ua-parser-js.jquery.json @@ -0,0 +1,31 @@ +{ + "title": "UA-Parser.JS", + "name": "ua-parser-js", + "version": "0.5.11", + "description": "Lightweight JavaScript-based user-agent string parser", + "keywords": [ + "user-agent", + "parser", + "browser", + "engine", + "os", + "device" + ], + "homepage": "https://faisalman.github.com/ua-parser-js", + "author": { + "name": "Faisal Salman", + "email": "fyzlman@gmail.com" + }, + "dependencies": { + "jquery": ">=1.5" + }, + "licenses": [ + { + "type": "MIT", + "url": "http://www.opensource.org/licenses/mit-license.php" + } + ], + "bugs": "https://github.com/faisalman/ua-parser-js/issues", + "docs": "https://github.com/faisalman/ua-parser-js", + "download": "https://raw.github.com/faisalman/ua-parser-js/master/ua-parser.min.js" +} diff --git a/ua-parser.js b/ua-parser.js index 12576b5..281b1d4 100644 --- a/ua-parser.js +++ b/ua-parser.js @@ -1,8 +1,8 @@ -// UA-Parser.JS v0.5.3 +// UA-Parser.JS v0.5.11 // Lightweight JavaScript-based User-Agent string parser // https://github.com/faisalman/ua-parser-js // -// Copyright © 2012 Faisalman +// Copyright © 2012-2013 Faisalman // Dual licensed under GPLv2 & MIT (function (global, undefined) { @@ -419,6 +419,13 @@ exports.UAParser = UAParser; } else { // browser env - global['UAParser'] = UAParser; + global.UAParser = UAParser; + // jQuery specific + if (typeof global.jQuery !== UNDEF) { + global.jQuery.ua = new UAParser().getResult(); + global.jQuery.setUA = function (uastring) { + global.jQuery.ua = new UAParser(uastring).getResult(); + } + } } })(this); diff --git a/ua-parser.min.js b/ua-parser.min.js index e04484b..6f0593a 100644 --- a/ua-parser.min.js +++ b/ua-parser.min.js @@ -1,7 +1,7 @@ -// UA-Parser.JS v0.5.3 +// UA-Parser.JS v0.5.11 // Lightweight JavaScript-based User-Agent string parser // https://github.com/faisalman/ua-parser-js // -// Copyright © 2012 Faisalman +// Copyright © 2012-2013 Faisalman // Dual licensed under GPLv2 & MIT -(function(e,t){"use strict";var n="",r="function",i="undefined",s="object",o="major",u="model",a="name",f="type",l="vendor",c="version",h="console",p="mobile",d="tablet",v={regex:function(){var e,o,u,a,f,l,c=arguments;for(o=0;o0){for(var o=0;o0){for(var o=0;o