javascriptjquerydevice-detection

Direct only mobile devices not tablets


I have this line of code which detects all touch devices:

<script>if( 'ontouchstart' in window ) window.location = 'mobile.html';</script>

I just want modify it so that it ONLY targets touch enabled mobile devices but also excludes tablet devices. How do I do this without being too specific?


Solution

  • If you are wanting to redirect a user to your mobile site you would use this:

    if (screen.width <= 768) {
            window.location = "mobile.html";
        }
    

    If you want to find out if its touch screen and less than 768px I think you could use this:

    var is_touch_device = 'ontouchstart' in document.documentElement;
    
    var viewport = $(window).width()
    
    if (is_touch_device == true && viewport < 768) {
    
       (Code goes here)
    
    }