Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 399
egr103 Avatar asked Dec 09 '25 00:12

egr103


1 Answers

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)

}
like image 53
Alex Avatar answered Dec 11 '25 12:12

Alex