问题
I have an issue with InAppBrowser. I am trying to catch errors that might come from the website I'm loading and I'm simulating this by a faulty .htaccess, which produces an error 500.
I have the following code:
ref = cordova.InAppBrowser.open($openuri, '_blank', 'location=no,toolbar=no,zoom=no,enableviewportscale=no,disallowoverscroll=no');
ref.addEventListener("loadstop",function() {
.....
});
ref.addEventListener("loaderror",function() {
alert('Toot');
});
ref.addEventListener('loadstart', function(event) {
.....
});
The website is opening and all the "loadstop" and "loadstart" events work correctly. However, when I deliberately make the page return an "error 500", the event "loaderror" is not firing.
It does fire for other errors - like when there is no internet on the phone.
Any ideas as to why this is happening are highly appreciated.
回答1:
Looking at InAppBrowser Plugin code for Android, it interally uses onReceivedError
method for handling loaderror
event. So i looked at this issue from webview perspective and stumbled on this link - https://code.google.com/p/android/issues/detail?id=968 which states that the onReceivedError
function is not meant to catch HTTP errors.
For HTTP error handling, we may have to use onReceivedHttpError
function which is not part of the plugin. I hope it makes sense as to why this is not working.
回答2:
I am using Ionic 4 and I couldn’t manage to make the solution based on config.xml editing to work :
preference name="ErrorUrl" value="myErrorPage.html"/
Placing an addEventListener on loaderror didn’t work neither. It looks like it is not triggered by http errors and the plugin need a fix.
But we found a hack that is much simpler.
On loadstop we wait 500 milliseconds and then we get the loaded url by triggering executeScript with and window.location.href
If the loaded url is of the custom error page, in Cordova (not in IAB) we display a custom message with a back button.
It's a hack but that cover the requirement for now
来源:https://stackoverflow.com/questions/37745243/cordova-inappbrowser-loaderror-event-not-firing-for-error-500