简体   繁体   English

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

[英]No output to iframe after using window.location.href

I am using window.location.href in a php function which forces a file download without opening an additional window. 我在php函数中使用window.location.href强制文件下载而不打开另一个窗口。 The file download works and the rest of the php script executes. 文件下载工作,其余的PHP脚本执行。 However, nothing that I output after the line with the javascript in the function will show up in the iframe. 但是,我在函数中使用javascript后输出的内容都不会显示在iframe中。 Is there a way to redirect the output back to the original php script? 有没有办法将输出重定向回原始的PHP脚本?

Here is the javascript line from the php file: 这是php文件中的javascript行:

echo "<script>window.location.href='download.php';</script>";

Here is the code from download.php: 以下是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
?>

This is an example of something in the php file that will not output to the iframe after the line of javascript: 这是php文件中的一些示例,它不会在javascript行之后输出到iframe:

echo 'Test Output';

Thanks in advance, 提前致谢,

Jay 松鸦

EDIT 编辑

I replaced the line of javascript with the following and it works perfectly: 我用以下内容替换了javascript行,它完美地运行:

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

You don't need to put the redirect inside the iframe page. 您无需将重定向放在iframe页面中。

The best way I can think of atm is to use an invisible frame as mentioned in Andrew Dunn's answer to this question 我能想到的最好的方法是使用Andrew Dunn对这个问题的答案中提到的一个看不见的框架

Quoting from that answer: 引用那个答案:

<iframe id="my_iframe" style="display:none;"></iframe>
<script>
function Download(url) {
document.getElementById('my_iframe').src = url;
};
</script>

Have that inside your main page. 在主页面中有这个。 url is basically the download url ("download.php" in your case). url基本上是下载URL(在你的情况下是“download.php”)。 You can use a button to make the download manual. 您可以使用按钮制作下载手册。 If you want it forced. 如果你想要它被迫。 Add src=url to the iframe and remove the script. 将src = url添加到iframe并删除脚本。

I would advice researching and using jQuery. 我建议研究和使用jQuery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM