JS保存内容到JSON和TXT文件

downloadJsonOrTxt('demo.json', JSON.stringify({a:"b"}))    
<script>
    function downloadJsonOrTxt(filename, text) {
        var pom = document.createElement('a');
        pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
        pom.setAttribute('download', filename);
        if (document.createEvent) {
            var event = document.createEvent('MouseEvents');
            event.initEvent('click', true, true);
            pom.dispatchEvent(event);
        } else {
            pom.click();
        }
    }
</script>

文章来源于:https://yuefoo.com/236.html