Reputation: 1391
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.
Upvotes: 5
Views: 14990
Reputation: 4701
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 -
// 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);
}
);
Upvotes: 2
Reputation: 2407
Yes, using the google chrome browser hit the f12 key, and click on the console button. Then use
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.
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
Upvotes: 5
Reputation: 8640
You could always send an AJAX call back to the server and track error messages there.
Upvotes: 2
Reputation: 993085
Most browsers support the window.console
object from the Console API:
console.log("Hello world");
Upvotes: 3