Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

use addEventListener instead of onclick

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...
commit 86c4cb4d17ceaac949d3a36537cc66dde8164a0c 1 parent 973b44a
@dduponchel dduponchel authored
Showing with 16 additions and 4 deletions.
  1. +16 −4 examples/download-zip-file.html
View
20 examples/download-zip-file.html
@@ -28,19 +28,30 @@
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) {
@@ -53,7 +64,8 @@
blobLink.innerHTML += " " + e;
}
return false;
- };
+ }
+ bindEvent(blobLink, 'click', downloadWithBlob);
} else {
blobLink.innerHTML += " (not supported on this browser)";
}
Please sign in to comment.
Something went wrong with that request. Please try again.