I open a new page with window.open("apphelp.html", "_blank", "location=no")
, and then it shows me a new window with my page. At the end of this page, I would like to put a "close button" so the user can come back to where they came from. I tried that with window.close()
but it didn't work. Any suggestions?
Asked
Active
Viewed 2.3k times
17

Shukant Pal
- 706
- 6
- 19

Hola Soy Edu Feliz Navidad
- 6,746
- 4
- 38
- 55
-
Did you find a solution to your issue, I am having trouble with the same thing. – SimonSays Mar 15 '13 at 02:45
-
No but I'll try again tomorrow. – Hola Soy Edu Feliz Navidad Mar 16 '13 at 13:48
-
1I have tried href="javascript:history.go(-1); with the var ref = window.open(encodeURI(url), '_self', 'location=no'); and it closes the window and returns to the index file of my app, not just shutting down from where I opened it, that would be better. I´ll see if I find something else. I read that with my code you should be able to use ref.close(); But I haven´t been able to make it work yet. – Claes Gustavsson Mar 28 '13 at 07:20
-
I think that history.back() and history.go(-1) reload the html, so it woudn't keep it states.. – Hola Soy Edu Feliz Navidad Mar 29 '13 at 22:46
2 Answers
46
I use a workaround... Since the inAppBrowser can be closed only with the back button or trough javascript. I display a close button from serverside that will change the current url firing also the event 'loadstop' letting to use the .close() method, in this way only the child browser will be closed, your app will remain in the same state.
For example my server display a webpage with a close button, something like this:
<a href="/mobile/close">Close</a>
in my client side javascript (the Phonegap app):
var ref = window.open(encodeURI(url), '_blank', options);
ref.addEventListener('loadstop', function(event) {
if (event.url.match("mobile/close")) {
ref.close();
}
});

Dario Barilà
- 515
- 6
- 5
-
-
4Is there a clearer example somewhere? Or, another approach? I cannot get this to work. For example, where exactly does that code go in PhoneGap? Do you need to have a real page that is called (not "mobile/close")? – user1452893 Feb 10 '15 at 23:53
-
I have this idea formatted for Ionic and NgCordova here: http://stackoverflow.com/a/31793577/122183 – Stone Aug 03 '15 at 17:49
-
@user1452893 so it's kind of hack. You create a link to a well-defined (potentially fake) URL and register an event listener which is launched every time navigation occurs inside the IAB. Inside the listener you check what kind of navigation happened, and if it's the magic link, you close the IAB. – jakub.g May 18 '16 at 14:23
6
I transform for multi platform support like this:
in the third page html and js
closeBrowser(){
if(history.length==1){
window.open('mobile/close');
}else{
history.back();
}
}
<a href="javascript:;" onclick="closeBrowser()">Close</a>
in my client side javascript (the Phonegap app):
var ref = window.open(encodeURI(url), '_blank','location=no');
ref.addEventListener('loadstart', function(event) {
if (event.url.match("mobile/close")) {
ref.close();
}
});
thanks the first floor!

Mr.Thanks
- 215
- 3
- 7