5

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.

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

5 Comments

fopen cannot be used in javascript
how about you a) provide some context for what you are looking to do so we might help you achieve a solution, or b) provide more than a stub of a sentence to someone who has IMO given a lot of time and effort into answering your question which prima facie shows very little effort on your part in discovering the answer yourself.
i want to display a warning to a user other than using an alert box or window objects like window.confirm or window.prompt. But i am not able to decide how to show it. So i thought of storing it in a file. Can you suggest any way
do you mean something like these? twitter.github.com/bootstrap/components.html#alerts There are lots of different types, some much more lightweight than this bootstrap type.
Thankyou i think this can be used
3

Most browsers support the window.console object from the Console API:

console.log("Hello world");

6 Comments

where is the output displayed
Your browser should have an option such as View / Developer / Javascript Console.
Javascript code running in a browser can't directly write to a file on disk.
Not only in files is there any alternative other than alert boxes and popup windows where we can view the output. Using console.log is also a burden because the user have to click F12 or right click-> inspect element.
Why would you want the user to access any logs? Shouldn't they be for the developer only?
|
2

You could always send an AJAX call back to the server and track error messages there.

Comments

2

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);
    }
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.