开发者

How can I know when a certain iframe gets removed from a page

Let's say I have an iframe on a HTML page:

<iframe src="/script.php"></frame>

The iframe is inside a modal box window (I'm using a jQuery plugin for modal window: http://opensource.steffenhollstein.de/templates/modalbox/).

When the modal box gets closed, the iframe inside it is removed from the page's HTML with jQuery remove() method.

How can I notice that the iframe has been removed and execute some javascript code? Basically what I want is to refresh the page once the modal box is closed. This is the close method for the modal box plugin:

jQuery.fn.modalBox.close = function(settings){

    // merge the plugin defaults with custom options
    settings = jQuery.extend({}, jQuery.fn.modalBox.defaults, settings);

  开发者_StackOverflow社区  if( settings.setFaderLayer && settings.setModalboxContainer ){
        jQuery(settings.setFaderLayer).remove();
        jQuery(settings.setModalboxContainer).remove();
        jQuery("iframe.modalBoxIe6layerfix").remove();
    }
};


You can add an event trigger inside the modalBox.close method and listen to it:

jQuery(document).bind('modalclose'), function() {
    // stuff to do when the modalbox closes
});

var fn = jQuery.fn.modalBox.close;
jQuery.fn.modalBox.close = function() {
    fn.apply(this, arguments);
    jQuery(document).trigger('modalclose');
};

You can also apply a custom event for removing elements in jQuery:

(function() {
    var ev = new $.Event('remove'),
        orig = $.fn.remove;
    $.fn.remove = function() {
        $(this).trigger(ev);
        orig.apply(this, arguments);
    }
})();

$('iframe').bind('remove', function() {
    // do stuff when an iframe was removed
});


In most browsers (sadly not IE < 9) you could use DOMNodeRemoved event. I don't know about your jQuery plug-in, but here's a plain JavaScript example:

var iframe = document.getElementById("your_iframe");

document.body.addEventListener("DOMNodeRemoved", function(evt) {
    var removedNode = evt.target;
    if (removedNode == iframe) {
        alert("iframe removed");
    }
}, false);

I'm sure you'd be better off hacking around with the plug-in to get it to tell you when it closes the dialog though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜