var browser = {
versions: function() {
var u = navigator.userAgent;
return {
trident: u.indexOf('Trident') > -1, //IE内核
presto: u.indexOf('Presto') > -1, //opera内核
webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
gecko: u.indexOf('Firefox') > -1, //火狐内核Gecko
mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios
android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android
iPhone: u.indexOf('iPhone') > -1 , //iPhone
iPad: u.indexOf('iPad') > -1, //iPad
webApp: u.indexOf('Safari') > -1 //Safari
};
}()
}
if (browser.versions.mobile || browser.versions.ios || browser.versions.android || browser.versions.iPhone || browser.versions.iPad) {
alert('移动端');
}
document.write(navigator.userAgent+'<br><br>')
document.write(browser.versions.trident+'<span>//ie</span> <br>')
document.write(browser.versions.presto+'<span>//opera</span> <br>')
document.write(browser.versions.webKit+'<span>//苹果、谷歌内核</span> <br>')
document.write(browser.versions.gecko+'<span>//火狐内核</span> <br>')
document.write(browser.versions.mobile+'<span>//是否为移动终端</span> <br>')
document.write(browser.versions.ios+'<span>//ios</span> <br>')
document.write(browser.versions.android+'<span>//android</span> <br>')
document.write(browser.versions.iPhone+'<span>//iPhone</span> <br>')
document.write(browser.versions.iPad+'<span>//iPad</span> <br>')
document.write(browser.versions.webApp+'<span>//Safari</span> <br>')
// 那如何判断页面是在移动端还是PC端打开的呢?
window.location.href = /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent) ?
"https://www.baidu.com/" : "http://news.baidu.com/";