我创建了一个使用 jQuery 打开对话框的 JSP 页面。
JSP(div):
<div id="dialog" title="Welcome To Chat" class="ui-helper-hidden tabdialog">
<iframe id="inlineframe" name="inlineframe" src="" frameborder="0" scrolling="auto" width="600" height="600" marginwidth="5" marginheight="5" ></iframe>
</div>
jQuery:
$("#inlineframe").attr("src","chat.jsp?roomid=" + id[1]);
$( "#dialog" ).dialog({
width:626,
height:626,
beforeClose:function(){
alert("closing");
}
});
到目前为止,一切都很好。请注意,我目前仅在 beforeClose
函数上使用警报。 iframe 本身打开一个名为 chat.jsp
的 JSP 页面,该页面包含大量 jQuery 函数,因为它是聊天程序的前端。
问题:
How do I send a message to the
chat.jsp
page so that it knows the dialog box is being closed?"
这很重要,因为 chat.jsp
需要向服务器传达用户已退出聊天的信息。该过程可能需要大约一秒钟,因此我需要将对话框的关闭延迟至少那么长时间。
将关闭事件传达给 chat.jsp
并为其提供足够的时间来执行正确的关闭的最佳选择是什么?
最佳答案
如果父页面(打开对话框)和 iframe 页面由同一域提供服务,您可以执行以下操作:
从对话框中删除关闭按钮(请参阅 this answer )。
在 iframe 页面中放置一个关闭按钮。此按钮将使用户注销,然后通过调用关闭对话框:
window.top.$('#dialog').dialog('close');
<小时/>
如果想要将父页面和 iframe 页面解耦(因此 iframe 页面不需要知道对话框 div 的 id),您可以让关闭按钮在父文档上触发自定义事件:
window.top.$(document).trigger('dialog-close');
并在父页面中注册一个事件处理程序:
$(document).on('dialog-close', function() {
$('#dialog').dialog('close');
});
关于java - jQuery - 在对话框 iframe 中运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22582794/