window.navigator.userAgent中有浏览器信息

// match()是能用正则的字符串方法,返回数组或者null
if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios)/i))) {
alert('ios')
}else if((navigator.userAgent.match(/(Android)/i))){
alert('android')
}else{
alert('pc')
}
// test是正则方法,返回true或者false
if(/(phone|pad|pod|iPhone|iPod|ios)/i.test(navigator.userAgent)) {
alert('ios')
}else if(/Android/i.test(navigator.userAgent)){
alert('android')
}else{
alert('pc')
}
本文介绍了如何通过window.navigator.userAgent属性来判断用户设备类型,包括iOS、Android和PC。使用了JavaScript中的正则表达式方法match和test进行模式匹配。
5212

被折叠的 条评论
为什么被折叠?



