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.
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.
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.
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.
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);
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');
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); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With