Kotanet
Kotanet

Reputation: 573

Jquery - best way to detect mobile browser? (mousedown/touchmove)

I have some Jquery scroller.

For desktop browsers I use this construction:

holder.bind('mousedown.rotate', function(e){
    //some actions  
    doc.bind('mousemove.dragrotate', function(e){
        //some actions          
    });
    doc.bind('mouseup.dragrotate', function(){
        //some actions  
        doc.unbind('.dragrotate');
    });
});

for mobile browsers it works in this way:

holder.bind('touchmove', function(jQueryEvent) {
//some actions
});

what is the best way to determine mobile borwsers? is there a way to use same function for all platforms?

thx

Upvotes: 3

Views: 1358

Answers (2)

user1488696
user1488696

Reputation:

var is_touch_device = 'ontouchstart' in document.documentElement;

Detecting touch screen devices with Javascript

Upvotes: 1

sachleen
sachleen

Reputation: 31131

You can use navigator.userAgent to check what browser the user is using... the following code would be a good starting point.

if (navigator.userAgent.match(/Android/i)
    || navigator.userAgent.match(/iPhone/i)
    || navigator.userAgent.match(/iPad/i)
    || navigator.userAgent.match(/iPod/i)
    || navigator.userAgent.match(/BlackBerry/i)
    || navigator.userAgent.match(/webOS/i)) {
    // Mobile browser specific code here
}

Detect Mobile Browsers has a JS file you can use if you want to get more specific.

Upvotes: 3

Related Questions