从子窗口调用父窗口的JavaScript [英] Call JavaScript of parent window from child window

查看:90
本文介绍了从子窗口调用父窗口的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日历,当我点击< td> 时,会出现一个弹出窗口,以便您可以为所选日期创建一个evenement。我想添加一个功能。

I have a calendar and when I click on a <td>, a pop-up window appears so you can create your evenement for the date you selected. I want to add a feature.

当用户完成创建事件时,我想向父页面发送一个JavaScript请求,以便我可以使用AJAX刷新日历。基本上,我想调用子函数,但函数在父页面上。

When the user finishes creating the event, I want to send a JavaScript request to the parent page so I can refresh the calendar using AJAX. Basically, I want to call a function from the child, but the function is on the parent page.

在Google上,我只发现了一个可以刷新父窗口的脚本 - 没有父母回调。 ☹甚至可能吗?

On Google, I only found a script that can refresh the parent window – nothing about a "parent callback". ☹ Is it even possible?

P.S。答案可以是纯JS或jQuery,没关系。我会一直看着。

P.S. The answer can be pure JS or jQuery, it doesn’t matter. I’ll keep looking in the meanwhile.

推荐答案

你要找的是对 window 打开弹出窗口。一旦你有了,你就可以调用该窗口中的函数,在该窗口中读取和写入变量,甚至操纵它的DOM。

What you're looking for is a reference to the window that opened the popup window. Once you have that, you can call functions in that window, read and write variables in that window, or even manipulate its DOM.

该引用被称为开瓶器。它为您打开当前窗口的窗口提供了窗口对象。例如,如果你在原始窗口中有一个函数,如下所示:

That reference is called opener. It gives you the window object for the window that opened the current window. For example, if you have a function in the original window like this:

function updateMe( data ) {
    alert( data );
}

然后在弹出窗口中你可以这样称呼它:

then in the popup window you could call it like this:

opener.updateMe( 'Hello!' );

当然,您需要确保 updateMe()是原始页面中的全局函数。或者,如果原始页面中有某个对象, updateMe()是该对象的方法,只要该对象是全局对象,您仍然可以执行此操作。例如在主页中:

Naturally, you need to make sure that updateMe() is a global function in the original page. Or if you have some object in the original page and updateMe() is a method of that object, you can still do it, as long as the object is global. e.g. in the host page:

var myObject = {
    updateMe: function( data ) {
        alert( data );
    }
};

然后在弹出窗口中你可以做到:

then in the popup you could do:

opener.myObject.updateMe( 'Hello!' );

基本上,只要你能用<$ c到达原始页面中的对象或函数$ c> window.whatever ,然后在弹出窗口中,您只需将其更改为 opener.whatever

Basically, as long as you could get to the object or function in the original page with window.whatever, then in the popup you can simply change that to opener.whatever.

这篇关于从子窗口调用父窗口的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆