dongxuanchao1425 2015-03-10 11:45
浏览 76
已采纳

通过PHP标题下载文件后,将document.readyState的状态更改为“完成”('Content-Disposition:attachment ...'

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.

  • 写回答

1条回答 默认 最新

  • dpowt82802 2015-03-19 19:29
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)