I have a PhoneGap app that uses the InAppBrowser to load the Google login experience. As such, I need an event listener that detects when the browser changes location. The setup below works perfectly fine on all Android versions except for 4.4.2, as best as I can tell; the event listener fires and all is good.
However, on Android 4.4.2 I can't seem to get any event listeners to fire for the window; loadstart, onload, onscroll, etc. Nothing seems to fire. Can't seem to find any solutions on Google or StackOverflow, unfortunately.
Not sure what additional information is needed/useful, but happy to provide anything.
var auth_window = window.open(auth_url, '_blank', 'location=no,toolbar=no');
auth_window.addEventListener('loadstart', function(event) {
alert("blah")
})
-
***Update***
I've been able to get the listener to fire by backing out of InAppBrowser and opening it again. I have no clue why it would work in this case but not otherwise, though. Any help here would be very much appreciated.
If your event listener not working is dependent on some logic, whether it's about which element it'll listen on or if it's registered at all, the first step is to check that the listener is indeed added to the element. Using a breakpoint in the developer tools , a logpoint or console.
Add an Event Handler to the window Object The addEventListener() method allows you to add event listeners on any HTML DOM object such as HTML elements, the HTML document, the window object, or other objects that support events, like the xmlHttpRequest object.
Android Tutorial: What are Listeners? Android Listeners are used to capture events. When, for instance, the user interacts with the Android system by clicking on a button, the Listeners would prompt the underlying activity to do the task associated with the button click.
Constantly calling exec will force process the messages.
setInterval(function () {
cordova.exec(null, null, '', '', [])
}, 200);
I am using this hack in meteor-phonegap-oauth, https://github.com/jperl/meteor-phonegap-oauth/blob/badfb932bb0b525a32c35115eb75edb8d21bbf57/patch_window.js#L20-L29
This will also fix the problem that plugins do not invoke their callback until the next plugin action, ex: Cannot use asynchronous methods in Cordova 3.4. Onsuccess is not called after the first call of plugin method
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With