无法读取属性'null"javascript错误
"Cannot read property 'addEventListener' of null" error in javascript
我有一个将文本保存为txt文件的函数。它自己工作(当我有一个简单的html和js文件),但当我把它添加到我的程序它抛出这个错误:
无法读取null属性'addEventListener'
(function(view) {
"use strict";
var document = view.document,
$ = function(id) {
return document.getElementById(id);
},
session = view.sessionStorage
// only get URL when necessary in case Blob.js hasn't defined it yet
, get_blob = function() {
return view.Blob;
}
text = "slavik's text";
save_file = $('saveFile');
text_filename = "filename.txt";
documemt.getElementById('saveFile').addEventListener("submit", function(event) {
event.preventDefault();
var BB = get_blob();
saveAs(
new BB(
[text.value]
, {type: "text/plain;charset=" + document.characterSet}
)
, (text_filename.value) + ".txt"
);
}, false);
view.addEventListener("unload", function() {
session.text = text.value;
session.text_filename = text_filename.value;
}, false);
}(self));
html部分:
<div id="leftColumn">
<ul>
<li><a href="#" onclick="vaucherEntry(); return false; showVauchers()">Vaucher Entry</a></li>
<li><a href="#" onclick="settings(); return false;">Settings</a></li>
<li><a href="#" onclick="about(); return false;">About</a></li>
<!--<button type="submit" onclick="saveFile()" id='saveFile'>Save File</button>-->
<form id="saveFile">
<input type="submit" value="Save file"/>
</form>
</ul>
</div>
我已经更新了你的代码,所以它看起来更清晰,我删除了一些打字错误,所以它现在没有抛出任何错误。
我刚刚创建了函数savea,因为它在你的逻辑中是需要的,并且缺失了,所以检查与你的代码的差异并修复它。
(function(view) {
"use strict";
var saveAs = function(param1, param2, param3) {
console.log('you should do something with this params:');
console.log(param1, param2);
},
document = view.document,
$ = function(id) {
return document.getElementById(id);
},
session = view.sessionStorage,
// only get URL when necessary in case Blob.js hasn't defined it yet
get_blob = function() {
return view.Blob;
},
text = "slavik's text",
save_file = $('saveFile'),
text_filename = "filename.txt";
document.getElementById('saveFile').addEventListener("submit", function(event) {
event.preventDefault();
var BB = get_blob();
saveAs(new BB([text], {type: "text/plain;charset=" + document.characterSet }), text_filename);
}, false);
document.addEventListener("unload", function() {
session.text = text.value;
session.text_filename = text_filename.value;
}, false);
}(self));
小提琴
相关文章:
- 字符串在将其传递给另一个活动Android JavaScript时读取Null
- 来自文本输入null的html javascript变量
- html+javascript:如何在某些条件下禁用dropdownlist中的null选项
- 当null应该在javascript中使用而不是未定义时
- Javascript Float32数组抛出无法读取属性'0'即使数组定义良好,也为null
- 全局Javascript变量变为null
- 验证对象是否为null Javascript/Angularjs
- 如何在JavaScript中检查变量是否为null和/或未定义
- 无法设置属性'innerHTML'在javascript中为null
- Javascript将变量作为null传递
- 加载的XML文档为null(Javascript)
- ASP.net Javascript函数中的隐藏字段为null
- javascript,HTML表单:单击按钮插入NULL
- Variable is null - javascript
- 如何使用文档检测全屏.msFullscreenElement !== null (javascript)
- appendChild null | javascript/html
- 无法读取property 'length'null (javascript)
- ajax xmlhttp response is null - javascript
- 无法读取属性'addEventListener'为null(JavaScript帆船应用程序)
- 无法读取属性'null"javascript错误