I am using Knockout.js
to bind iframe src
tag(This will be configurable with respect to User).
Now, if user has configured http://www.google.com (I know it won't load in iframe, thats why I am using it for -ve scenario) and that has to be shown in IFrame. but it throws error:-
Refused to display 'http://www.google.co.in/' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.
I have the following code for Iframe:-
<iframe class="iframe" id="iframe" data-bind="attr: {src: externalAppUrl, height: iframeheight}">
<p>Hi, This website does not supports IFrame</p>
</iframe>
What I want is, if the URL fails to load. I want to display Custom Message. FIDDLE HERE
Now, if I use onload and onerror as:-
<iframe id="browse" style="width:100%;height:100%" onload="alert('Done')" onerror="alert('Failed')"></iframe>
It works fine loading w3schools.com but not with google.com.
Secondly:- if I make it as a function and try like I have done in my fiddle, it doesn't works.
<iframe id="browse" style="width:100%;height:100%" onload="load" onerror="error"></iframe>
I don't know how should I make it run and capture the error.
Edited:- I have seen Want to call a function if iframe doesn't load or load's question in stackoverflow but it shows error for sites that can be loaded in iframe.
Also, I have looked into Stackoverflow iframe on load event Thanks!!
You wont be able to do this from the client side because of the Same Origin Policy set by the browsers. You wont be able to get much information from the iFrame other than basic properties like its width and height.
Also, google sets in its response header an 'X-Frame-Options' of SAMEORIGIN.
Even if you did an ajax call to google you wont be able to inspect the response because the browser enforcing Same Origin Policy.
So, the only option is to make the request from your server to see if you can display the site in your IFrame.
So, on your server.. your web app would make a request to www.google.com and then inspect the response to see if it has a header argument of X-Frame-Options. If it does exist then you know the IFrame will error.
Roblox uses an
iframe
trick to detect if the game is installed using<iframe src="roblox-player:foo">
by detecting a custom URI handler. Detecting thisiframe
failure triggers the "Download" button to appear. How do they do it then?@QZ Support I have no idea what your talking about.
@EvanLarsen I'm mentioning a very popular video game that successfully uses the
<iframe>
technique to launch (and I was hoping detect) a URI handler registered in the OS. It's a free game, you can install it and see for yourself. When you join a game through the browser, it uses an iframe hack to launch the game. If it fails, they show the "download" button. Somehow they know whether or not the URI was handled properly. My initial guess was catching theiframe
error, but the answers (as well as my tests) seem to suggest that it's not possible. How do they know when the iframe fails?This is just a guess but I bet the website is just calling a self hosted web app on your local machine. If the call fails then it shows the download button. Once you download and install the game on your machine then the website will be able to call the local self hosted web app. By self hosted web app.. I mean just an app with http end points which can be called by any website which knows about it. (ie. localhost:7845/opengame/room/2534 )
The
localhost:7845
is quite the assumption. First, it will hit mixed content warnings/errors (roblox.com uses HTTPS) and next, it would need failover ports in case of conflicts. The technique used to launch the software is aniframe
. This can be observed by examining the page source. Somehow when this fails to launch the software, they're able to detect it. This may be done through a "phone home" + timeout method using the browser tracker ID. I mention this because thisiframe
failure detection is useful for launching associated apps, but only if the failure can be detected.