How to silence console output on an iframe
Asked Answered
D

3

14

My page is using iframes to display some content, but right now I'm working on the main page and the output from the iframes is cluttering my console and making it hard to debug. Is there any way to silence the console?

I tried setting the console to a no-op:

var CONSOLE_LOG = window.console.log;
window.console.log = function() { /* nop */ };

function LOG(msg)
{
    window.console.log = CONSOLE_LOG;
    console.log(msg);
    window.console.log = function() { /* nop */ };
}

I expected this to work, but the iframes still generate output.

Danged answered 19/2, 2015 at 8:23 Comment(0)
C
12

a working fiddle here

var iframe = document.createElement('iframe');
iframe.setAttribute("src", "yourIframeURL");
document.body.appendChild(iframe);
iframeWindow = iframe.contentWindow;
iframeWindow.console.log = function() { /* nop */ };

This works on Mozilla,Chrome ,Safari

Celka answered 19/2, 2015 at 8:28 Comment(11)
Interesting. I would like to see a live demo of thisSafar
When should I do this? I first create the iframe with createElement, add the to DOM, set the on load method, and then the src attribute. If I do has you say immediately after creation (before DOM insertion), I get an exception that parentWindow of null is not defined. If I do it later, the iframe still generates outputTitania
iframe.onload = function () { //here you should do this ,when the iframe is complete loaded }Celka
Like I said, it still generates output.Titania
see this fiddle jsfiddle.net/gskf2dxx , to test it comment and uncomment iframeWindow.console.log = function() { /* nop */ };Celka
Are you not getting output with that sample? I still do... Both on Safari and ChromeTitania
in firefox works fine ,also i have got it to work on Chrome see this fiddle jsfiddle.net/gskf2dxx/12 , but doesn't work on IE and OperaCelka
Also works on Safari. Could you update your answer so I would accept it?Titania
This isn't working for me. I tested on 7/30/2019 in Firefox and Chrome, and both browsers are still showing messages from the iframe in the console. I didn't add my iframe dynamically to the page -- it was already in the HTML -- so I just used this code: document.getElementById("iframeIdHere").contentWindow.console.log = function() { /* do nothing */ }; Is there something I'm missing?Almeida
I'm trying to use the above snippet to remove Substack's annoying hiring call from my blog, but it also doesn't seem to be working. Here's their script: cdn.substack.com/min/main.bundle.jsTonneson
You should probably add that this will not work on any third party iframe, you will get the error: SecurityError: Permission denied to access property "console" on cross-origin objectBenefield
P
1

I have come accross a use case, where the iframes were created by a 3rd party library, so I had to get them from document instead of creating them:

const iframes = Array.from(document.getElementsByTagName('iframe'));
for (const item of iframes) {
  item.contentWindow.console.log = () => { /* nop */ };
}
Pintsize answered 12/10, 2020 at 12:11 Comment(2)
Code only answers are discouraged on SO. Pleaser as some explanation for quality & long term value. Answers that add insight to help future visitors solve their own similar issues are more useful, and more likely to be upvotes.Undertenant
@Undertenant You are so strict :), sometimes code is self-documented.Pintsize
N
1

You can turn on "Selected context only" on Chrome. enter image description here

Nonstandard answered 3/6 at 3:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.