<h1>Handling Offline iFrame Content</h1>
<section>
<h2>
Using HTML5 srcdoc tag
</h2>
<iframe src="http://google.com/thisfiledoesntexist.html" onerror="$(this).attr('srcdoc','<p>You need to be online to view this content.</p>');" onload="if(navigator.onLine===false) eval(this.getAttribute('onerror'));">
iframes not supported in your browser.
</iframe>
</section>
<section>
<h2>
Replacing the iframe
</h2>
<iframe src="http://google.com/thisfiledoesntexist.html" onerror="$(this).replaceWith('<p>You need to be online to view this content.</p>');" onload="if(navigator.onLine===false) eval(this.getAttribute('onerror'));">
iframes not supported in your browser.
</iframe>
</section>
<section>
<h2>
Using HTML5 srcdoc tag - Without jQuery
</h2>
<iframe src="http://google.com/thisfiledoesntexist.html" onerror="this.setAttribute('srcdoc','<p>You need to be online to view this content.</p>');" onload="if(navigator.onLine===false) eval(this.getAttribute('onerror'));">
iframes not supported in your browser.
</iframe>
</section>
<section>
<h2>
Replacing the iframe - Without jQuery
</h2>
<iframe src="http://google.com/thisfiledoesntexist.html" onerror="var p=document.createElement('p');this.parentNode.replaceChild(p,this);p.innerHTML='You need to be online to view this content.';" onload="if(navigator.onLine===false) eval(this.getAttribute('onerror'));">
iframes not supported in your browser.
</iframe>
</section>
CSS
/* this CSS is not necessary, it's just for the display of the example */
section { display:block;width:49.5%;min-height:200px;float:left;padding:8px;box-sizing:border-box;}
section iframe {width:90%;max-height:100px;}
section h2 { border-bottom:1px solid #999;}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.