3

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! :)

treyBake
  • 6,440
  • 6
  • 26
  • 57

3 Answers3

3
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");

Deckerz
  • 2,606
  • 14
  • 33
  • @ThisGuyHasTwoThumbs no problem :) feel free to mark it as the answer – Deckerz Aug 22 '17 at 09:08
  • 1
    The `window.opener.console.log` works for me with Karma and Chrome. Even the `child.console = console` and calling `console.log` works. I haven't tested it with normal webpages yet, maybe Karma replaces the console to be able to capture logs or maybe you can access the console directly and the answer is wrong. – inf3rno Aug 16 '18 at 09:23
2

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:

Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
1

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.

inf3rno
  • 24,976
  • 11
  • 115
  • 197