Let's say I have the following code:
<script>
function billy() {
alert('muahahahaha!');
}
function suzzy() {
return;
}
</script>
and a button like this (with an undefined onclick handler):
<input type='button' value='click me' onClick='FRANK()' />
When I click the button, the following appears in the developer console:
Function 'FRANK()' is not defined.
How could I store that message in a variable and display it on the page?
document.getElementById('prompt').innerHTML = log;
So it would appear as:
<div id='prompt'>
Function 'FRANK()' has not been defined.
</div>
Let's say I have the following code:
<script>
function billy() {
alert('muahahahaha!');
}
function suzzy() {
return;
}
</script>
and a button like this (with an undefined onclick handler):
<input type='button' value='click me' onClick='FRANK()' />
When I click the button, the following appears in the developer console:
Function 'FRANK()' is not defined.
How could I store that message in a variable and display it on the page?
document.getElementById('prompt').innerHTML = log;
So it would appear as:
<div id='prompt'>
Function 'FRANK()' has not been defined.
</div>
Share
Improve this question
edited May 27, 2014 at 5:48
jahroy
22.7k10 gold badges61 silver badges110 bronze badges
asked May 27, 2014 at 5:36
XRipperxMetalXXRipperxMetalX
1353 silver badges9 bronze badges
9
- 1 I have no idea of what you are talking, you have a function called frank which you haven't defined, your suzzy function returns nothing, so what you are trying to do? – Mr. Alien Commented May 27, 2014 at 5:38
- I just want to know why it is you want to do this? – powerc9000 Commented May 27, 2014 at 5:38
- I read the question for twice, I cant get it either. – jhyap Commented May 27, 2014 at 5:38
- 5 he wants to print error in dom in place of console – Govind Singh Commented May 27, 2014 at 5:39
- Yes thank you Govind Singh Nagarkoti – XRipperxMetalX Commented May 27, 2014 at 5:42
2 Answers
Reset to default 17If you want to display any error of the page in your div, you may use the global event handler onerror :
window.onerror = function(e){
document.getElementById('prompt').innerHTML = e.toString();
}
Demonstration
If your goal is to intercept all what is written by the browser in the console, not only the errors, I'm not sure it's directly possible as the browser doesn't use the accessible console
functions for everything.
But you can do a lot by hooking all global event handlers :
Demonstration
I think @dystroy's answer is sufficient here, but if you want proper error handling, you should be using try
and catch
statements instead..
Demo
function throw_msg() {
try {
var a = '';
alert(b);
}
catch(throw_error) {
document.getElementById('error-box').innerHTML=throw_error.message;
setTimeout(
function() {
document.getElementById('error-box').innerHTML='';
}, 2000);
}
}
Explanation for the above code :
We are first creating a function which will be called on click of the button, and than when you click the button, the code in the try
block gets executed, if it has any error, we then throw the error using the catch
statement where the error message is printed using throw_error.message
and at the end, we use setTimeout
to clear out the error message in 2000
i.e 2 seconds