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.
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.
Share Improve this question asked Feb 19, 2015 at 8:23 André FratelliAndré Fratelli 6,0687 gold badges49 silver badges93 bronze badges3 Answers
Reset to default 12a 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
You can turn on "Selected context only" on Chrome.
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 */ };
}