Handling events from remote webpage directly to the app using InAppBrowser

眉间皱痕 提交于 2019-12-07 19:39:32

问题


I would like to open an external webapp from my cordova app and handle webapp events directly on the native app. For instance, when a specific URL is loaded the app should handle it by calling a function. Does anyone know if this is possible?


回答1:


Yes, it is definitly possible to handle some events with the InAppBrowser. If you look at the API docs you'll see an addEventListener function that you can use. Currently it looks like the list of events you can listen for on the external page are still somewhat limited:

  1. loadstart - event fired when the InAppBrowser starts to load a URL
  2. loadstop - event fired when the InAppBrowser finished loading a URL
  3. loaderror - event fired when the InAppBrowser encounters an error loading a URL
  4. exit - event fired when the InAppBrowser window is closed

It looks like for your purposes you could just use the loadStart or loadStop events (not sure which would be best for your purpose, probably loadStart().)

Here is some example code:

In the HTML page that you are using to open the inAppBrowser:

function onDeviceReady(){
     var ref = window.open('http://your.site.com/page', '_blank', 'location=yes');
     ref.addEventListener("loadstop", IABcallback);
}

function IABcallback(o){
    console.log("InApBrowser loaded: " +  o.url);
    if( o.url === "http://your.site.com/page2.html"){
        // Do whatever special stuff you want to do for page2 here
    }
    ... 
}


来源:https://stackoverflow.com/questions/16275845/handling-events-from-remote-webpage-directly-to-the-app-using-inappbrowser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!