[英]No output to iframe after using window.location.href
我在php函数中使用window.location.href强制文件下载而不打开另一个窗口。 文件下载工作,其余的PHP脚本执行。 但是,我在函数中使用javascript后输出的内容都不会显示在iframe中。 有没有办法将输出重定向回原始的PHP脚本?
这是php文件中的javascript行:
echo "<script>window.location.href='download.php';</script>";
以下是download.php的代码:
<?php
session_start(); // Starts new or resumes existing session
$filename = $_SESSION['filename']; // Creates variable from session variable
header("Content-Type: text/plain"); // Output text file
header("Content-Disposition: attachment; filename=$filename"); // Output file name
readfile($filename); // Outputs the text file
?>
这是php文件中的一些示例,它不会在javascript行之后输出到iframe:
echo 'Test Output';
提前致谢,
松鸦
编辑
我用以下内容替换了javascript行,它完美地运行:
echo "<iframe id='my_iframe' style='display:none;' src='download.php'></iframe>";
您无需将重定向放在iframe页面中。
我能想到的最好的方法是使用Andrew Dunn对这个问题的答案中提到的一个看不见的框架
引用那个答案:
<iframe id="my_iframe" style="display:none;"></iframe>
<script>
function Download(url) {
document.getElementById('my_iframe').src = url;
};
</script>
在主页面中有这个。 url基本上是下载URL(在你的情况下是“download.php”)。 您可以使用按钮制作下载手册。 如果你想要它被迫。 将src = url添加到iframe并删除脚本。
我建议研究和使用jQuery。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.