Detect between a mobile browser or a PhoneGap application

后端 未结 16 1797
小蘑菇
小蘑菇 2020-11-28 03:03

Is it possible to detect if the user is accessing through the browser or application using JavaScript?

I\'m developing a hybrid application to several mobile OS thr

相关标签:
16条回答
  • 2020-11-28 03:28

    To my mind you try to make issue for self. You didn't mentioned your development platform but most of them have different deployment configuration. You can define two configurations. And set variable that indicates in which way code was deployed. In this case you don't need to care about devices where you deployed your app.

    0 讨论(0)
  • 2020-11-28 03:29

    It sounds like you are loading another webpage once the webview starts in the Phonegap app, is that correct? If that's true then you could add a param to the request url based on configuration.

    For example, assuming PHP,

    App.Config = {
      target: "phonegap"
    };
    
    <body onload="onbodyload()">
    
    var onbodyload = function () {
      var target = App.Config.target;
      document.location = "/home?target=" + target;
    };
    

    Then on the server side, include the phonegap js if the target is phonegap.

    There is no way to detect the difference using the user agent.

    0 讨论(0)
  • 2020-11-28 03:32

    I am using the same code for both phonegap app and our web client. Here is the code that I use to detect if phonegap is available:

    window.phonegap = false;
    $.getScript("cordova-1.7.0.js", function(){
        window.phonegap = true;
    });
    

    Keep in mind that phonegap js file is loaded asynchronously. You can load it synchronously by setting the correct option of a nifty jquery $.getScript function.

    Note that approach does make an extra GET request to grab phonegap js file even in your webclient. In my case, it did not affect the performance of my webclient; so it ended up being a nice/clean way to do this.Well at least until someone else finds a quick one-line solution :)

    0 讨论(0)
  • 2020-11-28 03:33

    Quick solution comes to mind is,

    onDeviceReady
    

    shall help you. As this JS call is invoked only by the Native bridge (objC or Java), the safari mobile browser will fail to detect this. So your on device app(phone gap) source base will initiate from onDeviceReady.

    And if any of the Phonegap's JS calls like Device.platform or Device.name is NaN or null then its obviously a mobile web call.

    Please check and let me know the results.

    0 讨论(0)
提交回复
热议问题