Franz Payer
Franz Payer

Reputation: 4137

Download Attribute Overriding onClick <a>

I am using the download attribute to trigger a forced download of a song, but it seems to be overriding the onClick event. I can only do one or the other.

<output></output>
<input onclick="window.close()" type="button" value="Cancel"/>

<script>
var a = document.createElement("a");
a.download = song + ".mp3"
a.href = url
a.onclick = function(e) {
    var notification = webkitNotifications.createHTMLNotification(
        'notification2.html'
    );
    notification.show();
    window.close();
}
document.getElementsByTagName("output")[0].appendChild(a);
document.getElementsByTagName("a")[0].innerHTML = "Download"
</script>

How do I both trigger the download, and fire the onClick event?

Upvotes: 0

Views: 771

Answers (1)

apsillers
apsillers

Reputation: 115910

Try using addEventListener:

a.addEventListener("click", function (e) {
    var notification = webkitNotifications.createHTMLNotification(
        'notification2.html'
    );
});

Upvotes: 1

Related Questions