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

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条)

报告相同问题?