Imen.Ab
Imen.Ab

Reputation: 39

(cordova) (inappbrowser) Let users know that you're getting their page ready

I've an app built with cordova and InAppBrowser. I'm trying to show a "loading spinner " or a "loading message" (instead of having a blank page while the inappbrowser is loading) , I tried to do it but it's not working , here is my app.js code:

$(document).ready(function() {
	document.addEventListener("deviceready", onDeviceReady, false);
});

var ref;

function onDeviceReady() {		
     	
		    try {				
				ref = cordova.InAppBrowser.open('https://cordova.apache.org', '_blank', 'location=no');
				ref.addEventListener('loadstart', loadStartCallBack);
				ref.addEventListener("exit", onBackButton, false); 
			}
			catch(err) {
				alert("Plugin Error - " + err.message);
			}
			
			

		function onBackButton(e) {
			navigator.app.exitApp();
		}

		function loadStartCallBack() {

    $('#status-message').text("loading please wait ...");

}
}

And here is my index.html code :

<!DOCTYPE html>

<html>
    <head>
        <meta name="format-detection" >
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <title>InApp Browser</title>
       
    </head>
    <body>      
		 <script type="text/javascript">
        navigator.splashscreen.show();
        </script>
		<div id="status-message"></div>
        <script type="text/javascript" src="js/jquery.js"></script>	
		<script type="text/javascript" src="cordova.js"></script>
		<script type="text/javascript" src="js/app.js"></script>
    </body>
</html>

Upvotes: 1

Views: 920

Answers (2)

Naresh Kumar
Naresh Kumar

Reputation: 938

You can't show loading message, because if you open Inappbrowser, your app goes to background so, the div <div id="status-message"></div> will be hide.

 var Ref;
   Ref = window.open('http://www.example.com', '_blank', 'location=no,toolbar=no');
             Ref.addEventListener('loadstart', inAppBrowserbLoadStart);
             Ref.addEventListener('loadstop', inAppBrowserbLoadStop);
             Ref.addEventListener('loaderror', inAppBrowserbLoadError);
             Ref.addEventListener('exit', inAppBrowserbClose);


       function inAppBrowserbLoadStart(event) {

             navigator.notification.activityStart("Please Wait", "Its loading....");
             alert(event.type + ' - ' + event.url);

        }

        function inAppBrowserbLoadStop(event) {
             navigator.notification.activityStop();
             alert(event.type + ' - ' + event.url);

        }

        function inAppBrowserbLoadError(event) {
             navigator.notification.activityStop();
             alert(event.type + ' - ' + event.message);
        }

        function inAppBrowserbClose(event) {

             Ref.removeEventListener('loadstart', iabLoadStart);
             Ref.removeEventListener('loadstop', iabLoadStop);
             Ref.removeEventListener('loaderror', iabLoadError);
             Ref.removeEventListener('exit', iabClose);
        }

Upvotes: 1

Carlos Delgado
Carlos Delgado

Reputation: 3065

The loadstop callback should do the trick for you.

Remember to add the property hidden=yes as third parameter in the browser.open method, otherwise the browser will be always visible and you won't see the loading message :

cordova.InAppBrowser.open('https://cordova.apache.org', '_blank', 'location=no,hidden=yes');

Finally your code should look like :

<script>
var ref;

function onDeviceReady() {
    try {
        ref = cordova.InAppBrowser.open('https://cordova.apache.org', '_blank', 'location=no,hidden=yes');
        ref.addEventListener('loadstart', loadStartCallBack);
        ref.addEventListener("loadstop", loadEndCallback, false);
        ref.addEventListener("exit", onBackButton, false);
    } catch (err) {
        alert("Plugin Error - " + err.message);
    }

    function onBackButton(e) {
        navigator.app.exitApp();
    }

    function loadStartCallBack() {
        $('#status-message').text("loading please wait ...");
    }

    function loadEndCallback(){
        alert("Your page has been loaded, showing ...");
        $('#status-message').text("");
        ref.show();
    }
}

document.addEventListener("deviceready", onDeviceReady, false);
</script>

Do you get another specific error or something with the remote debbuging in chrome?

Upvotes: 3

Related Questions