Last active
December 9, 2022 11:54
-
-
Save dcts/e9a9a1bb5276aadb113cf5d4c8307666 to your computer and use it in GitHub Desktop.
download_object_as_json.js
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
// taken from here => https://stackoverflow.com/a/30800715/6272061 | |
function downloadObjectAsJson(exportObj, exportName){ | |
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj, null, 2)); | |
var downloadAnchorNode = document.createElement('a'); | |
downloadAnchorNode.setAttribute("href", dataStr); | |
downloadAnchorNode.setAttribute("download", exportName + ".json"); | |
document.body.appendChild(downloadAnchorNode); // required for firefox | |
downloadAnchorNode.click(); | |
downloadAnchorNode.remove(); | |
} | |
// download data | |
x = { a: 1, b: 2 }; | |
downloadObjectAsJson(x, "myFilename"); | |
function downloadFileFromString(filename, text) { | |
var element = document.createElement('a'); | |
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); | |
element.setAttribute('download', filename); | |
element.style.display = 'none'; | |
document.body.appendChild(element); | |
element.click(); | |
document.body.removeChild(element); | |
} | |
function copyToClipboard(str) { | |
window.prompt("Copy to clipboard: Ctrl+C, Enter", str); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment