Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 11, 2023 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/08c0fbf32bfeb4b88d22c2171b2ad55b to your computer and use it in GitHub Desktop.
Save code-boxx/08c0fbf32bfeb4b88d22c2171b2ad55b to your computer and use it in GitHub Desktop.
Javascript Display Error Messages

JAVASCRIPT DISPLAY ERROR MESSAGE

https://code-boxx.com/display-error-messages-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>JS Show Error Message</title>
<script>
function condemo () {
// (A) CONSOLE LOG - OUTPUT TEXT OR VARIABLE IN CONSOLE
var foo = "bar";
console.log(foo);
// (B) CONSOLE TABLE - OUTPUT ARRAY OR OBJECT IN TABLE FORM
var colors = ["red", "green", "blue"];
console.table(colors);
// (C) CONSOLE ERROR - OUTPUT AN ERROR MESSAGE
try {
doesNotExist();
} catch (err) {
console.error(err);
}
}
</script>
</head>
<body>
<button onclick="condemo()">
Open developer's console and click on this
</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JS Show Error Message</title>
<script>
function doalert () {
try {
doesNotExist();
} catch (err) {
alert(err);
}
}
</script>
</head>
<body>
<input type="button" value="Error Alert" onclick="doalert();">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JS Show Error Message</title>
<!-- (A) THE CSS -->
<style>
#err {
display: none;
background: #f54242;
border: 1px solid #bd1717;
padding: 5px;
margin: 5px;
color: #fff;
font-weight: bold;
}
</style>
</head>
<body>
<!-- (B) THE HTML -->
<div id="err"></div>
<input type="button" value="Test Error" onclick="dohtml()">
<!-- (C) JAVASCRIPT -->
<script>
function dohtml () {
try {
doesNotExist();
} catch (err) {
let notify = document.getElementById("err");
notify.innerHTML = err;
notify.style.display = "block";
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment