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

前端 未结 9 435
太阳男子
太阳男子 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: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();
        }
    });
    

提交回复
热议问题