2016年5月11日 星期三

[JS]判斷瀏覽器 navigator.userAgent

function isMobile() {
         //   return true;
        if (isMobiles.any()) {
            return true;
        } else {
            return false;
        }
    }

var isMobiles = {
        Android: function () {
            return navigator.userAgent.match(/Android/i);
        },
        BlackBerry: function () {
            return navigator.userAgent.match(/BlackBerry/i);
        },
        iOS: function () {
            return navigator.userAgent.match(/iPhone|iPad|iPod/i);
        },
        Opera: function () {
            return navigator.userAgent.match(/Opera Mini/i);
        },
        Windows: function () {
            return navigator.userAgent.match(/IEMobile/i) || navigator.userAgent.match(/WPDesktop/i);
        },
        any: function () {
            return (isMobiles.Android() || isMobiles.BlackBerry() || isMobiles.iOS() || isMobiles.Opera() 
                   || isMobiles.Windows());
        },
        FB: function () {
            return navigator.userAgent.match(/FB/i);
        },
        FBIOS: function () {
            return navigator.userAgent.match(/FBIOS/i);
        }
    };

function PostFB() {
      try {
          if (isMobile()) {
              shareurl = "https://www.facebook.com/dialog/feed?app_id=' + MyappID 
                            + '&display=touch&caption=Mycaption&link=http%3A%2F%2Fwww.google.com.tw&picture=' 
                            + MyPictireLink + '&redirect_uri=' + Myredirect +'&description=Mydescription";
              window.open(shareurl);
              location.href = 'http://www.google.com';
          }
          else 
          {
              FB.ui({
                  method: 'feed',
                  link: 'http://www.google.com',
                  caption: '#',
                  redirect_uri: 'http://www.google.com',
                  picture: MyPictureLink,
                  name: 'name',
                  message: 'message',
                  description: 'description',
              }, function (response) {
                  if(response == undefined){
                //...do something
               }
               else{
                //...do something
               }
              });
          }

沒有留言:

張貼留言