判断设备及学习定义方法中的返回值使用
学习写法
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('安卓及其他设备'); }