如何使iframe上的控制台输出静音

And*_*lli 5 javascript iframe

我的页面使用iframe来显示一些内容,但是现在我正在使用主页面,而iframe的输出正在混乱我的控制台并使其难以调试.有没有办法让控制台静音?

我尝试将控制台设置为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 */ };
}
Run Code Online (Sandbox Code Playgroud)

我希望这可以工作,但iframe仍然会产生输出.

Vla*_*nut 8

工作小提琴这里

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

这适用于Mozilla,Chrome,Safari

  • 您可能应该补充一点,这不适用于任何第三方 iframe,您将收到错误:“安全错误:访问跨源对象上的属性“控制台”的权限被拒绝” (8认同)