How to fire deviceready event in Chrome browser (trying to debug phonegap project)

前端 未结 9 391
太阳男子
太阳男子 2020-12-23 16:09

I\'m developing a PhoneGap application and I\'d like to be able to debug it in Chrome rather than on the phone. However, I do the initialization of my code in an onDeviceRe

相关标签:
9条回答
  • 2020-12-23 16:33

    user318696's window.device detection works well. If using Kendo UI Mobile and PhoneGap, the following script will enable functionality in both PhoneGap builds and web browsers. This is based on Burke Holland's PhoneGap Build Bootstrap Project for Kendo UI Mobile and is intended to be placed at the bottom of the page before the closing body tag.

        <script type="text/javascript">
        var tkj = tkj || {};
    
        tkj.run = (function () {
            // create the Kendo UI Mobile application
            tkj.app = new kendo.mobile.Application(document.body);
        });
    
        // this is called when the intial view shows. it prevents the flash of unstyled content (FOUC)
        tkj.show = (function () {
            $(document.body).show();
        });
    
        (function () {
            if (!window.device) {
                //initialize immediately for web browsers
                tkj.run();
            }
            else if (navigator.userAgent.indexOf('Browzr') > -1) {
                // blackberry
                setTimeout(tkj.run, 250)
            } else {
                // attach to deviceready event, which is fired when phonegap is all good to go.
                document.addEventListener('deviceready', tkj.run, false);
            }
        })();
        </script>
    
    0 讨论(0)
  • 2020-12-23 16:34

    Ended up pulling out the StopGap code and having to introduce a tiny delay (have this code running in a separate script than page-specific code):

    window.setTimeout(function() {
        var e = document.createEvent('Events'); 
        e.initEvent("deviceready", true, false); 
        document.dispatchEvent(e);
    }, 50);
    
    0 讨论(0)
  • 2020-12-23 16:34

    For my mobile site and mobile App I'm using the following code with jQuery:

    function init() { ... };
    if ("cordova" in window) {
        $(document).on("deviceready", init);
    } else {
        $(init);
    }
    
    0 讨论(0)
  • 2020-12-23 16:34

    Use the Ripple Mobile Emulator. It is free on the Chrome Web Store. When it is installed, navigate to the page you want to test it on, right click the page and choose Ripple Mobile Emulator > Enable. When prompted, choose PhoneGap.

    The emulator is good, but it is still in beta so not everything has been implemented yet.

    Ad@m

    0 讨论(0)
  • 2020-12-23 16:42

    Add this code to your onLoad handler function:

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

    Event "deviceready" is fired in cordova.js so I don't know a way to detect existence of this event in application code.

    0 讨论(0)
  • 2020-12-23 16:44

    user318696 had the magic I was looking for. "device" is defined in cordova and does not get defined when in a browser (non-phoneGap app wrapper).

    EDITED:

    I ran into a scenario where Cordova took quite a while to initialize on a device and the "original" answer here was no longer valid. I have moved on to requiring a parameter on the URL when running tests in the browser. (in the example I'm looking for "testing=" in the url of the original page)

    $(document).ready(function () {
    
        // ALWAYS LISTEN
        document.addEventListener("deviceready", main, false);
    
        // WEB BROWSER
        var url = window.location.href;
        if ( url.indexOf("testing=") > -1 ) {
            setTimeout(main, 500);
        }
    
    });
    

    ORIGINAL:

    I haven't dug deeply enough to know how long to trust this [could they start defining "device" in the browser in a future release?] But at least up to 2.6.0 this is safe:

    $(document).ready(function () {
        // call main from Cordova
        if ( window.device ) {
            document.addEventListener("deviceready", main, false);
        }
    
        // from browser
        else {
            main();
        }
    });
    
    0 讨论(0)
提交回复
热议问题