简体   繁体   English

在javascript'如果手机'

[英]In javascript 'If mobile phone'

I was thinking of doing something with the jQuery.browser but this only returns which browser you're in and if its a webkit etc etc. 我正在考虑使用jQuery.browser做一些事情,但这只会返回你所在的浏览器以及它是否是webkit等。

So i basically want to turn off certain js files from even loading if you're on a mobile device? 所以我基本上想要在移动设备上关闭某些js文件甚至加载?

I assume you can do it but how? 我想你可以做到但是怎么做?

I think this answer is better because it doesn't depends on screen width: 我认为这个答案更好,因为它不取决于屏幕宽度:

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
    // some code..
}

I know that now you've got a brand browser dependency, but this is a bit more maintainable that checking for the screen size. 我知道现在你已经拥有了品牌浏览器依赖关系,但是检查屏幕尺寸会更加可维护。

You could use screen dimensions, that way you load your small UI for small screens: 您可以使用屏幕尺寸,这样就可以为小屏幕加载小UI:

if ($(window).width() < 480 || $(window).height() < 480) {
    //small screen, load other JS files
    $.getScript('/js/script.js', function () {
        //the script has been added to the DOM, you can now use it's code
    });
}

Docs for $.getScript() : http://api.jquery.com/jquery.getscript $.getScript()文档: http$.getScript()

I know this is a very late answer and you have probable solved your problem. 我知道这是一个非常晚的答案,你很可能解决了你的问题。 But anyways, here is what I use for all of my projects: 但无论如何,这是我用于所有项目的内容:

window.isMobile = /iphone|ipod|ipad|android|blackberry|opera mini|opera mobi|skyfire|maemo|windows phone|palm|iemobile|symbian|symbianos|fennec/i.test(navigator.userAgent.toLowerCase());

 $(window).resize(function(){ /*Bind an event handler to the "resize"*/ if ($(window).width() < 480 || $(window).height() < 480) { //what you have to do here } }) 

The problem with mobile devices vs. traditional browsers is that... well, what's the difference? 移动设备与传统浏览器的问题在于......嗯,有什么区别?

There are desktops out there with slower connections than your cell phone. 有些台式机的连接速度比手机慢。 There are cell phones with higher resolutions with desktops. 有台式机具有更高分辨率的手机。 And then, of course, the reverse is also true. 当然,反过来也是如此。

Ideally, you should consider making your site in such a way that it works well on both. 理想情况下,您应该考虑使您的网站在两者上运行良好。 While in practice, this is often difficult, I think these days that you'll find your efforts worth it. 虽然在实践中,这通常很难,但我想现在你会发现你的努力是值得的。 Your users will love you for it too. 您的用户也会爱你。

If you still feel the need to attempt this... especially in JavaScript... see this post: Mobile detection 如果你仍然觉得有必要尝试这个...特别是在JavaScript中......请看这篇文章: 移动检测

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM