My question is this: after downloading a file via PHP's header('Content-Disposition: attachment
... technique, the webpage that originated the download has a document.readyState
value of interactive
.
I would expect this to be complete
instead, as the downloading is complete.
Am I missing something, or is this the expected behavior?
If the latter, then is there any way to reset the document.readyState
back to complete
?
(This is using Internet Explorer, if it makes a difference...)
Background:
I have some JavaScript functions that I want active only when the page finishes loading, so I use
if( document.readyState != "complete" ) return;
at their start to achieve this goal. This works properly with the page loading, as document.readyState
is interactive
while loading progressively, and is complete
only upon completion of the page load.
Later in the page, I present a link which sends the user to the below PHP code to allow download of the data loaded:
header( 'Content-Type: text/csv; charset=Shift-JIS' );
header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
$output = fopen( 'php://output', 'w' );
//write to file output...
fclose( $output );
Now, pressing on this link causes document.readyState
to be first loading
and then interactive
, but the state stays there, never moving to complete
, thus disabling the earlier JavaScript functions...
I am at a loss of why this is so, and would appreciate any help, in any direction.
The first time your page has a readyState
of complete
, you know it has loaded fully and your scripts can run. When the user clicks a link that doesn't move away from the page (such as a page that only performs a file download) you could ignore the resulting readyState
. So instead of checking the readyState
in all your scripts, you could check whether the page has ever been in the complete
readyState
.
The rationale behind setting the readyState
to interactive
could be that technically, a different page has loaded, but was never rendered, and didn't make it into the history. But I'm just guessing. As you've seen in your fiddle, the behavior seems inconsistent.