Navin Rauniyar
Navin Rauniyar

Reputation: 10535

detecting media device with my own function

Why the following code won't work?

 var detectDevice;
    var mlg = function(){
        $(window).on('load resize',function(){
            if($(this).width >= 768){
                return detectDevice = 'mlg';
            }
        })
    }
    if(detectDevice=='mlg'){alert('test')}

Update: I need to use something like this:

var detectDevice;
var mlg = function(){
 //  detectDevice = 'mlg';
}
var mxs = function(){
 //  detectDevice = 'mxs';
}
if(detectDevice=='mlg'){
alert('test');
}

Upvotes: 0

Views: 35

Answers (3)

Rakesh Kumar
Rakesh Kumar

Reputation: 2853

Try this.

var detectDevice;
function mlg(){
    if($(window).width() >= 768){
        detectDevice = 'mlg';
        if(detectDevice=='mlg'){
            alert('test');
        }
    };
};
$(window).on('load resize', function(){mlg();});

Fiddle Demo

Upvotes: 1

Rohan Kumar
Rohan Kumar

Reputation: 40639

width() is a function try this,

$(window).on('load resize',function(){// remove quotes from window
   if($(this).width() >= 768){ //use width() and 768 rempve px
       detectDevice = 'mlg';// remove return
   }
});

Demo

Upvotes: 1

Milind Anantwar
Milind Anantwar

Reputation: 82241

width is method and not property.So it should be width().also use $(window) instead of $('window'):

$(window).on('load resize',function(){
        if($(this).width() >= 768){//or $(window).width()
            return detectDevice = 'mlg';
        }
    })

Upvotes: 1

Related Questions