Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I stop a JS function from running on a mobile device?

I have the following code running and I need to make it stop 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 so what shall I write between the curved brackets?

Thank you!

like image 564
Nesta Avatar asked Feb 13 '23 21:02

Nesta


2 Answers

You can 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 it is not a mobile device then you run the code

Using $(window).width it is not a very good solution. Think what will happen if i am not using a mobile device and just change dimensions in my browser window

like image 186
laaposto Avatar answered Apr 02 '23 14:04

laaposto


Try this :

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 }
like image 41
SivaRajini Avatar answered Apr 02 '23 15:04

SivaRajini