Phonegap web app in regular desktop browsers

前端 未结 18 1931
深忆病人
深忆病人 2020-12-08 02:27

If I develop a web app in phonegap, can the same web app be made to run in regular desktops/laptops inside a browser?

18条回答
  •  星月不相逢
    2020-12-08 02:48

    It will definitely run on desktop and on a webserver whatever, IF you do it right.

    I do this with many of my apps and things to note is:

    • CORS is allowed in PhoneGap but may not be on Desktop browsers
    • Cordova/PhoneGap APIs won't work and can throw errors if you try to call them

    Good practice is to wrap your device specific event listeners etc. in the onDeviceReady function as:

        onDeviceReady: function() {
        // Register the device event listeners
        document.addEventListener("backbutton", function (e) {
            console.log("default prevented");
            e.preventDefault();
        }, false );
        document.addEventListener("resume", onDeviceResume, false);
    

    and register that in the initialize function

    document.addEventListener("deviceready", onDeviceReady, false);
    

    Apart from that I use a primitive function to assign the variable "phonegap" that I can reference if I need to know what platform we are on, or if we are desktop (in which case it will be false).

        isPhoneGap: function() {
        var is = false;
        var agent = navigator.userAgent.toLowerCase();
        var path = window.location.href;
        if (path.indexOf("file://") != -1) {
            if (agent.indexOf("android") != -1) {
                is = "android";
            } else if (agent.indexOf("iphone") != -1 || agent.indexOf("ipad") != -1 || agent.indexOf("ipod") != -1) {
                is = "ios";
            } else if (agent.indexOf("iemobile") != -1) {
                is = "wp";
            }
        }
        return is;
    },
    

    Not super pretty but hope this helps.

提交回复
热议问题