window.close()在IE(11)/ Chrome中无法正常工作?

问题描述 投票:0回答:3

我正在对一个生成查询的网站进行AJAX调用,然后将其保存到.txt文件中。在完成AJAX之后,应下载此文件并关闭该下载窗口。Howether IE会自动关闭它,然后尝试关闭主窗口,该主窗口不应关闭。同时,Chrome仅关闭IE应该执行的下载窗口。有没有解决方法?

function start(){
   $.ajax({
        url: ('query.php'),
        type: 'POST',
        async:false,
        data: { 
        id: id,
        intnr: intnr
        },
        dataType: "html"
         })
          .done (function(response) { window.open('download.php');  window.close('download.php'); })
          .fail (function(xhr, textStatus, errorThrown) { alert(xhr.responseText); })
        ;
}

download.php只是:

<?php
header ( 'Content-Type: text/html');
header ( "Content-Disposition: 'attachment'; filename='File.txt'");
include ('/xx/xx/query.txt');
?>

EDIT:解决方法,但现在可以使用。缩短功能到

.done (function(response) { var download_window = window.open('download.php'); })

添加到download.php中

<script>
var s = navigator.userAgent; 
if(s.indexOf("Chrome") > -1 == true) 
{ 
window.open('', '_self', ''); 
window.close();
}
</script>
php ajax internet-explorer google-chrome download
3个回答
2
投票

然后呢:

.done (function(response) {
    var download_window = window.open('download.php');
    download_window.close();
 })

..应该使IE不能关闭其他任何对象。


1
投票

这并没有真正回答您的问题,但是提供了替代方法。

尝试用类似的方法在jQuery代码中:

.done (function(response) { window.location.href = "download.php"; })

..并添加标题以强制在download.php中下载:

header("Content-Description: File Transfer");
header("Content-Type: application/download");
header("Content-Type: application/force-download");
// Removed this from my code.
// header("Content-Type: application/octet-stream");
// Added this for yours.. not sure exactly what's optimal for your case.
header("Content-Type: text/html");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=File.txt");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Expires: 0");
header("Pragma: public");

ob_clean();
flush();
readfile("/xx/xx/query.txt");

在我的项目中,此php代码是在单击提交按钮(窗体)后运行的,如果我没记错的话,它只是显示了一个下载对话框,而没有更新地址栏或显示空白页面。


0
投票

如果是弧形窗口,则可以使用

function GetRadWindow() {
                var oWindow = null;
                if (window.radWindow)
                    oWindow = window.radWindow;
                else if (window.frameElement && window.frameElement.radWindow)
                    oWindow = window.frameElement.radWindow;
                return oWindow;
            }

function selfClose(){
GetRadWindow().close();
}
© www.soinside.com 2019 - 2024. All rights reserved.