1function download(filename, text) {
2 var element = document.createElement('a');
3 element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
4 element.setAttribute('download', filename);
5
6 element.style.display = 'none';
7 document.body.appendChild(element);
8
9 element.click();
10
11 document.body.removeChild(element);
12}
13
14// Start file download.
15download("hello.txt","This is the content of my file :)");
16
1function download(filename, text) {
2 var element = document.createElement('a');
3 element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
4 element.setAttribute('download', filename);
5
6 element.style.display = 'none';
7 document.body.appendChild(element);
8
9 element.click();
10
11 document.body.removeChild(element);
12}
1$('a#someID').attr({target: '_blank',
2 href : 'http://localhost/directory/file.pdf'});
1var fileContents = "Hello world!";
2var filename = "hello.txt";
3var filetype = "text/plain";
4
5var a = document.createElement("a");
6dataURI = "data:" + filetype +
7 ";base64," + btoa(fileContents);
8a.href = dataURI;
9a['download'] = filename;
10var e = document.createEvent("MouseEvents");
11// Use of deprecated function to satisfy TypeScript.
12e.initMouseEvent("click", true, false,
13 document.defaultView, 0, 0, 0, 0, 0,
14 false, false, false, false, 0, null);
15a.dispatchEvent(e);
16a.removeNode();
17