Search code examples
javascriptangularjscordovainappbrowser

Cordova Inapp browser throws error


I get the followin error when I ue Cordova Inappbrowser (http://ngcordova.com/docs/plugins/inAppBrowser/)

TypeError: Cannot read property 'open' of undefined

$cordovaInAppBrowser.open(urlToGo, '_self', options)
              .then(function(event) {
                // success
                console.log("Success...");
              })
              .catch(function(event) {
                // error
                console.log("Error...");
              });


              $cordovaInAppBrowser.close();


Solution

  • Try and wrap your code in a device ready first;

    document.addEventListener("deviceready", function () {
    
      $cordovaInAppBrowser.open(urlToGo, '_self', options)
        .then(function(event) {
          // success
          console.log("Success...");
         })
         .catch(function(event) {
           // error
           console.log("Error...");
         });
    
    
         $cordovaInAppBrowser.close();
    
    }, false);
    

    If that doesnt work try the official cordova-plugin-inappbrowser. I sometimes find that ng plugins dont always play nice.

    Their example to trigger the browser.

    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
      window.open = cordova.InAppBrowser.open;
    }