4

I have several iframes with 3rd party content in my page, they are throwing all kinds of errors and whatnot. They work well enough and its not my content so not my problem. Can I keep them from logging stuff into console so I can focus on my code? Either firefox or chrome works for me if such feature should be browser specific.

user81993
  • 6,167
  • 6
  • 32
  • 64

1 Answers1

1

Bit of a hack, but I've found console.log = function() {} to work in the past. Basically redefines console.log as an empty function so that it doesn't do anything when called.

The accepted answer for this Question also explains a way to do this in a way you can toggle it off and on.

Community
  • 1
  • 1
millerbr
  • 2,951
  • 1
  • 14
  • 25
  • 1
    Yes but does that work for iframes where you don't have access to the source? And what if the website makes use of console.debug/console.error? – Ivar Mar 15 '16 at 15:31
  • I'm not an expert on iframes but I believe they call the same console as your script, so redefining it will work. You can do the same for `debug` and `error` to handle them if needed. – millerbr Mar 15 '16 at 15:33
  • Nice, but what if he/she still wants to do their own console logging? This will make that impossible, and [cloning the function](http://stackoverflow.com/questions/1833588/javascript-clone-a-function) seems to result in an illegal invocation error. – toomanyredirects Mar 15 '16 at 15:35
  • >but what if he/she still wants to do their own console logging? yea that, i might just as well close the console. I still need the logs from my code – user81993 Mar 15 '16 at 15:37
  • Updated answer with a link to show you how to do that – millerbr Mar 15 '16 at 15:37
  • 6
    "I'm not an expert on iframes but I believe they call the same console as your script" — They don't. They have their own `window` object with their own `console` variable. – Quentin Mar 15 '16 at 15:39
  • Ah I'm wrong on that count then. You would need to specifically override the console in the iframe window then! – millerbr Mar 15 '16 at 15:42
  • What if it's a cross-origin iframe? I get the error "Blocked a frame with origin "<>" from accessing a cross-origin frame." – Mattias Martens Jul 09 '20 at 18:38