简体   繁体   English

在音频下载链接上触发点击事件

[英]Trigger click event on an audio download link

<a href="http://www.somesite.com/sometrack.mp3" onclick="trackDownload('sometrack.mp3')">
    <button> Download </button>
</a>

I need to trigger a download as well as trigger the JavaScript function trackDownload(). 我需要触发下载以及触发JavaScript函数trackDownload()。

trackDownload() is an Ajax function, so ideally after completing the ajax call we need to trigger the download. trackDownload()是Ajax函数,因此理想情况下,完成ajax调用后,我们需要触发下载。

But unfortunately this does not happen. 但不幸的是,这不会发生。 I believe its because the page navigates to the download link. 我相信这是因为页面导航至下载链接。

Is there a workaround? 有解决方法吗? I did think of a JS function that does the tracking and redirect to link. 我确实想到了执行跟踪并重定向到链接的JS函数。 But thought of taking a second opinion to stack overflow experts. 但是考虑采取第二种意见来堆积溢出专家。

What can you do is attach even to button instead if link and return false for anchor 你能做的是,即使链接也附加到按钮上,并为锚return false

HTML HTML

<a href="http://www.somesite.com/sometrack.mp3" onclick="return false;">
    <button id="button"> Download </button>
</a>

Jquery jQuery的

$(function () {
    $('#button').on('click', function () {
        trackDownload();
        alert($(this).parents('a').attr('href'));
        window.location = $(this).parents('a').attr('href');
    });
});

function trackDownload() {
    alert('Track is getting download');
}

if you want to open in new tab use window.open($('#myanchor').attr('href')); 如果要在新选项卡中打开,请使用window.open($('#myanchor').attr('href'));

DEMO DEMO

As specified that trackDownload() in comments that it is ajax function what you can do is 如注释中指定的trackDownload()是ajax函数,您可以做的是

   $('#button').on('click', function () {
        trackDownload($(this).parents('a')); // pass your link to this function

    });

function trackDownload(element) {
  $.ajax(
       url: 'yoururl',
       type: 'post',
       ... // all other parameter you want to pass
       success: function () { // success when ajax is completed
           window.location = element.attr('href'); // change url only when ajax is success
       }
}

But if you want to attach click event to link only then you can do the same, just attache the event to link 但是,如果您只想将click事件附加到链接,则可以执行相同操作,只需将事件附加到链接

<a id='music' href="http://www.somesite.com/sometrack.mp3" onclick="return false;">
     <button> Download </button>
</a>

JS JS

   $(function () {
        $('#music').on('click', function () {
            trackDownload();
            alert($(this).attr('href'));
            window.location = $(this).attr('href');
        });
    });

You can also look at this for more alternatives How can I simulate a click to an anchor tag? 您还可以查看更多替代方法。 如何模拟对锚标记的单击?

<a href="#" onclick="trackDownload('sometrack.mp3')">
    <button> Download </button>
</a>

<script>
    function trackDownload(file)
    {
        // other function body here....
        window.open('http://www.somesite.com/'+file, '_blank');
    }
</script>

Thanks for all the answers , It definitely did enlightened my hunt for the best solution. 感谢您提供所有答案,它的确为我寻求最佳解决方案提供了启发。

@Raunak's answer is did find a solution for my question but the use of @Raunak的答案确实找到了我的问题的解决方案,但使用了

onclick="return false;" 

on the anchor tag is however is not a right solution. 但是,将锚标签上的标签放错是不正确的解决方案。 Since it triggered multiple events on the Jquery dom engine. 由于它在Jquery dom引擎上触发了多个事件。

What I really need was to prevent the default action of the link and call the ajax function. 我真正需要的是防止链接的默认操作并调用ajax函数。

So I have added event.preventDefault(); 所以我添加了event.preventDefault(); on the click event followed by the ajax function and loaded the download address after on the success event of ajax call. 单击事件后跟ajax函数,并在ajax调用成功事件后加载下载地址。

  $('#container a').click(function(event){
     event.preventDefault(); // This is what is different from @Raunak's answer

     var el = $(this); // $(this).href may not be valid inside ajax function
                       // due to disambiguation of $(this) 
                       // hence storing the element on a local veriable

     $.post('http://someURL', { info: someInfo})
     .done(function(data) {
        location.href = el.attr('href');
     });

  });

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

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