I open a new window with window.open() to redirect users to the oauth login page. However, after successful login when the user is redirected back to my app the previous window with the window.open call does not close itself in ios.

On the iPad it would close the wrong window and on the iPhone it wouldn't close the window at all. The code works fine on Android and on desktop versions of Chrome and Firefox.

After much rooting about, I found a fix (posted below). If anyone has any better ideas or root causes, please post here.

share|improve this question

here is what I ended up getting to work...
never could get the window.close function to work; even in the setTimeout as shown above

I tested this on:
    windows XP : Chrome20,Firefox12,IE8
    Android gingerbread : android browser
    Android Ice Cream : android browser, Firefox
    Ipad : default browser (i assume its safari)
    Iphone 3gs and 4s : default


<SCRIPT LANGUAGE=\"JavaScript\">
    function refresh() {
        var sURL = unescape("http://(some web page)/");
        window.location.replace(sURL);
    }
    function closeWindow() {
        var isiPad = navigator.userAgent.match(/iPad/i) != null;
        var isiPhone = navigator.userAgent.match(/iPhone/i) != null;
        if (isiPad || isiPhone) {
           setTimeout( \"refresh()\", 300 );
        } else {
           window.close();
        }
    }
</SCRIPT>

...... and the html code .......

<p><input class="bigbutton" type="button" name="cancel" id="cancel" value="Cancel" onClick="closeWindow()"></p>

share|improve this answer
up vote 3 down vote accepted

After some searching, I found this tweet which posts a workaround - https://twitter.com/#!/gryzzly/statuses/177061204114685952 by @gryzzly

Copied here in full

window.close() doesn't work on iOS after window.open()ing or target="_blank"? do setTimeout(window.close, timeout); where timeout > 300.

This along with removing a .focus() in which I focus on the parent window before closing the new window completely solved the problem for me.

share|improve this answer
2  
tweet poster here! better suggestion is to actually use window.addEventListener("load", window.close); – gryzzly Mar 6 '13 at 15:12
    
I've moved on from that project so I'm not sure whether I had actually tried 'load' or not, but that makes sense. Will file it away for the future, thanks! – JohnP Mar 7 '13 at 20:52
2  
this does not seem to work on iOS8 safari – walnutmon Oct 14 '14 at 18:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.