Javascript code to detect loss of internet connectivity

Posted on 2004-11-19
Medium Priority
Last Modified: 2010-08-05
I am writing a web based chat service much like the one here:

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!
Question by:NeoGemini

Expert Comment

ID: 12623729
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..
LVL 63

Expert Comment

ID: 12623779
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.


Expert Comment

by:Andrea Ercolino
ID: 12627942
you could put the IFRAME content in another invisible container and then copy it to the visible one if it's valid
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

Andrea Ercolino earned 1500 total points
ID: 12628152
the validity check could be in the content itself... a function call at the end of the body will do
that function (refreshVisible) should be declared in the main container and should do the following
1. copy the content of the invisible container to the visible IFRAME --> if there is a page error, then there is no function call, then there is no visible refresh, and no error shown
2. reset a timer for calling a new function (refreshHidden) in two seconds --> if there is a page error, then there is no function call, then the timer will continue to run and possibly call the refreshHidden function upon expiration
3. you'll need a check at the beginnig fro preventing execution when called from the visible container rather than the hidden one

it's just an idea...

Expert Comment

ID: 12629971
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.

Author Comment

ID: 12632477
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!

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question