Search code examples
javascriptjquery

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


Solution

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