gpt4 book ai didi

javascript - 未定义 saveTextAsFile

转载 作者:行者123 更新时间:2023-11-29 15:33:42 24 4
gpt4 key购买 nike

我正在尝试保存到文件,但出现错误“saveTextAsFile is not defined”,见下文

<script type='text/javascript' src='SaveTextAsFile.js'></script>

<textarea id="inputTextToSave" style="width:512px;height:256px"></textarea>

<table>
<tr>
<td>Filename to Save As:</td>
<td><input id="inputFileNameToSaveAs"></input></td>
<td><button onclick="saveTextAsFile()">Save Text to File</button></td>
</tr>
</table>

我在文件 SaveTextAsFile.js 中有这个函数 saveTextAsFile() 在与 html 相同的目录中:

function saveTextAsFile()
{
var textToWrite = document.getElementById("inputTextToSave").value;
var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value;

var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
if (window.webkitURL != null)
{
// Chrome allows the link to be clicked
// without actually adding it to the DOM.
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
}
else
{
// Firefox requires the link to be added to the DOM
// before it can be clicked.
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
}

downloadLink.click();
}

最佳答案

在你的脚本标签 src 的开头添加一个斜杠,使它对你的主机来说是绝对的,否则,如果你在一个 url 中说 "localhost/foo/bar/",浏览器将尝试从 "localhost/foo/bar/SaveTextAsFile.js" 加载文件,如果您添加斜线,它将尝试从 "localhost/SaveTextAsFile.js"加载文件

<script type='text/javascript' src='/SaveTextAsFile.js'></script>

关于javascript - 未定义 saveTextAsFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31885936/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com