2023-04-11 10:41:55 +07:00

43 lines
1.6 KiB
Markdown

# Using jQuery/Zepto ($.ua)
Although written in vanilla js, this library will automatically detect if jQuery/Zepto is present and create `$.ua` object (with values based on its User-Agent) along with `window.UAParser` constructor. To get/set user-agent you can use:
## `$.ua.get():string`
Get user-agent string
## `$.ua.set(ua:string)`
Set user-agent string
## Code Example
```js
// Say we are in a browser which has 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"
// Get the details
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"
console.log($.ua.get()); // "Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Sprint APA7373KT Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0"
// Now lets try to reset to another custom user-agent
$.ua.set('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.browser.name); // "Safari"
console.log($.ua.engine.name); // "Webkit"
console.log($.ua.device); // {vendor: "Motorola", model: "Xoom", type: "tablet"}
console.log($.ua.browser.version); // "4.0"
console.log($.ua.browser.major); // "4"
// Add class to <body> tag
// <body class="ua-browser-safari ua-devicetype-tablet">
$('body')
.addClass(
'ua-browser-' +
$.ua.browser.name +
' ua-devicetype-' +
$.ua.device.type);
```