首先运行JQuery,然后在单击链接时下载文件 [英] Run JQuery first and then download files when link clicked

查看:71
本文介绍了首先运行JQuery,然后在单击链接时下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接,其中包含直接文件下载URL.因此,当用户单击链接时,该文件就会下载.

I have a link which has direct file download URL in it. So when a user click on the link, that file downloads.

这里是链接:

<a class="download-video" href="http://mylink.mp4?file=1&name=A Video"> Download</a>

我希望在下载此文件之前运行一些JQuery.我可以使用一种解决方案,但是我没有直接下载,而是重定向到了视频.我想要的是下载而没有任何重定向.这是我尝试过的:

I was hoping to run some JQuery before this file downloads. I was able to work a solution, but instead of direct downloading, I am redirected to the video. All I want is to download withouth any redirection. This is what I tried:

$('a.download-video').click(function(event) {

    var href = this.href;
    //My JS Code
    alert(href);

    event.preventDefault();

   window.location = href;
});

谢谢

推荐答案

您可以添加下载属性赋予锚标记,然后就不要使用preventDefault();

You can add the download attribute to the anchor tag and then just don't use preventDefault();

$('a.download-video').click(function(event) {

    var href = this.href;
    //My JS Code
    alert(href);

});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="download-video" href="https://www.w3schools.com/html/mov_bbb.mp4?file=1&name=A Video" download> Download</a>

这是一个链接,以实现浏览器兼容性.

这篇关于首先运行JQuery,然后在单击链接时下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆