早睡早起,方能养生
Sleep early rise early, way to keep healthy

Javascript生成文件.js并下载

super
2024-03-20 17:12
views 928

// 创建要写入文件的内容
var jsCode = `
function helloWorld() {
    console.log("Hello, World!");
}
`;

// 创建 Blob 对象
var blob = new Blob([jsCode], { type: 'text/javascript' });

// 创建下载链接
var downloadLink = document.createElement("a");
downloadLink.href = window.URL.createObjectURL(blob);
downloadLink.download = "myScript.js";

// 将链接添加到页面中,并触发下载
document.body.appendChild(downloadLink);
downloadLink.click();

// 移除链接
document.body.removeChild(downloadLink);


分享
0 条讨论
top