Cordova: Call function at main App from inappbrowser.

var inAppBrowserRef = cordova.InAppBrowser.open("http://www.mypage.com", "_blank"); inAppBrowserRef.addEventListener("message", function (params){ if(params.data.action === "myNativeMethod"){ // Call your native method myApp.myNativeMethod(); } }); //Then in the webpage being loaded into the InappBrowser you'd send the message: <button id="myButton">Press me</button> <script type="text/javascript"> document.getElementById("myButton").addEventListener("click", function(){ var message = {action: "myNativeMethod"}; webkit.messageHandlers.cordova_iab.postMessage(JSON.stringify(message)); }, false); </script> //If you don't have direct control over the page being loaded into the InappBrowser, you could inject the code to send the message: inAppBrowserRef.executeScript({ code: '\ document.getElementById("myButton").addEventListener("click", function(){\ var message = {action: "myNativeMethod"};\ webkit.messageHandlers.cordova_iab.postMessage(JSON.stringify(message));\ }, false);\ ' });

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.