0

I have an offline html, and I add an Iframe inside it that redirect to a site if there's connection available. but in case if there's no internet connection the Iframe will redirect to offline page/html.

Can someone explain to me what code that I need to use? I'm using checking internet connection script but i don't know what code that I need to insert inside javascript for exchange alert('You are Online') > redirect to online site and alert('You Are Offline') > redirect to offline page

if(navigator.onLine)
  {
    alert('You are Online');
  }
  else
  {
    alert('You are Offline')
  }
<iframe height="500px" width="100%" src="http://example.github.io/" id="foo"></iframe>
<div style="text-align:center"> This Iframe </div>
Aotsuya
  • 1
  • 4

2 Answers2

0

Try something like this

<iframe id="browse" src="" onload="doNothing()" onerror="doRedirect()"></iframe>

And in JavaScript

function doRedirect(){
    document.getElementById('browse').src = "your offline page";
}

or

function doRedirect(){
    window.frames['browse'].location.replace("url to offline page");
}
Jyothi Babu Araja
  • 10,076
  • 3
  • 31
  • 38
  • it won't work, after I shut the connection, the Iframe load "Server Not Found" in firefox, not load the offline html/page – Aotsuya Feb 27 '16 at 10:12
0

Use can using GlobalEventHandlers.onerror for your fallback. For example:

var my_iframe = document.getElementById('my_iframe');

my_iframe.onerror = (my_iframe.src = 'http://example.com');
<iframe id="my_iframe" src="http://example.xxx/"></iframe>

Or see my fiddle.

Tuan Ha
  • 620
  • 2
  • 8
  • 25