简体   繁体   English

自动下载行为和后退按钮问题

[英]Auto-download behavior and back button issue

Instead of linking directly to files for download on a web site we link to a page that says "thank you for downloading". 我们没有直接链接到要在网站上下载的文件,而是链接到显示“感谢您下载”的页面。 The page has tracking codes on it so we know how many people have downloaded the file. 该页面上有跟踪代码,因此我们知道有多少人下载了该文件。 The page launches the download file using the jQuery code shown which adds a short delay after the page loads before the download begins. 该页面使用显示的jQuery代码启动下载文件,该jQuery代码在页面加载后增加了短暂的下载延迟。 The download location has a content disposition header so it always downloads properly in the browser leaving the "thank you for downloading" page visible. 下载位置具有内容配置标头,因此它始终在浏览器中正确下载,从而使“感谢您下载”页面可见。 This all works well. 这一切都很好。

The problem comes if the user carries on browsing past this page and then hits back. 如果用户继续浏览该页面然后回击,就会出现问题。 The download fires again. 下载再次触发。

Using window.location.replace(href); 使用window.location.replace(href); didn't seem to fix it. 似乎没有解决。

The issue is further complicated by the fact that the CMS delivering the page has set it to expire immediately so it's not being cached. 由于交付页面的CMS已将其设置为立即过期,因此不进行缓存,这一事实使问题变得更加复杂。

Suggestions for (i) ways to avoid this problem; 有关(i)避免此问题的方法的建议; (ii) any better ways to handle file download / thank you pages? (ii)处理文件下载/感谢页面的更好方法?

jQuery Code jQuery代码

$(document).ready(function () {
    $('a.autoDownload').each(function () {
        setTimeout('navigateToDownload("' + $(this).attr('href') + '")', 4000);
    });
});

function navigateToDownload(href) {
    document.location.href = href;
}

One possible approach would be to set a cookie via Javascript when the page first loads. 一种可能的方法是在页面首次加载时通过Javascript设置cookie。 Then, if that page is ever loaded again, you can check for the presence of the cookie, and if present, do not execute the auto download? 然后,如果该页面再次被加载,则可以检查cookie是否存在,如果存在,是否不执行自动下载?

Using the Cookie plugin for jQuery as an example: 以jQueryCookie插件为例:

$(document).ready(function () {
    $('a.autoDownload').each(function () {
        var hasDownloadedThisLink = $.cookie("site." + $(this).attr('id'));
        if (!hasDownloadedThisLink) {
            $.cookie("site." + $(this).attr('id'), "true");
            setTimeout('navigateToDownload("' + $(this).attr('href') + '")', 4000);
        }   
    });
});

This is just an example. 这只是一个例子。 If you went this way, you'd have to consider how many possible download links there might be, as there is a limit on how many cookies you can set. 如果您采用这种方式,则必须考虑可能有多少个下载链接,因为可以设置的Cookie数量是有限的。 Also notice that I used an id attribute of the links to identify them in the cookie - I figured this would be more suitable that using some form the href attribute. 还要注意,我使用链接的id属性在cookie中标识它们-我认为这比使用某种形式的href属性更合适。 I also prefixed the cookie name with site. 我还为cookie名称加上了site.前缀site. .

Well there are a couple of solutions to this. 好了,有两种解决方案。 Here's an example: 这是一个例子:

function navigateToDownload(href){
    var e = document.createElement("iframe");
    e.src=href;
    e.style.display='none';
    document.body.appendChild(e);
}

Other implemenatations might exist, but I doubt they're less "hacky". 可能存在其他实现,但是我怀疑它们是否不太“ hacky”。

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

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