简体   繁体   English

使用Java关闭窗口时创建事件

[英]Creating an event when closing a window using Javascript

I'm creating a popup that will allow me to select values from it, then after I've selected the values, I need to close the window, then pass it to my main form. 我正在创建一个弹出窗口,允许我从中选择值,然后在选择了值之后,需要关闭窗口,然后将其传递给我的主窗体。

The code in my main form: 我主要形式的代码:

onClick="cust=window.open('popup.php?count=<?=$RowStart?>&name_id=' + document.form1.name_id<?=$RowStart?>.value'custWindow','top=50, left=50, height=150, width=375, scrollbars=yes, resizable=yes');cust.focus(););

And the code inside the popup: 以及弹出窗口中的代码:

<form name="form1" action="<?=$_SERVER['PHP_SELF']?>" method="post">
<table width="100%" border="0" cellpadding="0" cellspacing="0"> 
<tr>
<td>
<input id="name" name="name" type="text"/>
<input id="nameid" name="nameid" type="hidden" value ="<?=$name_id?>"/>
</td>
<td>
<input name="ellipse" type="button" value="..." onClick="ellipse=window.open('engine_item_list2.php?name='+document.form1.name.value,'ellipse','top=50,left=50,width=500,height=500, scrollbars=yes, resizable=yes'); ellipse.focus();"/> 
</td>
</tr>
</table>
</form>

What happens is when I click the button in the main form, it goes to open a new window where the popup has a form. 发生的事情是,当我单击主窗体中的按钮时,它将打开一个新窗口,其中弹出窗口具有一个窗体。 Inside the form is an ellipse that gets the value for the name_id using the input from the name textfield. 表单内部是一个椭圆,它使用名称文本字段中的输入获取name_id的值。

I am able to get the values from the ellipse to the name_id, because the ellipse has a submit button inside it. 我能够从椭圆到name_id的值,因为椭圆里面有一个提交按钮。 But my popup window form1 should not have a save button involved, so I have to pass the values from the popup window back to the main form by either submitting it when I close the popup or using another way. 但是我的弹出窗口form1不应包含保存按钮,因此我必须通过在关闭弹出窗口时提交它或使用其他方法将弹出窗口中的值传递回主窗体。

I think I should use a window.close() but I dont know how to pass the data back using it. 我想我应该使用window.close(),但是我不知道如何使用它传回数据。 Is there a way I can link the close button in the window such that when it gets clicked the values gets to be passed back to the main form? 有没有一种方法可以链接窗口中的关闭按钮,以便在单击它时将值传递回主窗体?

When a "done" button or something like that is clicked in your popup window, you should be able to use window.opener from the popup window to find the window that opened it and then put your data into the DOM or javascript variables of that window. 在弹出窗口中单击“完成”按钮或类似的按钮时,您应该能够从弹出窗口中使用window.opener查找打开它的窗口,然后将数据放入该窗口的DOM或javascript变量中窗口。 After putting the data from the popup into the original window, you can then close the popup with window.close() . 将来自弹出窗口的数据放入原始窗口之后,可以使用window.close()关闭弹出窗口。

Note, it is more common these days to use a temporary overlay in the same window for your form rather than popping up a new window and it also avoids any conflicts with popup blockers that might get in the way of popping up your new window. 请注意,如今,在表单的同一窗口中使用临时覆盖而不是弹出新窗口更为常见,这还可以避免与弹出窗口阻止程序发生任何冲突,这些冲突可能会妨碍您弹出新窗口。

This will not handle the close corner in the window. 这将无法处理窗口中的关闭角。 That would generally just be equivalent to the "Cancel" operation so nothing needs to be done when it is clicked (per normal UI convention). 通常,这仅相当于“取消”操作,因此在单击时(根据常规UI约定)无需执行任何操作。 The typical UI would involve a "Done", "Finished" or "OK" button that would be clicked when the user wants the data entered to be accepted. 典型的UI将涉及“完成”,“完成”或“确定”按钮,当用户希望输入的数据被接受时,将单击该按钮。 It would be in the click handler for this button that you would use window.opener to find the parent/main window, put the data from the popup form into that window and then call window.close() to close the popup. 您将使用window.opener在此按钮的单击处理程序中查找父/主窗口,将来自弹出表单的数据放入该窗口,然后调用window.close()关闭弹出窗口。

Javascript Java脚本

window.onbeforeunload=function(){
   alert('This is the popup!');
}

OR 要么

function window.onbeforeunload(){
   alert('This is the popup!');
}

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

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