PhoneGap:检测是否在桌面浏览器上运行 [英] PhoneGap: Detect if running on desktop browser

查看:220
本文介绍了PhoneGap:检测是否在桌面浏览器上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用PhoneGap的Web应用程序:为移动版本构建,并希望为桌面和移动版本提供单个代码库。我想要能够检测PhoneGap呼叫是否会工作(即,是在支持PhoneGap的移动设备上的用户)。



我搜索,不能相信没有简单的方法这样做。许多人提出了建议;





没有一个工作,除非你从应用程序的桌面版本中删除PhoneGap Javascript文件,这打败了我的目标,有一个代码库。



到目前为止,我想出的唯一解决方案是浏览器/用户代理嗅探,但这是不健壮的说至少。任何更好的解决方案欢迎!



编辑:一个稍微更好的解决方案是尝试调用PhoneGap函数后一些小的超时 - 如果它不工作,那么假设用户是

解决方案

我使用此代码:

  if(navigator.userAgent.match(/(iPhone | iPod | iPad | Android | BlackBerry | IEMobile)/)){
document.addEventListener(deviceready,onDeviceReady,假);
} else {
onDeviceReady(); //这是浏览器
}

UPDATE



还有很多其他方法可以检测phonegap是否在浏览器上运行,这里是另一个很好的选择:

  var app = document.URL.indexOf('http://')=== -1&& document.URL.indexOf('https://')=== -1; 
if(app){
// PhoneGap应用程序
} else {
//网页
}
pre>

如下所示:在移动浏览器或PhoneGap应用程序之间检测


I'm developing a web application that uses PhoneGap:Build for a mobile version and want to have a single codebase for the 'desktop' and mobile versions. I want to be able to detect if PhoneGap calls will work (ie, is the user on a mobile device that will support PhoneGap).

I've searched and cannot believe there is no simple way of doing this. Many people have offered suggestions;

None of which work, unless you remove the PhoneGap Javascript file from the desktop version of the app, which defeats my goal of having one codebase.

So far the only solution I have come up with is browser / user agent sniffing, but this is not robust to say the least. Any better solutions welcome!

EDIT: A marginally better solution is to try calling a PhoneGap function after some small timeout - if it doesn't work, then assume the user is on a desktop web browser.

解决方案

I use this code:

if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|IEMobile)/)) {
  document.addEventListener("deviceready", onDeviceReady, false);
} else {
  onDeviceReady(); //this is the browser
}

UPDATE

There are many other ways to detect if phonegap is running on a browser or not, here is another great option:

var app = document.URL.indexOf( 'http://' ) === -1 && document.URL.indexOf( 'https://' ) === -1;
if ( app ) {
    // PhoneGap application
} else {
    // Web page
}  

as seen here: Detect between a mobile browser or a PhoneGap application

这篇关于PhoneGap:检测是否在桌面浏览器上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆