我有一个javascript,当点击一个按钮时我应该创建一个文本文件的下载链接,但是我得到一个"无效的左侧表达式"错误。
js看起来像这样:
(function () {
var textFile = null,
var makeTextFile = function (text) {
var data = new Blob([text], {type: 'text/plain'});
// If we are replacing a previously generated file we need to
// manually revoke the object URL to avoid memory leaks.
if (textFile !== null) {
window.URL.revokeObjectURL(textFile);
}
textFile = window.URL.createObjectURL(data);
return textFile;
};
var create = document.getElementById('create');
create.addEventListener('click', function () {
var link = document.getElementById('downloadlink');
link.href = makeTextFile({{request_string}});
link.style.display = 'block';
}, false);
})();
我也试过这样做:
var text_string = {{request_string}}
link.href = makeTextFile(text_string);
{{request_string}}
看起来像这样:
----------------Request File Generated 2015-06-30 17:23:16.324439------------------
WINDOW_START | WINDOW_END | DURATION | SAT | TYPE | PRIORITY |
--------------------------------------------------------------------------------
2015/01/02 00:00:00 | 2015/01/22 00:00:00 | 500 | F7 | NOMINAL | 5 |
html看起来像这样:
<div class="panel panel-body">
<button id="create">Create file</button>
<a download="info.txt" id="downloadlink" style="display: none">Download</a>
</div>
{{request_string}}
是str类型,所以我不确定问题是什么。谁能告诉我我做错了什么?
谢谢!
答案 0 :(得分:0)
我最终使用document.getElementsByTagName('pre')
而不是尝试传入包含字符串的变量。