简体   繁体   English

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

[英]window.close() not working correctly in IE(11)/Chrome?

I'm making an AJAX call to a site which generates a query and then saves it to a .txt file.我正在对一个站点进行 AJAX 调用,该站点生成一个查询,然后将其保存到一个 .txt 文件中。
This file should be downloaded after the AJAX was done and close that download window.此文件应在 AJAX 完成后下载并关闭该下载窗口。
Howether IE closes it automatically and then tries to close the mainwindow, which shouldn't be closed.如何 IE 自动关闭它,然后尝试关闭应该关闭的主窗口。
Meanwhile Chrome only closes the download window which is what IE should do aswell..同时 Chrome 只关闭下载窗口,这也是 IE 应该做的。
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 : download.php 只是:

<?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添加到download.php

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

How about this then:那么这个怎么样:

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

.. should make IE not close anything else. .. 应该让 IE 不关闭其他任何东西。

This doesn't really answer your question, but offers an alternative.这并不能真正回答您的问题,但提供了另一种选择。

Try something like this in the jQuery code:在 jQuery 代码中尝试这样的事情:

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

.. and add headers to force download in 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");

In my project, this php code was run after clicking a submit button (form) and if I recall correctly, it just showed a download dialogue box without updating the address bar or showing an empty page.在我的项目中,这个 php 代码是在单击提交按钮(表单)后运行的,如果我没记错的话,它只是显示了一个下载对话框,而没有更新地址栏或显示一个空页面。

This will work if it is rad window如果它是 rad 窗口,这将起作用

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();
}

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

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