简体   繁体   English

将值从子窗口传递给父窗口

[英]Passing a value from child window to parent

Here is my scenario: i have a parent web page, in my javascript code I open a new window. 这是我的情况:我有一个父网页,在我的JavaScript代码中,我打开了一个新窗口。 This new window is a server-side aspx page. 这个新窗口是服务器端的aspx页面。 This child page needs to save some data in the database, and after saving it returns an ID. 该子页面需要在数据库中保存一些数据,保存后返回一个ID。 Now, I need to pass this ID from the child server page to the parent's javascript. 现在,我需要将此ID从子级服务器页面传递到父级的javascript。

Essentially I need the child server code to trigger: 1) return the value to the javascript code of the parent page 2) close itself 本质上,我需要触发子服务器代码:1)将值返回到父页面的javascript代码2)关闭自身

What would be the acceptable way to do it? 可接受的方式是什么?

Set a variable in the parent page: 在父页面中设置一个变量:

var dbID;

When the DB call returns from the opened window, place this in the opened window's load event (or ready event if using jquery or another framework): 当数据库调用从打开的窗口返回时,请将其放置在打开的窗口的load事件(如果使用jquery或其他框架,则将其放置在ready事件中):

window.opener.dbID = <%= newID %> 
// or whatever your framework's technique is for this :-)
window.close();

You can also call a global function on the parent as well using the same technique. 您也可以使用相同的技术在父对象上调用全局函数。

On "parent" page: 在“父级”页面上:

function handleNewDBID(dbID) { alert("lookit me, i got an ID! " + dbID); }

And on the opened window: 在打开的窗口上:

window.opener.handleNewDBID(<%=newID%>);

如果您在子窗口中并且假设您的父窗口具有对象childResult

window.opener.childResult = "yourIdHere";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM