View Full Version : How to trap Close button click of modal window from the calling page

08-06-2011, 03:51 PM
1) Script Title: :: DHTML Modal window v1.1

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex8/dhtmlwindow/dhtmlmodal.htm

3) Describe problem: I want to trap "Close" button(at the top right corner) click from my calling page. How can i do that?
Present script only allow to trap close(parent.modalwindow.hode()) event inside modal window by using:-
agreewin.onclose=function(){ //Define custom code to run when window is closed
.But due to some project requirement i need to find out when user click top close button to close the modal window.
Please help.

08-06-2011, 04:40 PM
The calling page is the page that launched the window, not the page in the window. Do you really mean the calling page? If so, that's what:

agreewin.onclose=function(){ //Define custom code to run when window is closed

does. If you mean the page in the window:

parent.agreewin.onclose=function(){ //Define custom code to run when window is closed

I see an example is given:

var iframedoc=this.contentDoc
alert(iframedoc.body.innerHTML) //alert entire body content of iframe
alert(iframedoc.getElementById("myname").value) //alert value of form field with id="myname"

That runs on the calling page, but accesses information inside the window. If you want to access information on the calling page use ordinary javascript, without the contentDoc object.

08-07-2011, 04:10 AM
Thanks John for your prompt reply.
Yes you are correct that "Calling page" means the page from where i have called modal window page. Like from A page, i want to call B page inside modal window.So here "A" is my calling page.
Now, problem is if i click top right Close icon(which is not part of any page,comimg with modal window), i am not getting any event in my calling page,here in "A" page.

agreewin.onclose=function(){ //Define custom code to run when window is closed
The above code is only working if i forcefully or you can say using code,close the modal window,but the above code is not working if i close modal window by clicking "top Close" icon (check red circle of attached image)4009.
So how to trap close event?

08-07-2011, 07:24 AM
Yeah. I had recently been looking at the parent script (the Window Widget) for someone else and assumed it would work the same way, at least as far as the close button is concerned.

But it doesn't. I didn't write either of these, so I can't say for sure why - what the logic was in that. I also cannot be 100% sure that my fix won't mess something else up. But it seems fine in limited testing.

Here's what to do. Using a text only editor like NotePad, edit the modal.js file. Find the close function (around line #54) and comment out (red slashes) the two highlighted lines as shown:

close:function(t, forceclose){ //DHTML modal close function
t.contentDoc=(t.contentarea.datatype=="iframe")? window.frames["_iframe-"+t.id].document : t.contentarea //return reference to modal window DIV (or document object in the case of iframe
//if (typeof forceclose!="undefined")
//t.onclose=function(){return true}
if (dhtmlwindow.close(t)) //if close() returns true

Added Later:
I see now that the email box cannot be closed without a "valid" email, not even with the close button. But if the onclose function were written differently and the form returned a different onclick/onsubmit .hide() event than the box itself, it would be fine with the code change as outlined above.

08-07-2011, 02:41 PM
Thanks John for your help. Now i can manage rest of the my requirements.:)