PhoneGap: Detect if running on desktop browser

后端 未结 30 2726
时光取名叫无心
时光取名叫无心 2020-11-29 15:47

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

相关标签:
30条回答
  • 2020-11-29 16:04

    I use a combination of what GeorgeW and mkprogramming suggested:

       if (!navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/)) {
          onDeviceReady();
       } else if (Phonegap.available){
          onDeviceReady();
       } else {
          console.log('There was an error loading Phonegap.')
       }
    
    0 讨论(0)
  • 2020-11-29 16:04

    Slightly modified, but works for me perfectly without any issues.

    Intent is to load Cordova only when on embedded device, not on a desktop, so I completely avoid cordova on a desktop browser. Testing and development of the UI and MVVM and so is then very comfortable.

    Put this code eg. in file cordovaLoader.js

    function isEmbedded() {
        return  
        // maybe you can test for better conditions
        //&& /^file:\/{3}[^\/]/i.test(window.location.href) && 
         /ios|iphone|ipod|ipad|android/i.test(navigator.userAgent);
    }
    
    if ( isEmbedded() )
    {
       var head= document.getElementsByTagName('head')[0];
       var script= document.createElement('script');
       script.type= 'text/javascript';
       script.src= 'cordova-2.7.0.js';
       head.appendChild(script);
    }
    

    Then instead of including cordova javascript itself include cordovaLoader.js

    <head>
      <script src="js/cordovaLoader.js"></script>
      <script src="js/jquery.js"></script>
      <script src="js/iscroll.js"></script>
      <script src="js/knockout-2.3.0.js"></script>
    </head> 
    

    Ease your work! :)

    0 讨论(0)
  • 2020-11-29 16:05

    I think this is simplest: var isPhoneGap = (location.protocol == "file:")

    EDIT For some people that didn't work. Then you might try (haven't tested)

    var isPhoneGap = ! /^http/.test(location.protocol);
    
    0 讨论(0)
  • 2020-11-29 16:05

    Interestingly, many answers, but they don't include these three options:

    1 – The cordova.js will set the cordova object in the global scope. If it is there then you are most likely running in a Cordova scope.

    var isCordovaApp = !!window.cordova;
    

    2 – Cordova will run your application as you would open a HTML document from your Desktop. Instead of the HTTP protocol it will use FILE. Detecting this will give you a chance to assume that your app was loaded locally.

    var isCordovaApp = document.URL.indexOf('http://') === -1
      && document.URL.indexOf('https://') === -1;
    

    3 – Use the load event of the cordova script to detect the context. The script include can be easily removed in the build process or the script loading will simply fail in a browser. So that this global variable will not be set.

    <script src="../cordova.js" onload="javascript:window.isCordovaApp = true;"></script>
    

    Credit goes to Damien Antipa from Adobe

    0 讨论(0)
  • 2020-11-29 16:06

    This works for me (running 1.7.0)

    if (window.device) {
      // Running on PhoneGap
    }
    

    Tested on desktop Chrome and Safari.

    0 讨论(0)
  • 2020-11-29 16:06

    I guess in someways they aren't that different are they? Ha Ha... not funny. Who didn't think this wouldn't be a problem? Here's the simplest solution for your considerations. Push different files to your server then you do to PhoneGap. I'd also temporarily go with the http: check suggested above.

    var isMobileBrowserAndNotPhoneGap = (document.location.protocol == "http:");
    

    My interest is in pushing the browsers navbar up, so really I can just delete the isolated script's tag and press rebuild [in DW] (they'll be some cleanup for deployment anyway so this can be one of those tasks.) Anyway I feel it's a good option (considering not much else is available) to efficiently just manually comment out things with isMobileBrowserAndNotPhoneGap when pushing to PG). Again for me in my situation I will simple delete the tag for the (isolated code) file that pushes up the navbar when it's a mobile browser (it will be that much faster and smaller). [So ya if you can isolated the code for that optimized but manual solution.]

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