Pages

Thursday, February 5, 2015

JS: Open on InAppBrowser for Cordova/Phonegap based mobile application

There is requirement when we want to load the external link inside our mobiapp. So to ease the process I have created a class that will help in achieving it. You just have to create an instance of it. That's it!
var OpenOnInAppBrowser = function () {
        window.name = "projectname";
        var iabRef = null;

        window.notifier = {
            flag: false,
            start: function() {
                if(!window.notifier.flag) {
                    window.notifier.flag = true;
                    navigator.notification.activityStart('Loading!', 'Please wait ...');
                }
            },
            stop: function() {
                window.notifier.flag = false;
                navigator.notification.activityStop();
            }
        }

        var iabLoadStart = function (event) {
            console.log('OpenOnInAppBrowser -> iabLoadStart: ' + event.type + ' - ' + event.url);
            window.notifier.start();
        }

        var iabLoadStop = function (event) {
            console.log('OpenOnInAppBrowser -> iabLoadStop: ' + event.type + ' - ' + event.url);
            window.notifier.stop();
            var curl = event.url;
        }

        var iabLoadError = function (event) {
            console.log('OpenOnInAppBrowser -> iabLoadError: ' + event.type + ' - ' + event.message);
            window.notifier.stop();
        }

        var iabClose = function (event) {
            console.log('OpenOnInAppBrowser -> iabClose: ' + event.type);
            window.notifier.stop();
            iabRef.removeEventListener('loadstart', iabLoadStart);
            iabRef.removeEventListener('loadstop', iabLoadStop);
            iabRef.removeEventListener('loaderror', iabLoadError);
            iabRef.removeEventListener('exit', iabClose);
        }

        this.init = function (iUrl) {
            console.log('OpenOnInAppBrowser -> init: ' + iUrl);
            iabRef = window.open(iUrl, '_blank', 'location=no,toolbar=no,hidden=no');
            iabRef.addEventListener('loadstart', iabLoadStart);
            iabRef.addEventListener('loadstop', iabLoadStop);
            iabRef.addEventListener('loaderror', iabLoadError);
            iabRef.addEventListener('exit', iabClose);
        }
    };

/*
 * Usage
 */

var openOnInAppBrowser = new OpenOnInAppBrowser();
openOnInAppBrowser.init(link);

// link = 'http://www.google.com/'

No comments:

Post a Comment