如何检测设备是否是桌面和/或移动设备,以及AngularJS是否与wifi连接 [英] How to detect if device is desktop and/or mobile, and if connection is wifi or not with AngularJS

查看:72
本文介绍了如何检测设备是否是桌面和/或移动设备,以及AngularJS是否与wifi连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已对SO进行了一些研究,但类似的Q& A用于检测它是否有连接,但不是关于连接类型。

I have done some research on SO but the similar Q&A are for detecting if it has connection or not, but not about connection type.

目的我的网站是,如果用户在移动设备(手机或平板电脑)和wifi上,播放视频剪辑;如果用户在移动设备上而不是在wifi上,则播放视频片段;如果用户不在手机上,则播放视频片段。

The purpose of my website is that, if a user is on mobile(phone or tablet) and on wifi, play a video clip; if a user is on mobile and not on wifi, play a video clip; if a user is not on mobile, then play the video clip.

不同行为的原因是为了避免由于相对较大的尺寸而对用户造成可能的附加费用视频剪辑这与速度无关 - 现在LTE v.s的速度差异wifi也许只有一点点;更多的是用户担心没有wifi连接的数据使用收费。

The reason for the different behavior is to avoid possible surcharges happen to the user due to the relatively larger size of the video clip. That is not related to speed - nowadays speed difference of LTE v.s. wifi maybe only little; it is more for the concern of users getting charged for the data usage without wifi connection.

所以我的问题是,使用AngularJS(< 2.0),
1)如何检测设备是桌面还是移动
2)如何检测设备是否连接到wifi

So my question is, using AngularJS(<2.0), 1) How to detect the device is desktop or mobile 2) How to detect the device is connected to wifi or not

(我想Q1,后备是使用Bootstrap @media,但它并不理想。)

(I guess for Q1, the fallback is to use Bootstrap @media, but it isn't ideal.)

推荐答案

你不需要Angular这样做校验。

You don't need Angular to do such check.

要检测设备是桌面设备还是移动设备,请使用 navigator.userAgent ,请参阅< a href =https://stackoverflow.com/a/25394023/3319126>回答

In order to detect if a device is a desktop or a mobile, use navigator.userAgent, see this answer

为了检测连接类型,请使用 navigator.connection ,请参阅此答案

小心,此API支持不是通用的,请参阅此处。 >
另一种方法是尝试插件,它依赖于互联网速度检查,但我有从未使用它。

最后,如果您真的需要智能手机用户的这个信息,请转换您的网站 Cordova ,然后分发您的应用。

In order to detect the connection type, use navigator.connection, see this answer
Be careful, this API support is not universal, see here.
Another way to do it is to try this plugin, which relies on internet speed check, but I have never used it.
Finally, if you REALLY need this info for smartphone users, convert your website on Cordova, then distribute your app.

这篇关于如何检测设备是否是桌面和/或移动设备,以及AngularJS是否与wifi连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆