Question


Q: How to close a mootools dialog from inside an iFrame?

I have the following code to open the dialog box on the parent:

window.addEvent("domready", function(e){
/* Modal */
$("mootools_dialog").addEvent("click", function(e){
    e.stop();
    var dlgx = document.getElementById("mootools_dialog").offsetLeft-window.getScroll().x-20;
    var dlgy = document.getElementById("mootools_dialog").offsetTop+window.getScroll().y-400;
    dlgx = (dlgx < 20) ? 20 : dlgx;
    dlgy = (dlgy < 20) ? 20 : dlgy;
    var SM = new SimpleModal({"hideHeader":true,"closeButton":false,"hideFooter":true,"offsetLeft":dlgx, "offsetTop":dlgy });
    var form_check = null;

    SM.show({
      "model":"modal",
      "title":"Title, or empty?",
      "contents":"<iframe src='booking.iframe.php' id='booking_iframe' scrolling='no' /></iframe>"
    });
  })
});

It works to call functions from inside the iFrame, like href='javascript:{parent.book_click();}' But I cannot find a way to close the mootools window.

What do I need inside the iFrame?
I tryed to call a function in the parent to close the window but could not make any code work.
<a href="javascript:{parent.close_dialog();}">Close</a>or
parent.SimpleModal.close() - but no sucess.
I have mootools 1.3.2

Was it helpful?

Solution

It seems that you're using SimpleModal class for custom dialogs. The first thing you should notice is that it doesn't have close method. However it has hide method which does the job.

But hide method cannot be called on global SimpleModal class, it should be called on instance (that's your SM variable). So you can modify your code like following:

window.SM = new SimpleModal({"hideHeader":true,"closeButton":false,"hideFooter":true,"offsetLeft":dlgx, "offsetTop":dlgy });
var form_check = null;

SM.show({
  "model":"modal",
  "title":"Title, or empty?",
  "contents":"<iframe src='booking.iframe.php' id='booking_iframe' scrolling='no' /></iframe>"
});

Now you have global variable SM with current dialog instance, and now you can call its methods from <iframe> without any problems like this:

<a onclick="parent.SM.hide()">Close</a>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top