How do I make a code, JQuery / JavaScript only work on Desktop? That is, in mobile phones and tablets, I do not want it working ... With CSS I use Media Query, but with JavaScript I get a little lost.
Thank you!
How do I make a code, JQuery / JavaScript only work on Desktop? That is, in mobile phones and tablets, I do not want it working ... With CSS I use Media Query, but with JavaScript I get a little lost.
Thank you!
If the case depends on the screen size , you can just do it:
if ($(window).width() > 960) {
alert('ecrã é maior que 960');
}
If the case really depends on whether you are a desktop browser or not, you can do this as suggested in this question of Stack Overflow in English:
$(document).ready(function() {
var isDesktop = (function() {
return !('ontouchstart' in window) // works on most browsers
|| !('onmsgesturechange' in window); // works on ie10
})();
//edit, if you want to use this variable outside of this closure, or later use this:
window.isDesktop = isDesktop;
if( isDesktop ){ /* desktop things */ }
});