Safari doesn't call iframe onload when src is not a valid site

For the next iframe, Safari never calls the onload function and does not display anything in the iframe. All other browsers I tested call onload and display the webpage with a default error.

<html> <body> <iframe src="http://www.asdkjhjhkjhkjhjhjhfhkjhsdflkjahdsjfhasf.com" onload="alert('iframe loaded');"> </iframe> </body> </html> 

Why is this happening? If there is no solution to this problem, then I need to find a way to detect if the iframe is not loading.

+2
javascript html safari iframe
source share
1 answer

I just stumbled upon this post with a workaround for the missing load event in Safari. A post from 2011, but it seems to be still relevant.

The solution proposed for Safari is to check the readyState event on an iframe document like this:

 <iframe id="ifra" src="http://www.asdkjhjhkjhkjhjhjhfhkjhsdflkjahdsjfhasf.com" onload="alert('iframe loaded');"> </iframe> <script> var iframe = document.getElementById('ifra'), doc = iframe.contentDocument || iframe.contentWindow; if (doc.document) doc = doc.document; var _timer = setInterval(function() { if (doc.readyState == 'complete') { clearInterval(_timer); alert('iframe ready'); } }, 1000); </script> 

According to Mozillaโ€™s documentation on contentDocument , you donโ€™t need to include a workaround with contentWindow when running just this script in Safari, but I saved it here if you want to use it in browsers.

+2
source share

All Articles