java - jQuery - 在对话框 iframe 中运行函数

标签 java jquery jsp iframe

我创建了一个使用 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/

相关文章:

java - Web 服务建议 Java

javascript - 使用 jQuery 从输入获取关联数组

javascript - 检查一个数组是否包含在另一个数组中

javascript - 无法获取 jQuery 插件 - timepicker - 在 jsp 页面中工作

jsp - 如何禁用对 JSP 页面的 GET 请求?

java - 为什么 1_2_3_4 在 java 中是有效的整数文字?

java - 为什么 java 在使用 -jar 开关执行 jar 文件时不遵循类路径?

java - 无法在项目 dbproxy 上执行目标 org.apache.maven.plugins :maven-compiler-plugin:3. 0:编译(默认编译):

javascript - 悬停时获取数据属性的特定部分

java - 身份验证重定向后如何返回 Jersey MVC Viewable?