1

I created a JSP page that opens a dialog using jQuery.

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");
          }
    }); 

So far, so good. Notice I am only using an alert on the beforeClose function for now. The iframe itself is opening a JSP page named chat.jsp which has a multitude of jQuery functions in it also as it is the front end of a chat program.

Question:

How do I send a message to the chat.jsp page so that it knows the dialog box is being closed?"

This is important as chat.jsp needs to communicate back to the server that the user has logged out of chat. That process will likely take about a second, so I'll need to delay the closing of the dialog for at least that long.

What is my best option for communicating the closing event to chat.jsp and affording it enough time to execute a proper close?

Sandeep Chatterjee
  • 3,220
  • 9
  • 31
  • 47
Alan
  • 822
  • 1
  • 16
  • 39

1 Answers1

1

Provided the parent page (that opens the dialog) and the iframe page are served by the same domain, you could do the following:

Remove the close button from the dialog (see this answer).

Place a close button in the iframe page. This button would log the user out and then close the dialog by calling:

window.top.$('#dialog').dialog('close');

If want to decouple the parent page and the iframe page (so the iframe page does not need to know the id of the dialog div), you could have the close button fire a custom event on the parent document:

window.top.$(document).trigger('dialog-close');

And register an event handler in the parent page:

$(document).on('dialog-close', function() {
    $('#dialog').dialog('close');
});
Community
  • 1
  • 1
John S
  • 21,212
  • 8
  • 46
  • 56
  • I really liked your second option and tried it. Removing the close button on the dialog box worked great, but sending a "dialog-close" signal to the parent page didn't work. As I look at the code it looks like it should work and seems logical. Maybe we're missing something? – Alan Mar 22 '14 at 22:20
  • Well, it looks like I was mistaken. It does work after all. For whatever reason the iframe wasn't loading the page properly after I had made a change. After some fiddling about I got it working. Thanks for your assistance. Top marks for you. – Alan Mar 22 '14 at 23:45