0

This seems like it would be a pretty wide-spread use for simplemodal but I cannot find how to do it anywhere.

I am pulling up a page via iframe using simplemodal, generating a variable, then I would like to pass it back to the parent page when the modal is closed.

How would I go about doing this?

Thanks for any help!

RandyLahey
  • 979
  • 4
  • 10
  • 26

2 Answers2

1

how do you mean pass it back? jquery? If you are not on the same domain, protocol, or otherwise have the document.domain set up correctly then you will run into errors with the following snippet, but you could try something like the following to access the parent page's DOM.

window.parent.$

So let's say you have this function on the parent page,

function UpdateTheHeaderPlease(data) {
$('.header').replaceWith(data);

}

then you have this code wrapped up in the simplemodal callback OnClose:

$("#element-id").modal({onClose: function (dialog) {
window.parent.UpdateTheHeaderPlease(some data);

});

Rob Allen
  • 2,871
  • 2
  • 30
  • 48
  • I mean after I close the modal with the iframe, how do i use that variable on the main page?. So wouldn't closing the modal make the window.parent.whatever not usable beacause the iframe doesn't exist anymore? Also it's all on the same domain, protocol etc, so there shouldn't be a problem there. – RandyLahey May 02 '12 at 20:30
  • so I was thinking that you update the parent in the OnClose callback via javascript. So technically before the iframe was closed, but perhaps that would work? – Rob Allen May 02 '12 at 20:50
0

Ok I figured it out, thanks Rob A for some good ideas.

I had to first give the iframe a name.

$.modal('<iframe src="modal_calls.cfm" name="frameName">', {

Then within the iFrame page i set the variable i wanted passed back to a window scoped variable:

window.frameVar = myVar;

Then before i closed the modal i set a window scoped variable using the name of the iFrame:

onClose: function (dialog) {
    window.newFrameVar= window.frameName.frameVar;
    $.modal.close();
}

Then I was able to access it on the parent document using window.newFrameVar.

RandyLahey
  • 979
  • 4
  • 10
  • 26
  • by the way, i think it is generally a good idea to avoid iframes. If you control both urls then you might want to look into using a div that is populated by ajax or something – Rob Allen May 02 '12 at 21:19