Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture console log of an iframe

Is there a way to capture the console outside of an iframe?

I'm working on an online IDE similar to jsFiddle and I wanted to give the users to option to at least read the results of the javascript console.

like image 241
Adonis K. Kakoulidis Avatar asked Feb 17 '13 18:02

Adonis K. Kakoulidis


People also ask

Can we get data from iframe?

But the iframe MUST be from the same domain as the parent document. If not from the same domain, a security violation occurs (You can't add content from foreign sites to your page and change that content.) If no security violation, you can do anything with the selection.

Can you Console log from HTML?

The console. log() method in HTML is used for writing a message in the console. It indicates an important message during testing of any program. The message is sent as a parameter to the console.

How do you visualize a Console log?

Look for Developer Tools or Simply Tools menu in all major browsers. If you are using Google Chrome the press Cntrl+shift+j to see console. In Firefox, press Ctrl+Shift+I and click on Console to view the console on Firefox.

How do I print a log from the Console?

log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Syntax: console. log(A);


2 Answers

If you want to print the log messages inside the window container's body, it is possible to declare the panel there:

var console = {     panel: $(parent.document.body).append('<div>'),     log: function(m){         this.panel.prepend('<div>'+m+'</div>');     }        }; console.log('message'); 
like image 151
A. Rodas Avatar answered Oct 21 '22 04:10

A. Rodas


Here is another solution if you don't want to append to HTML

var console = {   __on : {},   addEventListener : function (name, callback) {     this.__on[name] = (this.__on[name] || []).concat(callback);     return this;   },   dispatchEvent : function (name, value) {     this.__on[name] = (this.__on[name] || []);     for (var i = 0, n = this.__on[name].length; i < n; i++) {       this.__on[name][i].call(this, value);     }     return this;   },   log: function () {     var a = [];     // For V8 optimization     for (var i = 0, n = arguments.length; i < n; i++) {       a.push(arguments[i]);     }     this.dispatchEvent("log", a);   } }; 

Outside the iframe

iframe.contentWindow.console.addEventListener("log", function (value) {   console.log.apply(null, value); }); 
like image 42
SeanJM Avatar answered Oct 21 '22 02:10

SeanJM