Cordova: Call function at main App from inappbrowser.
Mon Aug 29 2022 18:43:52 GMT+0000 (Coordinated Universal Time)
Saved by
@marcopinero
#javascript
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);\
'
});
content_copyCOPY
Comments