Home » Android » javascript – Phonegap 3 contains an alert box in which "OK" crashes browser

javascript – Phonegap 3 contains an alert box in which "OK" crashes browser

Posted by: admin April 23, 2020 Leave a comment

Questions:

I can’t come up with a good explanation for this, but when I include

<script type="text/javascript" src="phonegap.js"></script>

In my Phonegap app, which I have not modified, 2 pops will appear on screen.

//The first popup
gap:["PluginManager","startup","PluginManager224542697"]

//the second
gap:["App","show","App224542698"]

I have to cancel both popups and would really love to understand the reasoning.

The two lines in question are on line 21117 and 21118 of phonegap.js

 // Tell the native code that a page change has occurred.
 require('cordova/exec')(null, null, 'PluginManager', 'startup', []);
 require('cordova/channel').onNativeReady.fire();

This of course does not break when its in the app, but it does mean that development is next to impossible.

How to&Answers:

For me the issue was occurring because phonegap.js was falling back to PROMPT based mode when running outside of phonegap. I believe this mode is intended for phonegap below Android 3.2, where all communication is via alert prompts (which is what you’re seeing).

I fixed the problem by setting the following variable before loading phonegap.js:

window._cordovaNative = true;

This tricks phonegap into thinking you’re running above phonegap 3.2 hence never enters Prompt mode.

Of course, if you’re targeting devices below 3.2 then doing this will probably break all communication with Phonegap on those devices…

Answer:

I have stumbled upon this error and question while trying the following:

  • Completed the hello world tutorial with cordova create, etc.
  • Deployed both on emulator and the connected device w/o a problem.
  • I got the same error as OP when doing cordova serve and just navigating to //localhost:8000 as instructed.

I agree – seeing those alerts is super annoying. It kills the whole point of speedy non-native js-based development.

What is going wrong?

Communication API with the “device” is falling back to this handler promptbasednativeapi.js (also see cordova-js/src/android/exec.js). On version 3.x.x just saying

window._cordovaNative = true;

was not enough for me. One should implement the whole protocol of communication.

Quick solution

You can use some browser side “emulator”, e.g. Ripple emulator extension for chrome ([UPDATE] Consider using https://github.com/apache/incubator-ripple for phonegap 3.x.x as pointed by DuKes0mE). Such an “emulator” can understand and fire respective events, e.g. “device ready”. So far it looks promising 🙂

What is going on?

An answer by @antscode was of great help. After some digging and reading of cordova/cordova-js code I figured out that

  • Error comes from the mechanism which is a part of cordova plugin architecture. Plugins are developed as so called cordova commands which can be executed from js – this is exactly how cordova architecture is organized.
  • Cordova is a JS framework. One is suggested to primary write (non-native) JS code. To talk to all those different native plugins one has to come up with the protocol to communicate with them, sort of RPC with JSON serialization. This is exactly what is going own.
  • Plugins can be also in pure JS. To quote the manual

Create a new echome plugin with:

window.echo = function(str, callback) {
    cordova.exec(callback, function(err) {
        callback('Nothing to echo.');
    }, "Echo", "echo", [str]);
};

Access it as a cordova command via JS:

 window.echo("echome", function(echoValue) {
        alert(echoValue == "echome"); // should alert true.
    });

No emulator solution

I could imagine a situation when an rather complicated app will break just inside such an web-browser client side “emulator” (and only in it). A better solution would be to figure out a way to convince the app not to fallback to PROMPT method of communication (the one that makes annoying alerts). Well, I do not have such a solution right now 🙁 Will be happy to learn how to do it though.

Solution is here: https://gist.github.com/ewiger/7d5e0cc8fccf311e9ce2

Answer:

there is probably no return event for the cancel button. if no instructions are passed to the kernel, this results in an exception(your browser crashing.) try defining an action for the cancel button and see if that helps.

Answer:

I went into hello/platforms/ios/CordovaLib and copied the cordova.js file into my www root and changed

Or download from here:https://github.com/apache/cordova-ios/blob/master/CordovaLib/cordova.js

<script type="text/javascript" src="phonegap.js"></script>

to

<script type="text/javascript" src="cordova.js"></script>

and no more popups

Answer:

I was having the same issue with a project created by another developer. The cause in my case was the remains of the Cordova/PhoneGap 2.x configuration. This popup is related to the plugin system. Removing plugin from my config.xml made it better:

<plugin name="InAppBrowser" value="org.apache.cordova.InAppBrowser" />

The end, I decided it would be best to make a new 3.x project and move all of my HTML/CSS/JS to the newly created project. The new project fixed all issues.

Answer:

I had initially resisted posting an answer on this question. As was mentioned by one of the developers that the phonegap.js should not be included unless it is production code.

This however means you can’t tests phonegap features.

To briefly explain my thought process, in all my years developing (php) I have often set global variables that I can easily write code for dev, stage or production.

Please consider this might not be the best way to go, but for me it works and I’m enjoying the rest of my backbone app far more than revisiting this currently 😉

So, I did this:

//Define SD
var SD = {}; //define SD so we can use it globally

/*==================================================
Is Mobile - If true then we are a mobile
================================================== */
SD.isMobile = true;

//This is with the assumtion that your domain contains (for exmaple) http://yourdomain.local
if (document.URL.indexOf("local") > 0 || document.URL.indexOf("sex") > 0) {
    SD.isMobile = false;
}

SD = {
    isMobile: SD.isMobile,
    ENVIROMENT: 'liveApp',
    CDN: 'yoururl.com/',
    HTTP: 'http://yoururl.com/',
 }

// #define the globals depending on where we are -----------------------------------------------------
SD.globals = function () {
    switch (window.location.hostname) {
        case "sd.local":
            SD.ENVIROMENT = 'localApp',
                SD.CDN = 'sd.local/',
                SD.HTTP = 'http://yoururl.com/',
                SD.AJAX = SD.HTTP+'app/';
            break;
        case "192.168.0.25": //Set to your machines IP address
            SD.ENVIROMENT = 'mobilePhone',
                SD.AJAX = SD.HTTP+ 'app/';
            break;
        default:
            SD.AJAX = SD.HTTP+'app/';
            break;
    }
};

And now finally, after all the init work I add the phonegap.js if we need it.

 if(SD.isMobile){
    $.getScript('phonegap.js', function( data, textStatus, jqxhr){
    c( "cordova was loaded." );
});

Answer:

I was experiencing the exact same two popups you describe but only experienced them when i added iOS to my phonegap project. the first time i did this i copied the www folder from my working Android app. this was the problem because it had artifacts from the Android plugins. After blasting and recreating the iOS app in the phonegap project using the command line tools i more carefully brought over only the needed html, js and css files for my app which resolved the issue.