PDA

View Full Version : Online or Offline detection


Jerome
11-29-2002, 06:34 PM
Hi,

I like the people to watch my page when they are online, but NOT offline. Is it possible to detect whether a page is viewed with or without connection?

Thanks,
Jerome

chrismiceli
11-29-2002, 06:45 PM
if they are not connected there is no way to tell if they are viewing the page or not, if they are on, you can do it with some easy server side like cgi or asp.

Vladdy
11-29-2002, 06:57 PM
sure there is
just examine window.location.href
if it is file://something the page is viewed locally as a file.
if it is http://, the page is served using http:// protocol
in the latter case you can check if the site is served by your server and not a local one by checking if the string after the http:// matches your domain.
Or you can make a simple if

if(window.location.href!='http://www.yoursite.com/thepage.html)
{ // page is viewed locally as a file or from another server

}

Borgtex
11-29-2002, 07:28 PM
Modified Vladdy code to detect if the browser is online and if the page is being viewed locally or not:

if (!navigator.onLine || self.location.href.indexOf('http://')==-1)
{ // page is viewed locally as a file or offline, from the cache

}

chrismiceli
11-29-2002, 07:32 PM
why do you do this
if (!navigator.onLine || self.location.href.indexOf('http://')==-1)
{ // page is viewed locally as a file or offline, from the cache

}
when you can do this
if (!navigator.onLine || self.location.protocol != "http:")
{ // page is viewed locally as a file or offline, from the cache
}

Jerome
11-29-2002, 11:07 PM
Thanks,

Jerome