Javascript callback when IFRAME is finished loading?

First up, going by the function name xssRequest it sounds like you’re trying cross site request – which if that’s right, you’re not going to be able to read the contents of the iframe.

On the other hand, if the iframe’s URL is on your domain you can access the body, but I’ve found that if I use a timeout to remove the iframe the callback works fine:

// possibly excessive use of jQuery - but I've got a live working example in production
$('#myUniqueID').load(function () {
  if (typeof callback == 'function') {
    callback($('body', this.contentWindow.document).html());
  }
  setTimeout(function () {$('#frameId').remove();}, 50);
});

Leave a Comment