Last active
October 20, 2016 07:41
-
-
Save synr/9a5f8294fe9ffa1c89d1ddfa6997dc37 to your computer and use it in GitHub Desktop.
判斷是不是行動裝置瀏覽者的寫法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//重點是下面這行 | |
// true = 是手機,false 就是其他可能(電腦等等的) | |
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent); | |
//------------------------------------------------------------------------------------------------------- | |
// 用法:直接單獨寫在 if 等等裡面的條件使用即可 | |
/* | |
if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)){ | |
console.log('不是電腦'); | |
}else{ | |
console.log('是電腦'); | |
} | |
*/ | |
//或是寫成 function ,然後到處使用也可以 | |
/* | |
function is_mobile(){ | |
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent); | |
} | |
if(is_mobile()){ | |
console.log('不是電腦'); | |
}else{ | |
console.log('是電腦'); | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment