Javascript / jquery: determine if the iframe page failed to load

I have an iframe that loads an external domain, and sometimes the domain goes down or expires. I tried to figure out how to determine if the iframe page is loaded or not. I cannot use AJAX because the domain is different from the main page (protection against cross origin).

Here is a sample code, I think it should work (but it is not):

<!doctype html> <html> <head> </head> <body> <iframe id="myIframe" src="http://www.domain.com/"></iframe> <script src="jquery.min.js"></script> <script> $('#myIframe').on('load',function() { alert('Loaded correctly'); }); $('#myIframe').on('error',function() { alert('Load failed!'); }); </script> </body> </html> 

I have no way to add code to a remote domain or upload a jsonp file, so AJAX / JSONP tricks are not possible (as far as I know).

+4
source share
1 answer

Try the following:

 $($('#myIframe')[0].document).ready(function() { alert('loaded'); }); 

http://jsfiddle.net/DepKF/33/

0
source

Source: https://habr.com/ru/post/1486136/


All Articles