niutech / showModalDialog

window.showModalDialog polyfill using a <dialog> element
https://niutech.github.io/showModalDialog/demo.html
Apache License 2.0
177 stars 88 forks source link

Close popup with a button from the content. #15

Closed micjohnson382 closed 8 years ago

micjohnson382 commented 8 years ago

I am able to close the popup using the X on the top right corner. I am trying to close the popup a thru a "Close" button from the iframe dialog-body content. I tried using removeChild function but failed.

I have tried with "self.parent.document.getElementsByTagName('dialog')[0].close()" but got this error "Uncaught TypeError: Cannot read property 'close' of undefined"

How do I do that? Appreciate your help.

micjohnson382 commented 8 years ago

I found the soultion thru this link.

https://github.com/niutech/showModalDialog/issues/5

But not sure what to do If my calling and the popup pages are located in different folders. I am getting "Uncaught TypeError: Cannot read property 'close' of undefined" error.

niutech commented 8 years ago

The directory should not matter, as long as the modal and the parent document are in the same origin. Then you should be able to acces the parent document from inside the modal using:

var parentDoc = parent.document;
var dialogElms = parentDoc.getElementsByTagName('dialog');
var firstDialog = dialogElms[0];
firstDialog.close();

If you get a TypeError, it means that the dialogElms is empty.

micjohnson382 commented 8 years ago

Thank you very much for your reply. Yes I am getting a TypeError. I did check the length of dialogElms and it returned 0. I am using the exact code given here and Not sure why I am getting zero length.

niutech commented 8 years ago

Open Dev Tools and inspect the code provided. Check if parentDoc points to the parent document, and if it contains a <dialog> element. Also check if replacing parent with top will work.

micjohnson382 commented 8 years ago

Thank you very much. The code is working well after I added "parent." to reach my popup root level. var parentDoc = parent.parent.document; Appreciate your help.

Zolfaghari commented 8 years ago

to close from modal form: parent.document.getElementById('dialog-close').click();