0

We have recently encountered an issue in our code with iPads after upgrading to iOS 9. For various reasons we open an empty window and start writing into the document once it becomes available. This usually works fine and we have various tests in place for different browser behaviour etc., but the new problem in iOS seems problematic.

When you open a new window with window.open the returned reference immediately has a document and using document write works without error. You can check the content etc. everything has worked. Then iOS decides to replace the document with a new one. Since we managed to successfully write in the first place we don't try to write again so users would see an empty page. Writing into the new document works as expected and is actually being shown to the user. Just randomly checking if the document has changed or detecting the user agent seems like a hacky solution so the question is:

Is there a better way to handle this (any events being fired) any difference on the documents/window reference to check against. Or does anyone know why iOS9 is doing this?

Here is a codepen with the working/not working code for iOS 9 http://codepen.io/anon/pen/epapGL

    function openWindow(twice) {
        win = window.open('about:blank', '_blank', 'width=500,height=700');
        if (win.document) {
            originalDocument = win.document;
            win.document.write('<div>This should appear in the opened window.</div>');
            noticeDiv.innerHTML = 'First write successful';
        }
        if (twice) {
            setTimeout(writeAgain, 0);
        }
    }

    function writeAgain() {
        if (win.document !== originalDocument) {
            win.document.write('<div>This is the second try. The document was replaced.</div>');
            noticeDiv.innerHTML = 'Second write successful';
        }
    }

Edit: This is not about adding content in general, it is about the iOS 9 specific behaviour.

Edit 2: I have now opened a bug report with Apple for this. I'll see what they say.

noVerity
  • 3
  • 3
  • Have you tried to use the `data` URL scheme? Something like `data:text/html,
    Hello world!
    `
    – kabiroberai Nov 23 '15 at 16:12
  • Possible duplicate of [Add content to a new open window](http://stackoverflow.com/questions/10472927/add-content-to-a-new-open-window) – Spencer Wieczorek Nov 23 '15 at 16:13
  • I don't think this is a duplicate as the problem isn't writing itself, but the reference to the document changing after the window has opened. I'll look into data uri, but it sounds like it wouldn't be usable for IE http://caniuse.com/#feat=datauri and I wouldn't want to maintain two solutions unnecessarily. – noVerity Nov 23 '15 at 16:25

1 Answers1

0

I found something similar. A solution to the problem seems to be modifying this:

win = window.open('', '_blank', 'width=500,height=700');

that means, omit the 'about:blank', just leave it empty. Works also in your code example on codepen.

I have no idea why, and my programming skills are limited. But I had the same issue with some forum software and managed to pinpoint it down to that.

Cheers, Thomas

Thomas
  • 16
  • That does seem to solve things. Odd problem, I might change the url to the empty string for iOS. – noVerity Dec 01 '15 at 12:00
  • Hi Vertity! Looks like the new iOS 9.2 update from today (December 12th 2015) sorts out the thing. I checked your example on Codepen, and it seems to open the first attempt properly. Even w/o any modification of your code. Can you confirm this? – Thomas Dec 09 '15 at 20:38