2

i'm using try, catch, for debugging, but warnings is not create exceptions. How to get all javascript warnings and errors to output div?

UPDATED: If browser supports Afaik logging, how to get that log to string or output div?

UPDATED: I found the way how to do that: i can reload console.log function to my custom function an call native console.log function.

6
  • 5
    Why not just use the browser's debug console? Commented Jan 23, 2013 at 13:52
  • Could you show us your code? Commented Jan 23, 2013 at 13:52
  • Afaik debugger warnings are not accessible from javascript Commented Jan 23, 2013 at 13:53
  • 1
    @Pekka In example the Internet Explorer didn't get a console ;-) Commented Jan 23, 2013 at 13:55
  • @Bergi how to get Afaik log to string? Commented Jan 23, 2013 at 13:57

3 Answers 3

0

First of all, get rid of the try catch. Don't use try catch when you are debugging.

Second, you don't want to out errors to a div, use firebug or inspector for that - console.log();

Third, if you really want to do it: you could use try catch and in the catch, use something like

$('body').append($('div').html('variable for error message goes here'));

if you are using jquery

OR

document.getElementByTagName("body").appendChild( document.createTextNode("variable for error message goes here") );

if you have plain javascript

EDIT: try looking up ie debug bar , ie webDeveloper

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

6 Comments

Yes, i know that. I asked about warning, which created through console.warn :-)
I see, this is the first time I hear about console.warn()
are you using javascript console? I need repeat all errors, warnings, notes out of it
i need display that on the webpage. For IE i will be use just try catch.
IE has a debugger console for javascript! Press F12.
|
0

I understand myself why someone may want something to actually happen when an error occours in the document. The answers above just say that you would use developer tools, but I needed things to actually happen, and after some searching, here's what I found...

If you wish to catch all errors that come through, you can put the following code into your file, best at the top:

window.onerror = function(errorMsg, url, lineNumber){
    // any action you want goes here
    // errorMsg is the error message itself.
    // url should be the file presenting the error, though i have
    //    found that it only presents to me the address of the site.
    // lineNumber is the line number the error occoured on.
    // here is an example of what you could do with it:
    alert("Error in " + url + " at " + lineNumber + ":\n" + errorMsg);
}

I, myself, like to output the errors to a div that contains them all, though you can do literally anything to this information that you could do with any other string passed to a function.

Here is an example of what may happen if you throw an error with a button using the code above:

Error in your.site.here at 1:

Uncaught ReferenceError: foo is not defined

Comments

-1

For IE javascript debugging you can follow this guide:

http://msdn.microsoft.com/en-au/library/ie/gg699336(v=vs.85).aspx

Keep in mind that the developer tools window must be open prior to loading the page for the warnings and errors to appear in the console.

For webkit, (chrome, safari) developer console - here is a guide:

https://developers.google.com/chrome-developer-tools/docs/console

...Firefox also has a console

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.