JavaScript – Cordova callback in iframe

I created some Android web applications using Cordova and nanohttpd. The main page loads the localhost URL from nanohttp. The main page contains an iframe, which loads some pages from the same domain from the main page (localhost)

The following code is called from the iframe:

document.addEventListener("deviceready", function(){
    cordova.exec(function(response) {           
        self.doSomething(response);
    },function(error) {
        alert(JSON.stringify(error));
    },"thePlugin","showMap", param);}, false);

Now this is my question: cordova.exec from iframe can call the Java plug-in and display the map. But it can't find the JavaScript callback function. Can the callback function be triggered in iframe? What if possible?

resolvent:

Yes, very likely

Your problem is caused by a conflict between the main window and the iframe window

Please refer to this link: https://github.com/phonegap/phonegap/wiki/iFrame-Usage

I successfully implemented this by contacting android with Cordova plug-in

To achieve this goal, you need to do the following:

>Use PostMessage to create communication between parent window and child / iframe. Parents and children listen to each other. > send / trigger message from child / iframe to parent to call Cordova plug-in. > call Cordova plug-in from parent window. > create global function (Java in Android) to be called from Cordova code / side in parent window. > find java file in Cordova (call the callback function and parse the result). Clue: callbackContext.success (parameter); from here, we need to call the global function created in the parent window. > call the global function created in the parent window from the java file of Cordova. The following is a clip of calling JS code from the Java code:

>Use PostMessage to send the message obtained from the parent back to iframe / child

This should work. If you find any problems, please let me know

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>