Catch errors generated in javaScript and display in a div

1

I have div called log and I would like to capture everything that is generated in the console, just below I generate an index error to undefined a += 8; . The problem is that it only captures the first error.

  • How do I show all errors generated when opening the page?

  • And how do I show any error that might be generated during code execution?

var log = document.getElementById('log');

window.onerror = function(message, url, lineNumber) {  
  log.innerHTML += lineNumber + ":<br/>" + message + "<br/>In: " + url;
};

a += 8; //Index indefinido;
b += 5; //Index indefinido;
//funcaoindefinida(); //Função indefinida;
//var var = 0; //Palavra reservada;
<div id="log"></div>

I tried to work with try...catch(e) but also only get the first error generated in the code

var log = document.getElementById('log');
try{
  a += 8; //Index indefinido;
  b += 5; //Index indefinido;
  //funcaoindefinida(); //Função indefinida;
  //var var = 0; //Palavra reservada;
}
catch(e){
  log.innerHTML += e.message;
}
<div id="log"></div>
    
asked by anonymous 05.08.2015 / 19:08

0 answers