Shai UI
Shai UI

Reputation: 51978

How to show javascript console for an iframe?

I have an iframe on my page:

<iframe></iframe>

And there's a div on my page where I'd like to show the javascript console for it:

<div id='console'></div>

So in the iframe, if a script did: console.log("hello world");

it would show in my console div.

Is there a way to do this? Also, I don't want to open developer tools. I want the console to be shown as part of my page within the html.

Upvotes: 3

Views: 4894

Answers (1)

Suraj
Suraj

Reputation: 51

You can get the console value in your HTML entity.

<iframe id="xyz"></iframe>

var iframeWindow = document.getElementById("xyz").contentWindow;

iframeWindow.console.log = function(val) {
    var divId = document.getElementById("console");
    var span = document.createElement("span");
    span.appendChild(document.createTextNode(val));
    divId.appendChild(span);
};

this will get all the console.log of the iframe and append to the div

Upvotes: 1

Related Questions