Similar to this question, but my setup is a little different.
I’ve written this function:
function ShowDialog(url, width, height, id, options) {
var self = arguments.callee;
if(!self.dialogs) self.dialogs = {};
var src = url + (url.indexOf('?') === -1 ? '?' : '&') + '_popup=1';
if(id) src += '&_field=' + encodeURIComponent(id);
if(url in self.dialogs === false) {
var defaults = {
width: width,
height: height,
modal: true,
resizable: false,
draggable: false,
closeText: 'Close'
};
self.dialogs[url] = $('<div/>')
.html('<iframe width="100%" height="100%" marginwidth="0" marginheight="0" frameborder="0" scrolling="auto" src="'+src+'">Your browser does not support iframes.</iframe>')
.dialog(options ? $.extend(defaults, options) : defaults);
} else {
if(self.dialogs[url].data('reload')) {
self.dialogs[url].data('reload', false).children('iframe').attr('src', src);
}
self.dialogs[url].dialog('open');
}
return false;
}
Which I use like this:
<button onclick="return ShowDialog('/companies/add', 400, 320, 'id_company');">New</button>
When you click the button, it opens a dialog with an iframe containing a form. When you submit the form, it redirects to another page which just contains a script which is supposed to close the dialog.
In the other question, he has the ID of the iframe so he can find it, and then call .dialog('close'). I’ve generated my iframe on the fly, so I don’t know how to access it.
However, I know that my iframe always sits inside a div which has the .dialog object… there should be a way to get the parent element of an iframe from within that iframe, no?
Anyone know how?
Can we perhaps find it doing something like
window.parent.jQuery(this).parent()
? I don’t know what this needs to be though. Some kind of reference to itself… window didn’t work.
window.frameElement gives you the IFrame element