在 javascript 中“如果手机”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9048253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:29:49  来源:igfitidea点击:

In javascript 'If mobile phone'

javascriptjquerymobile

提问by Jamie Hutber

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?

我想你可以做到,但怎么做?

回答by Nahuel Barrios

I think this answeris 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.

我知道现在您有一个品牌浏览器依赖项,但这比检查屏幕尺寸更易于维护。

回答by Jasper

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: //api.jquery.com/jquery.getscript

回答by Progo

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());

回答by Abdoulaye SOW

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

回答by Brad

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 中……请参阅这篇文章: 移动检测