If an image fails to load on a page we have the onerror function which can load a default image.
Is there an equivalent for loading a site into an iFrame?
E.g. mashable.com can be loaded into an iFrame whereas many social sites e.g. Facebook, Twitter et al can not.
So when a user tries to load e.g. Twitter and nothing is shown, I'd like a message to show saying "this page cannot be displayed" and then open the link in a new tab instead and direct them after say 3 seconds.
Not sure if this is possible.
An iframe, short for inline frame, is an HTML element that contains another HTML document within it. The iframe element is specified with the iframe tag. It may be placed anywhere in an HTML document, and thus anywhere on a web page.
To check if iframe is loaded or it has a content with JavaScript, we can set the iframe's onload property to a function that runs when the iframe is loaded. document. querySelector("iframe").
You see code iframes commonly used on websites that need to include external content like a Google map or a video from YouTube. Both of those popular websites use iframes in their embed code.
IFrames are not obsolete, but the reasons for using them are rare. Using IFrames to serve your own content creates a "wall" around accessing the content in that area. For crawlers like Google, It's not immediately clear that cotent in an iframe will be ranked as highly as if the content were simply part of the page.
Due to security restrictions, that isn't possible from the client side. However you have two alternative solutions:
See Catch error if iframe src fails to load . Error :-"Refused to display 'http://www.google.co.in/' in a frame.."
I had the same problem and I was using AngularJS, I got it working using the method below. Hope it helps..
Set an iframe in the html
<div ng-if="!iframeError">
<iframe id="iframeID" src="http://www.google.com" height="500px"></iframe>
</div>
<div ng-if="iframeError">Not Supported</div>
I set an interval for listening if the iframe is available
var ife = setInterval(function () {
if ($("#iframeID")) {
$("#iframeID").load(function (ev) {
var iframe = $("#iframeID");
var iframeDocument;
try {
iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
}
catch (e) {
window.open("http://www.google.com");
$scope.iframeError = true;
//I opened the url in new page
//you can handle it any way you want
}
});
clearInterval(ife);
}
}, 200);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With