Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect a console.warn message

Tags:

javascript

I have a need to find out when a warning (not error) message is sent to the browser's console.log . From research, I know that I can detect error messages with JavaScript code similar to this:

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);
}

But that only shows error messages. It will not show any warning messages.

Is there a similar way to detect a specific warning message in the console.log? Or, alternately, is there some JS code that will allow me to search the console.log for a specific string?

Note that I can see the warning message in the browser's debugging (console logging) window. But I want to 'intercept' the warning message and perform some action if that warning is found.

Added

The intent is to find out when a specific warning message occurs; the message being similar to "Loading failed for the <script> with source "https://www.example.com/js/somescript.js" .

But the question of how to intercept a warning message (not error; the code I included does that) that is output by the browser (not my code).

Added

So, here's what I need. Assume this code

console.warn("Here is a warning!");

What code would you write to see if that message was written to the console as a warning?

like image 348
Rick Hellewell Avatar asked Dec 19 '18 03:12

Rick Hellewell


People also ask

How do you show warnings in console?

warn():- This method is used to log a warning message to the console.

Is warn a console method?

warn() The console. warn() method outputs a warning message to the Web console. Note: In Chrome and Firefox, warnings have a small exclamation point icon next to them in the Web console log.

How do you catch warnings in JavaScript?

filter(e => typeof e == 'string'); for(m in messages){ if(messages[m]. indexOf('The warning I am looking for...') != -1){ //treat the warning as you want //you could use switch case if you want }; }; return console.

When should I use console warn?

The HTML DOM console. warn() method is used to display a warning message in the console. In some browsers there is a small exclamation mark in console log for these warnings The console. warn() method will not interrupt your code execution.


1 Answers

The warnings you are talking about are generated by the browser, not console.warn. This is why you cannot override it. Most likely, you need to manually add listeners for each event you need. For example, if you want to handle a script loading error use the onerror event:

<script src="https://www.example.com/js/somescript1.js" onerror="console.log('Cannot load script from ' + this.src)"></script>
<script src="https://www.example.com/js/somescript2.js" onerror="console.log('Cannot load script from ' + this.src)"></script>
like image 127
Victor Avatar answered Oct 01 '22 02:10

Victor