最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I console.log to parent window? - Stack Overflow

programmeradmin0浏览0评论

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.$.fnpareDates();
        window.opener.$.fn.addStatusIcon();
        window.opener.$.fn.iconTooltips(1);
        window.opener.$.fn.iconTooltips(2);
        window.opener.console.log('test');
    } else {
        $.fnpareDates();
        $.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! :)

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.pareDates();
        window.opener.$.fn.addStatusIcon();
        window.opener.$.fn.iconTooltips(1);
        window.opener.$.fn.iconTooltips(2);
        window.opener.console.log('test');
    } else {
        $.fn.pareDates();
        $.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! :)

Share Improve this question asked Aug 22, 2017 at 8:57 treyBaketreyBake 6,5687 gold badges28 silver badges63 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 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");

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:

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.

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 mented out. Strange.

发布评论

评论列表(0)

  1. 暂无评论