Safe/consistent mechanism to check iframe loading?
I've run into a problem triggering an event when a page loads in an iframe. Lots of posts about it in various places online, but I'm turning here in hope of finding the best answer.
I have an <iframe src="foo"> in my page, and was trying to trigger an event when foo was loaded. The obvious $(iframe).contents().ready failed in a strange way. It turns out that (even though src foo is specified in the iframe) the iframe is initially created with an empty document which is later *replaced* by foo. Unfortunately the contents() method returned the initial empty document which (was ready but) wasn't the document I actually wanted.
How can I trigger an event when the document that I actually asked for is ready? I can use a polling hack, testing whether the iframe's document.location.href === foo, but I'd like something that feels more "correct".