2013-08-29 43 views
0

是否可以從移動設備中排除/忽略該javascript?從移動設備中排除javascript窗口大小

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

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

謝謝!

回答

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/

+0

謝謝!這看起來不錯。我試過了,但現在窗口不會在調整大小後在正常瀏覽器上重新加載。 – user1939490

+0

因爲doSomething()缺失..我修好了!再試一次 – Parfait

+0

你是我的王:)工作正常!非常感謝。 – user1939490

1

jQuery只是包裝標準的調整大小的DOM事件,例如。

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

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

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

JSFIDDLE DEMO

+0

你能告訴我我的代碼的例子嗎?我是一個JavaScript初學者。非常感謝! – user1939490

+0

用JSFIDDLE DEMO更新了我的答案... –

+0

謝謝!看起來也很棒。 – user1939490