2016年5月11日 星期三

[JS]判斷瀏覽器 navigator.userAgent

  1. function isMobile() {
  2. // return true;
  3. if (isMobiles.any()) {
  4. return true;
  5. } else {
  6. return false;
  7. }
  8. }
  9.  
  10. var isMobiles = {
  11. Android: function () {
  12. return navigator.userAgent.match(/Android/i);
  13. },
  14. BlackBerry: function () {
  15. return navigator.userAgent.match(/BlackBerry/i);
  16. },
  17. iOS: function () {
  18. return navigator.userAgent.match(/iPhone|iPad|iPod/i);
  19. },
  20. Opera: function () {
  21. return navigator.userAgent.match(/Opera Mini/i);
  22. },
  23. Windows: function () {
  24. return navigator.userAgent.match(/IEMobile/i) || navigator.userAgent.match(/WPDesktop/i);
  25. },
  26. any: function () {
  27. return (isMobiles.Android() || isMobiles.BlackBerry() || isMobiles.iOS() || isMobiles.Opera()
  28. || isMobiles.Windows());
  29. },
  30. FB: function () {
  31. return navigator.userAgent.match(/FB/i);
  32. },
  33. FBIOS: function () {
  34. return navigator.userAgent.match(/FBIOS/i);
  35. }
  36. };
  37.  
  38. function PostFB() {
  39. try {
  40. if (isMobile()) {
  41. shareurl = "https://www.facebook.com/dialog/feed?app_id=' + MyappID
  42. + '&display=touch&caption=Mycaption&link=http%3A%2F%2Fwww.google.com.tw&picture='
  43. + MyPictireLink + '&redirect_uri=' + Myredirect +'&description=Mydescription";
  44. window.open(shareurl);
  45. location.href = 'http://www.google.com';
  46. }
  47. else
  48. {
  49. FB.ui({
  50. method: 'feed',
  51. link: 'http://www.google.com',
  52. caption: '#',
  53. redirect_uri: 'http://www.google.com',
  54. picture: MyPictureLink,
  55. name: 'name',
  56. message: 'message',
  57. description: 'description',
  58. }, function (response) {
  59. if(response == undefined){
  60. //...do something
  61. }
  62. else{
  63. //...do something
  64. }
  65. });
  66. }

沒有留言:

張貼留言