javascript 我们可以使用javascript创建日志文件吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9866835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we create log files using javascript
提问by user1275375
Instead fo displaying in alert box I want to direct my output from javascript to a log file. Is there any method for doing it. If so please explain it with an example.
而不是显示在警报框中,我想将我的输出从 javascript 定向到日志文件。有没有什么方法可以做到。如果是这样,请举例说明。
采纳答案by user1289347
Yes, using the google chrome browser hit the f12 key, and click on the console button. Then use
是的,使用 google chrome 浏览器按 f12 键,然后单击控制台按钮。然后使用
console.log(your code);
you can log objects, arrays, strings ,variables. Much more useful than alerts.
您可以记录对象、数组、字符串、变量。比警报有用得多。
Also in firefox the firebug plugin is quite useful. Has similar functionality, and adds the inspect element function that google chrome has built in.
同样在 firefox 中,firebug 插件非常有用。具有类似的功能,并增加了谷歌浏览器内置的检查元素功能。
EDIT: Ok, based on your comment, you cannot just write to their file system. The browser will not let you. If you want something unobtrusive try something like a classy modal window or overlay, something that is optional for the user to interact with rather than the annoying alerts and confirms. You could even add something like this http://davidwalsh.name/dw-content/top-bar-opacity.php
编辑:好的,根据您的评论,您不能只写入他们的文件系统。浏览器不会让你。如果您想要一些不显眼的东西,请尝试使用经典的模态窗口或叠加层之类的东西,这是用户可选择的交互方式,而不是烦人的警报和确认。你甚至可以添加这样的东西 http://davidwalsh.name/dw-content/top-bar-opacity.php
回答by Greg Hewgill
Most browsers support the window.console
object from the Console API:
大多数浏览器都支持window.console
来自Console API的对象:
console.log("Hello world");
回答by Steve
You could always send an AJAX call back to the server and track error messages there.
您始终可以将 AJAX 调用发送回服务器并在那里跟踪错误消息。
回答by Peter
Actually there is a way to do it but it is only available on Google Chrome and mostly for HTML5 applications packaged as extensions. There are plans to make it available in wider distributions but not quite there yet. It is called the FileSystem API. Here is an example I was playing with a while ago -
实际上有一种方法可以做到这一点,但它仅在 Google Chrome 上可用,并且主要用于打包为扩展程序的 HTML5 应用程序。有计划在更广泛的发行版中提供它,但还没有完全实现。它被称为FileSystem API。这是我前段时间玩的一个例子 -
// test HTML5 file system API
function onInitFs(fs){
console.log("Opened file system " + fs.name);
}
function errorHandler(){
var msg = '';
switch(e.code){
case FileError.QUOTA_EXCEEDED_ERR:
msg = 'QUOTA_EXCEEDED_ERR';
break;
case FileError.NOT_FOUND_ERR:
msg = 'NOT_FOUND_ERR';
break;
case FileError.SECURITY_ERR:
msg = 'SECURITY_ERR';
break;
case FileError.INVALID_STATE_ERR:
msg = 'INVALID_STATE_ERR';
break;
default:
msg = 'Unknown Error';
break;
};
console.log('Error: ' + msg);
}
window.requestFileSystem(
window.TEMPORARY,
5*1024*1024 /*5MB*/,
onInitFs,
errorHandler
);
// create empty file called log.txt
// throws an error e is not defined
function onInitFs(fs){
fs.root.getFile(
'log.txt',
{
create: true,
exclusive: true
},
function(fileEntry){
console.log('fileEntry.isFile = ' + fileEntry.isFile);
console.log('fileEntry.name = ' + fileEntry.name);
console.log('fileEntry.fullPath ' + fileEntry.fullPath);
},
errorHandler
);
}
function errorHandler(){
var msg = '';
switch(e.code){
case FileError.QUOTA_EXCEEDED_ERR:
msg = 'QUOTA_EXCEEDED_ERR';
break;
case FileError.NOT_FOUND_ERR:
msg = 'NOT_FOUND_ERR';
break;
case FileError.SECURITY_ERR:
msg = 'SECURITY_ERR';
break;
case FileError.INVALID_STATE_ERR:
msg = 'INVALID_STATE_ERR';
break;
default:
msg = 'Unknown Error';
break;
};
console.log('Error: ' + msg);
}
window.requestFileSystem(
window.TEMPORARY,
5*1024*1024,
onInitFs,
errorHandler
);
// simple debugging
window.requestFileSystem(
window.TEMPORARY,
5*1024*1024,
function(fs){
console.dir(fs.root);
fs.root.getFile('log.txt');
},
function(error){
console.dir(error);
}
);