[capacitor/Android] How to close the window opened by window.open()
-
App opens the external window for some predefined user iteration and that window must be auto-closed. But what is perfectly works at desktop do not work at under capacitor/Android. If I use onload=“setTimeout(() => window.close(), 5000)” it prints error in console “Scripts may close only the windows that were opened by it”. If I use
… some Vue…
let w = window.open()
setTimeout(() => w.close(), 5000)
…It does nothing. Please help
-
I think it’s generally better to use Capacitor plugins for that:
https://capacitorjs.com/docs/apis/browser
But looking at their docs on how to close the in-app browser:
“Close an open browser. Only works on iOS and Web environment, otherwise is a no-op” . I read that as it will not work on Android.Confirmed close with browser plugin close will not work on Android: https://github.com/ionic-team/capacitor/issues/1414
Found this also , maybe it will help you:
https://medium.com/@harshal_59221/window-close-not-working-how-to-redirect-the-parent-window-i-have-got-you-covered-solution-56c63a40f169 -
Solved- the stock cordova iab works perfectly.
-
@Sfinx
Great find! -
@Sfinx could you please tell us how you managed to use the stock cordova iab in a capacitor context? I’ve tried using capacitor’s iab but it doesn’t have event listeners for when the window loads.
-
I’ve used stock call:
if (this.$q.platform.is.capacitor) { return cordova.InAppBrowser.open(url, '_blank', params)
I’ve not needed onload events but needed to auto-close the window after user posts some data at URL. It was not trivial as one tab can’t close other one