Can we create log files using javascript

Instead of showing a warning in the window, I want to direct my output from javascript to a log file. Is there any way to do this. If yes, please explain this with an example.

+5
source share
4 answers

Yes, using the Chrome Chrome browser, press f12 and click the console button. Then use

console.log(your code);

You can write objects, arrays, strings, variables. Much more useful than warnings.

Also in firefox, the firebug plugin is very useful. It has the same functionality and adds the check item function that Google Chrome has built in.

EDIT: , , . . - , - , -, , , . - http://davidwalsh.name/dw-content/top-bar-opacity.php

+5

window.console API :

console.log("Hello world");
+3

AJAX- .

+2

There is actually a way to do this, but it is only available in Google Chrome and mainly for HTML5 applications packaged as extensions. It is planned to make it available in wider distributions, but not quite there yet . It is called the FileSystem API . Here is an example I played 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);
    }
);
+2
source

All Articles