您的位置:首页 > 其它

判断设备及学习定义方法中的返回值使用

2016-07-07 14:56 393 查看
学习写法

1)定义判断设备的方法,注意返回值的使用

function getUserAgent() {
var userAgent = navigator.userAgent,
iPhone = userAgent.indexOf('iPhone') > -1,
iPad = userAgent.indexOf('iPad') > -1,
iPod = userAgent.indexOf('iPod') > -1,
Android = userAgent.indexOf('Android') > -1;
if (iPhone || iPad || iPod) {
return {
type: 'iOS'
};
} else if (Android) {
return {
type: 'Android'
};
} else {
return {
type: ''
};
}
}


2)方法使用,先运行,取到返回值,方便下面直接使用

var userAgent = getUserAgent();


3)使用 userAgent.type 使用返回值

if(userAgent.type == 'iOS'){
alert('苹果设备');
}else{
alert('安卓及其他设备');
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: