Skip to content

Commit

Permalink
use addEventListener instead of onclick
Browse files Browse the repository at this point in the history
Also use attachEvent as a fallback. This page will only works with
everything but old IE so this fallback will only be useful to display
error messages in IE 8 :)
  • Loading branch information
dduponchel committed Feb 20, 2014
1 parent 973b44a commit 86c4cb4
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions examples/download-zip-file.html
Expand Up @@ -28,19 +28,30 @@ <h2>The Blob URL / saveAs</h2>
var zip = new JSZip();
zip.file("Hello.txt", "Hello world\n");

function bindEvent(el, eventName, eventHandler) {
if (el.addEventListener){
// standard way
el.addEventListener(eventName, eventHandler, false);
} else if (el.attachEvent){
// old IE
el.attachEvent('on'+eventName, eventHandler);
}
}

// data URI
var dataUriLink = document.getElementById('data_uri');
dataUriLink.onclick = function () {
function downloadWithDataURI() {
dataUriLink.href = "data:application/zip;base64," + zip.generate({type:"base64"});
dataUriLink.download = "hello.zip";
}
var dataUriLink = document.getElementById('data_uri');
bindEvent(dataUriLink, 'click', downloadWithDataURI);

// Blob
var blobLink = document.getElementById('blob');
var saveAs = window.saveAs || (navigator.msSaveBlob && navigator.msSaveBlob.bind(navigator))
var blobDownloadSupported = JSZip.support.blob && (saveAs || window.URL && window.URL.createObjectURL);
if (blobDownloadSupported) {
blobLink.onclick = function () {
function downloadWithBlob() {
try {
var blob = zip.generate({type:"blob"});
if (saveAs) {
Expand All @@ -53,7 +64,8 @@ <h2>The Blob URL / saveAs</h2>
blobLink.innerHTML += " " + e;
}
return false;
};
}
bindEvent(blobLink, 'click', downloadWithBlob);
} else {
blobLink.innerHTML += " (not supported on this browser)";
}
Expand Down

0 comments on commit 86c4cb4

Please sign in to comment.