PhoneGap: Detect if running on desktop browser

后端 未结 30 2835
时光取名叫无心
时光取名叫无心 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

    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

    
      
      
      
      
     
    

    Ease your work! :)

提交回复
热议问题