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);\
'
});
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter