1

I need to show few Iframes in my Angular App.

for that , my HTML is like

<iframe ng-src="{{iframeObj.iUrl}}" frameborder="0"></iframe>

But when the iUrl is not allowed in Iframes that is when X-Frame-Options:SAMEORIGIN or DENY

Then my IFrame does not come , and its natural that it will not come.

But I want to show an alternative text when Iframe does not load correctly. Like Image tag has and alt attribute.

I need some way to show alt Text when Iframe can not load.

AngularJS and plain JavaScript solution is mostly desired.

Thanks in advance.

  • I think ``s have an `onerror` event. Try it. – Ismael Miguel Oct 21 '14 at 13:56
  • Try this: http://stackoverflow.com/questions/15273042/catch-error-if-iframe-src-fails-to-load-error-refused-to-display-http-ww – Ismael Miguel Oct 21 '14 at 13:59
  • Or this: http://stackoverflow.com/questions/6327128/can-i-catch-exception-of-iframe-in-parent-window-of-iframe – Patrick Q Oct 21 '14 at 14:00
  • @PatrickQ You won't have access to `contentWindow` when the `src` is not on the same domain, which is what the OP is asking. – Ismael Miguel Oct 21 '14 at 14:03
  • 1
    @IsmaelMiguel The top answer in that question mentions how to handle the case of a different domain if you have control of both systems. OP here does not mention whether or not that is the case, so I see no harm in providing that solution in a comment (I did not mark this as a duplicate of that question). – Patrick Q Oct 21 '14 at 14:08
  • @PatrickQ `Iframe Alt Text When Iframe load error for X-Frame-Options:SAMEORIGIN | DENY` The title denies what you said in your comment. – Ismael Miguel Oct 21 '14 at 14:09
  • @IsmaelMiguel That says nothing about whether or not OP controls both systems, only that they are not the same domain. – Patrick Q Oct 21 '14 at 14:09
  • @PatrickQ That is true, but since the OP asks for a specific error, we can assume that he doesn't control the other domain, or he would have set the header `x-frame-options` and wouldn't have this problem. – Ismael Miguel Oct 21 '14 at 14:31

1 Answers1

0

I have found a solution for your problem:

The idea is to set a onload event BEFORE you set the src.

You can try this code:

document.getElementsByTagName('iframe').onload=function(){
    try{
        var win=this.contentWindow;
        win.closed && error_function();
    }catch(e){
        error_function();
    }
};

Check here for how it works: http://jsfiddle.net/tec6y2y9/1/

Note: On the fiddle, I made some changes to show that it actually works! The error_function() is a fictional function to show where you put your code in case of failure!

I hope this works for you.

Ismael Miguel
  • 4,185
  • 1
  • 31
  • 42
  • @CBroe It isn't only on chrome :/ Firefox doesn't like my code too :/ – Ismael Miguel Oct 21 '14 at 14:50
  • @CBroe `win.closed` is set to `false` on chrome and firefox **even when the connection is closed**. – Ismael Miguel Oct 21 '14 at 14:56
  • @IsmaelMiguel , It does not work , I dont get any alert in jsfiddle. If I can get the error event of the Iframe could not load , Its my requirement... – Shahjada Talukdar Oct 21 '14 at 16:23
  • @DestroMas I am working on a cross-browser solution. It does work on Opera 12.17, which surprised me when CBroe said it wasn't working on Chrome. Currently I'm at work, but I already have a solution (more like an attempt) in my mind. – Ismael Miguel Oct 21 '14 at 16:54
  • @DestroMas I can't find a pure client-side answer. The standard that "should work" is up there. It works only on Opera 12.17 and below (maybe 'till Opera 9?). I have an idea for a server side + client side solution. – Ismael Miguel Oct 21 '14 at 22:51
  • @oshingc Can you be more specific? "It doesn't work" won't help me. I need to know the OS, Firefox version and the VERY EXACT error message. – Ismael Miguel Mar 31 '16 at 19:20