LucyTurtle
LucyTurtle

Reputation: 1133

Turn Off Ad Output / Errors In Console

We are displaying Google Adwords ads on our site. Something that irriates me to no end is that the ads will run with errors or normal console.log() output and that text appears in our console. I am wondering if there is anyway to turn these errors off through Adword's scripting, or through Javascript.

Ads appear in an iframe.

Upvotes: 2

Views: 126

Answers (1)

Brandon Dixon
Brandon Dixon

Reputation: 1134

You can re-define the console.log output as such:

var oldConsole = console;
console = {
    log: function(str,goOut = false) {
    	if (goOut) {
           oldConsole.log(str);
        }
    },
    warn: function(str,goOut = false) {
    	if (goOut) {
            oldConsole.warn(str);
        }
    },
    error: function(str,goOut = false) {
    	if (goOut) {
            oldConsole.error(str);
        }
    }
}

console.log("this will not appear");
console.log("This will appear",true);

Saving the console object to oldConsole allows you to still actually output to the console, then re-defining the console object allows you to change the function.

With this function, to actually output something, you need to put TRUE as the second parameter, which is not done by default, to all of your console.log output so that it actually appears.

As a note, here's exactly what's happening:

  1. Save the reference to console to the variable oldConsole
  2. Re-define console as a new object.
  3. Set it's properties of log, warn, and error to our own functions.
  4. The second parameter, goOut, is set to FALSE by default, which means a call to console.log("hello") results in goOut to being false (not undefined).
  5. When you explicitly set it to true, call upon oldConsole.log(str,true) to actually output to log.

Upvotes: 2

Related Questions