从移动设备中排除javascript窗口大小

时间:2013-08-29 10:14:25

标签: javascript jquery mobile

是否可以从移动设备中排除/忽略该javascript?

function doSomething() {
location.reload();
};

var resizeTimer = null;
$(window).bind('resize', function() {
if (resizeTimer) clearTimeout(resizeTimer);
resizeTimer = setTimeout(doSomething, 100);
});

谢谢!

2 个答案:

答案 0 :(得分:1)

jQuery只是包装标准的resize DOM事件,例如。

window.onresize = function(event) {
    ...
}

$(function(){
    var mobile;
    if (window.width < 481) {
        mobile = 1; 
    }

    if (!mobile) {
    // All your stuff.
    }
});

JSFIDDLE DEMO

答案 1 :(得分:0)

//Class for Detecting Mobile devices
var isMobile = {
    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);
    },
    any: function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
    }
};

var resizeTimer = null;

//if not mobile do resize
if( !isMobile.any() ){
  $(window).bind('resize', function() {
    if (resizeTimer) clearTimeout(resizeTimer);
      resizeTimer = setTimeout(doSomething, 100);
  });
};
function doSomething() {
  location.reload();
};

供您参考

http://www.abeautifulsite.net/blog/2011/11/detecting-mobile-devices-with-javascript/