doulv1760 2014-06-26 10:01
浏览 230
已采纳

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

I'm making an AJAX call to a site which generates a query and then saves it to a .txt file.
This file should be downloaded after the AJAX was done and close that download window.
Howether IE closes it automatically and then tries to close the mainwindow, which shouldn't be closed.
Meanwhile Chrome only closes the download window which is what IE should do aswell..
Is there a workaround for this?

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 is just :

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

EDIT : Workaround but it is working now..
shortened function to

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

added into download.php

<script>
var s = navigator.userAgent; 
if(s.indexOf("Chrome") > -1 == true) 
{ 
window.open('', '_self', ''); 
window.close();
}
</script>
  • 写回答

2条回答 默认 最新

  • dsx666666 2014-06-26 11:25
    关注

    How about this then:

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

    .. should make IE not close anything else.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!