I want to show a warning message in the Chrome console, like the highlighted item in this screenshot:
console.log(message)
displays a normal white message.
console.error(message)
creates an error message.
But using console.warning(message)
returns Uncaught TypeError: console.warning is not a function.
So is there any way to present a console warning for JavaScript?
It should be like this:
JavaScript
x
13
13
1
(function() {
2
var newbgcolor = document.getElementById('mycolor').value;
3
document.getElementById('output').style.backgroundColor = newbgcolor;
4
});
5
6
function update() {
7
var mycolorvalue = document.getElementById('mycolor').value;
8
if (mycolorvalue != "#000000") {
9
document.getElementById('output').style.backgroundColor = mycolorvalue;
10
} else {
11
console.warning("Text will be hard to read!"); // <-- error happens
12
}
13
}
JavaScript
1
3
1
#output {
2
background-color: #00ffff;
3
}
JavaScript
1
14
14
1
<!DOCTYPE html public "-//W3C//HTML 4.01 Transitional//EN">
2
<html>
3
4
<head>
5
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
6
</head>
7
8
<body>
9
<p>Background color:</p>
10
<input type="color" id="mycolor" value="#00ffff" oninput="update()" />
11
<p id="output">You will see the change here.</p>
12
</body>
13
14
</html>
But it doesn’t work. How do I do this?
Edit: I could use custom CSS!
JavaScript
1
4
1
console.log(
2
"%c Foo",
3
"display:block;width:100%;background-color: #332B00;color:#F2AB26;"
4
);
But it just doesn’t look right:
Advertisement
Answer
Try using console.warn
method instead of console.warning
.
Example
JavaScript
1
1
1
console.warn("A sample warning message!");
Reference
Here’s a resource containing a full list of available console methods: https://developer.mozilla.org/en-US/docs/Web/API/console#methods