Link to home
Start Free TrialLog in
Avatar of NeoGemini
NeoGemini

asked on

Javascript code to detect loss of internet connectivity

I am writing a web based chat service much like the one here:
http://code.jenseng.com/jenChat/

My question is this, if you look at that example, it uses a hidden iFrame that auto-refreshes itself then writes any new chat lines it pulls to the main menu, which works great, until the client looses their internet connection. Then the auto-refreshing iFrame turns into a page cannot be displayed error, and no further lines in the chat are displayed since that page is no-longer auto-refreshing. My question becomes what would be the best way to detect that the frame is no longer refreshing and force a reload every 5-10 seconds until the connection resumes itself, then of course the iFrame's own refresh would take over? This has to assume a complete loss of internet connectivity, so I would assume it would be a bit of javascript in the main page that does not auto-refresh, so it would continue to work even if a loss of connectivity is experienced.

Any help in the right direction would be appreciated, thank you!
Avatar of robotman757
robotman757

You can try soemthing like this...create a variable and then get the html of the iFrame.
var txt = window.frames["youriframe"].innerHTML;
check the txt variable for "The page cannot be displayed"...if it contains this text, tell the script to refresh, and if not then do nothing. I can see this working in my head, but have no way to test it, so I am not totally sure..
Avatar of Zvonko
My idea would be to pleace something at the beginning of every content returned to the iframe, something like a content title.
If the title is not there, then is the content not comming from the server, then the output should be suppressed and the refresh rate lowered.

Of course does the title not need to be displayed to the user.

you could put the IFRAME content in another invisible container and then copy it to the visible one if it's valid
ASKER CERTIFIED SOLUTION
Avatar of Andrea Ercolino
Andrea Ercolino

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You could have a timer on the main page that checks the value of a variable on the popup. If the variable is there then the window is good. If not then the window is bad then close it.
Avatar of NeoGemini

ASKER

I'm working on these ideas, RAPUTA your idea seems the most promising, can you expand a little on your explanation? I greatly appreciate it!