javascript - 使用 window.location.href 后没有输出到 iframe

标签 javascript php html

我在 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
?>

这是在 javascript 行之后不会输出到 iframe 的 php 文件的示例:

echo 'Test Output';

提前致谢

周杰伦

编辑

我用以下代码替换了 javascript 行,它运行良好:

echo "<iframe id='my_iframe' style='display:none;' src='download.php'></iframe>";

最佳答案

您不需要将重定向放在 iframe 页面内。

我能想到的最好的 atm 方法是使用 Andrew Dunn 对此 question 的回答中提到的隐形框架。

引用该答案:

<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。

关于javascript - 使用 window.location.href 后没有输出到 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37626217/

相关文章:

javascript - Backbone Handlebars 变量返回 0(零)

php - 我可以在 PHP 中重载数组下标运算符吗?

javascript - 无法在node.js中显示带有EJS文件的图像

javascript - 单击其子项时检测 div

javascript - Ajax 在 Safari 中加载图像时不考虑 srcset

php - 如何在Mysql中实现Join来获取以下记录

php - 如何使用 PHP 在我的服务器上验证 Android 生成的 token ?

javascript - 使用 Highcharts-ng 不显示图表

javascript - 如何使背景图像滚动速度比页面慢?

javascript - Three.js和gsap比例动画