Automatically create $.ua object if jQuery is present

This commit is contained in:
Faisal Salman 2013-02-07 23:15:38 +07:00
parent d1120a4d5d
commit 0109c76858
5 changed files with 70 additions and 10 deletions

View File

@ -1,11 +1,11 @@
{ {
"title": "UA-Parser.JS", "title": "UA-Parser.JS",
"name": "ua-parser-js", "name": "ua-parser-js",
"version": "0.5.3", "version": "0.5.11",
"author": "Faisal Salman <fyzlman@gmail.com> (http://faisalman.com)", "author": "Faisal Salman <fyzlman@gmail.com> (http://faisalman.com)",
"description": "Lightweight JavaScript-based user-agent string parser", "description": "Lightweight JavaScript-based user-agent string parser",
"keywords": [ "keywords": [
"user agent", "user-agent",
"parser", "parser",
"browser", "browser",
"engine", "engine",

View File

@ -11,7 +11,7 @@ Lightweight JavaScript-based User-Agent string parser
## Features ## 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) ![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" 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 ## License
Copyright © 2012 Faisalman <<fyzlman@gmail.com>> Copyright © 2012-2013 Faisalman <<fyzlman@gmail.com>>
Permission is hereby granted, free of charge, to any person obtaining a copy of 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 this software and associated documentation files (the "Software"), to deal in

31
ua-parser-js.jquery.json Normal file
View File

@ -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"
}

View File

@ -1,8 +1,8 @@
// UA-Parser.JS v0.5.3 // UA-Parser.JS v0.5.11
// Lightweight JavaScript-based User-Agent string parser // Lightweight JavaScript-based User-Agent string parser
// https://github.com/faisalman/ua-parser-js // https://github.com/faisalman/ua-parser-js
// //
// Copyright © 2012 Faisalman // Copyright © 2012-2013 Faisalman
// Dual licensed under GPLv2 & MIT // Dual licensed under GPLv2 & MIT
(function (global, undefined) { (function (global, undefined) {
@ -419,6 +419,13 @@
exports.UAParser = UAParser; exports.UAParser = UAParser;
} else { } else {
// browser env // 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); })(this);

6
ua-parser.min.js vendored

File diff suppressed because one or more lines are too long