Last active
July 28, 2018 17:17
-
-
Save priyanshujain/85c4ea8920e952915e9d72a2ffd841f3 to your computer and use it in GitHub Desktop.
Download data as file in browser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function download(data, filename, type) { | |
var file = new Blob([data], {type: type}); | |
if (window.navigator.msSaveOrOpenBlob) // IE10+ | |
window.navigator.msSaveOrOpenBlob(file, filename); | |
else { // Others | |
var a = document.createElement("a"), | |
url = URL.createObjectURL(file); | |
a.href = url; | |
a.download = filename; | |
document.body.appendChild(a); | |
a.click(); | |
setTimeout(function() { | |
document.body.removeChild(a); | |
window.URL.revokeObjectURL(url); | |
}, 0); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Download all hyperlinks of a page | |
link_tags = document.getElementsByTagName('a') | |
var links = [] | |
for(var i=0; i<link_tags.length;i++) { | |
links.push({ | |
url: link_tags[i].href, | |
name: link_tags[i].innerText | |
}) | |
} | |
for(var i=0;i<links.length;i++) { | |
var link = document.createElement("a"); | |
link.download = links[i].name; | |
link.href = links[i].url; | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link); | |
delete link; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment