Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if a site will open in an iframe

Tags:

html

iframe

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.

like image 798
StudioTime Avatar asked Sep 16 '13 13:09

StudioTime


People also ask

Can I use iframe for any website?

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.

How do I check if an iframe is present?

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").

Does anyone use IFrames anymore?

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.

Is iframe going away?

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.


2 Answers

Due to security restrictions, that isn't possible from the client side. However you have two alternative solutions:

  • Do a call from the server and check for X-Frame-Options headers.
  • Alternatively you can set a timeout and assume the page can't be loaded if the load event isn't fired after some time.

See Catch error if iframe src fails to load . Error :-"Refused to display 'http://www.google.co.in/' in a frame.."

like image 59
hectorct Avatar answered Sep 20 '22 13:09

hectorct


I had the same problem and I was using AngularJS, I got it working using the method below. Hope it helps..

  1. 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>
    
  2. 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);
    
like image 25
Satpal Tanan Avatar answered Sep 22 '22 13:09

Satpal Tanan