How to stop JS function on mobile device?

I have the following code and I need to stop it if you are using a mobile device

jQuery(document).ready(function($){
    var inHeight = $("#wrapper").innerHeight();
    $("#wrapper .col").each(function(){
        $(this).height(inHeight+"px");
        $(this).find('.content').height((inHeight-60)+"px");
    });
});

Can I use something like if($(window).width()<600){ /* do something */ }If this is the case, what should I write between curved brackets?

Thanks!

+4
source share
2 answers

You may try:

if(!(/iPhone|iPad|iPod|Android|webOS|BlackBerry|Opera Mini|IEMobile/i.test(navigator.userAgent) )) {

jQuery(document).ready(function($){
    var inHeight = $("#wrapper").innerHeight();
    $("#wrapper .col").each(function(){
        $(this).height(inHeight+"px");
        $(this).find('.content').height((inHeight-60)+"px");
    });
}); 

}

So, if this is not a mobile device, then you run the code

Using $(window).width, this is not a good solution. Think about what happens if I don’t use a mobile device and just resize it in my browser window.

+5
source

Try the following:

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());
        }
    };



if( isMobile.any() ) {....} else { // place your code here }
+2
source

Source: https://habr.com/ru/post/1525247/


All Articles