treyBake
treyBake

Reputation: 6558

How can I console.log to parent window?

I've got a function that essentially refreshes a table, which works ok, but some of the JS functions don't run. To debug I'm trying to pass data between a popup and it's parent window. Currently I have this function:

$.fn.runFncs = function(isParent)
{
    if (isParent == 1) {
        window.opener.$.fn.compareDates();
        window.opener.$.fn.addStatusIcon();
        window.opener.$.fn.iconTooltips(1);
        window.opener.$.fn.iconTooltips(2);
        window.opener.console.log('test');
    } else {
        $.fn.compareDates();
        $.fn.addStatusIcon();
        $.fn.iconTooltips(1);
        $.fn.iconTooltips(2);
    }
};

and this gets run on an ajax success.

When I hit the button for the ajax, I get my success message etc. but no console.log in my parent window. I've been able to access the parent window before using window.opener and it seems to run ok, just not this time for some reason.

I tried research but either my query was too specific or it was simple "what is console.log" questions so a little stuck here.

Is there an alternative way I can console.log to the parent window? Maybe a document function I'm unaware of?

Thanks! :)

Upvotes: 4

Views: 4897

Answers (4)

Pete Prokopowicz
Pete Prokopowicz

Reputation: 1

In the code that opened the child window, you can reset the console of the child window to be the parent's console. Then, any code running in the child window that writes to its console will be writing to the parent console, without modification to the child-window's code.

let newWindow = window.open(some-url);
newWindow.console = this.console;

But, there is a curious side-effect (as of Chrome 127.0.6533.120 Aug 2024). In the child window, I get a NotAllowedError when code running there tries to access the Navigator's share feature. This doesn't happen if the second line, setting the new window's console, is commented out. Strange.

Upvotes: 0

inf3rno
inf3rno

Reputation: 26137

I tried out in recent (2018 aug) Firefox, Chrome and Opera and IE11 too and window.opener.console.log works perfectly in all of them. So I think the problem were somewhere else in your code.

You could even do child.console = console in the parent window and keep console.log, but most of the browsers clear that variable between page loads, and there is no way to set it again right after the document was created, but before the scripts are executed. You can add window.console = window.opener.console to the head of the child page if you can edit that. That should do the trick too.

Upvotes: 1

Deckerz
Deckerz

Reputation: 2604

function log(message){
    console.log(message);
}

Put that function in your parent window and call it like so. You basically need to provide a wrapper function that you can access

window.opener.log("Hi");

Upvotes: 3

Pablo Lozano
Pablo Lozano

Reputation: 10342

You cannot access directly from one window/tab to anothers's console object, but you can send messages from one window to another. The parent window would get that messages and then it would write it on the console. See this Q&A for more details:

Upvotes: 2

Related Questions