1
|
/**
|
2
|
* Generate a file to be downloaded by the browser
|
3
|
* @param filename name of the file to be downloaded
|
4
|
* @param text content of the downloaded file
|
5
|
*/
|
6
|
function download(filename, text) {
|
7
|
// first a hidden <a> element is created and download content is assigned
|
8
|
var element = document.createElement('a');
|
9
|
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
|
10
|
element.setAttribute('download', filename);
|
11
|
|
12
|
element.style.display = 'none';
|
13
|
document.body.appendChild(element);
|
14
|
|
15
|
// generate artificial click event to download the file
|
16
|
element.click();
|
17
|
|
18
|
// remove the download link from the webpage
|
19
|
document.body.removeChild(element);
|
20
|
}
|
21
|
|
22
|
// Adds toDateInputValue() method to Date object
|
23
|
// toDateInputValue() produces date in YYYY-MM-DD format
|
24
|
Date.prototype.toDateInputValue = (function() {
|
25
|
var local = new Date(this);
|
26
|
local.setMinutes(this.getMinutes() - this.getTimezoneOffset());
|
27
|
return local.toJSON().slice(0,10);
|
28
|
});
|