gpt4 book ai didi

javascript - location.href 的回调函数

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:13:47 27 4
gpt4 key购买 nike

我们有以下代码

Javascript:

function Download() {
//Show Loading Spinner
location.href = "FileDownloadHandler.ashx";
//Some Code(Hiding Loading Spinner)
}

aspx 页面:

<input type="button" value="Download" onclick="Download();" />

“FileDownloadHandler”会将文件下载给用户。

实际结果:location.href 下的代码在未完成处理程序执行的情况下立即执行。

预期结果:我想停止执行“SomeCode”,直到处理程序完成执行。

我怎样才能做到这一点?

提前致谢。

最佳答案

终于找到了解决问题的方法。使用 jQuery 插件我们可以实现这一点。我已经实现了它并且工作起来很有魅力。

这个插件做文件下载,它有成功和失败的回调,所以我们可以控制文件下载完成后应该执行的代码。

在这里我们必须调用此插件而不是 location.href 来调用我们的 FileDownloadHandler,如下所示。

 $("#loading").show();

$.fileDownload('FileDownloadHandler.ashx', {

successCallback: function (url) {

$("#loading").hide();

},
failCallback: function (html, url) {

$("#loading").hide();
alert("Failed");

}
});

下面有更多详细信息

http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/

http://jqueryfiledownload.apphb.com/

http://github.com/johnculviner/jquery.fileDownload/blob/master/src/Scripts/jquery.fileDownload.js

关于javascript - location.href 的回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37811665/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com