Skip to main content
Fixed typo
Source Link
Giddy Naya
  • 4.5k
  • 2
  • 19
  • 32

The statements above is simply trying to let you know that the browsers alone cannot tell. So basically this option is unreliable.

Here is a simple snippet to pingfetch own resource:

// This fetches your website's favicon, so replace path with favicon url
// NoteNotice the appended date param which helps prevent browser caching.
fetch('/favicon.ico?d='+Date.now())
  .then(response => {
    if (!response.ok)
      throw new Error('Network response was not ok');

   // At this point we can safely assume the user has connection to the internet
        console.log("Internet connection available"); 
  })
  .catch(error => {
  // The resource could not be reached
        console.log("No Internet connection", error);
  });

Below a simple snippet to pingfetch external resource, same as above but with external resource url:

// Firstly you trigger a resource available from a reputable site
// For demo purpose you can use the favicon from MSN website
// Also notenotice the appended date param which helps skip browser caching.
fetch('https://static-global-s-msn-com.akamaized.net/hp-neu/sc/2b/a5ea21.ico?d='+Date.now())
  .then(response => {
  // Check if the response is successful
    if (!response.ok)
      throw new Error('Network response was not ok');

// At this point we can safely say the user has connection to the internet
        console.log("Internet available"); 
  })
  .catch(error => {
  // The resource could not be reached
        console.log("No Internet connection", error);
  });

The statements above is simply trying to let you know that the browsers alone cannot tell. So basically this option is unreliable.

Here is a simple snippet to ping own resource:

// This fetches your website's favicon, so replace path with favicon url
// Note the appended date param which helps prevent browser caching.
fetch('/favicon.ico?d='+Date.now())
  .then(response => {
    if (!response.ok)
      throw new Error('Network response was not ok');

   // At this point we can safely assume the user has connection to the internet
        console.log("Internet connection available"); 
  })
  .catch(error => {
  // The resource could not be reached
        console.log("No Internet connection", error);
  });

Below a simple snippet to ping external resource, same as above but with external resource url:

// Firstly you trigger a resource available from a reputable site
// For demo purpose you can use the favicon from MSN website
// Also note the appended date param which helps skip browser caching.
fetch('https://static-global-s-msn-com.akamaized.net/hp-neu/sc/2b/a5ea21.ico?d='+Date.now())
  .then(response => {
  // Check if the response is successful
    if (!response.ok)
      throw new Error('Network response was not ok');

// At this point we can safely say the user has connection to the internet
        console.log("Internet available"); 
  })
  .catch(error => {
  // The resource could not be reached
        console.log("No Internet connection", error);
  });

The statements above is simply trying to let you know that browsers alone cannot tell. So basically this option is unreliable.

Here is a simple snippet to fetch own resource:

// This fetches your website's favicon, so replace path with favicon url
// Notice the appended date param which helps prevent browser caching.
fetch('/favicon.ico?d='+Date.now())
  .then(response => {
    if (!response.ok)
      throw new Error('Network response was not ok');

   // At this point we can safely assume the user has connection to the internet
        console.log("Internet connection available"); 
  })
  .catch(error => {
  // The resource could not be reached
        console.log("No Internet connection", error);
  });

Below a simple snippet to fetch external resource, same as above but with external resource url:

// Firstly you trigger a resource available from a reputable site
// For demo purpose you can use the favicon from MSN website
// Also notice the appended date param which helps skip browser caching.
fetch('https://static-global-s-msn-com.akamaized.net/hp-neu/sc/2b/a5ea21.ico?d='+Date.now())
  .then(response => {
  // Check if the response is successful
    if (!response.ok)
      throw new Error('Network response was not ok');

// At this point we can safely say the user has connection to the internet
        console.log("Internet available"); 
  })
  .catch(error => {
  // The resource could not be reached
        console.log("No Internet connection", error);
  });
Fixed typo
Source Link
Giddy Naya
  • 4.5k
  • 2
  • 19
  • 32
  • Try to access a resource on your own server because this is your users environment (Typically i use website's favicon because the response is very light and it is not frequently updated).
  • If there is no connection to the resource, simplesimply say "Error in connection" or "Connection lost" when you need to make notificationsnotify the user rather than assume a broad "No internet connection" which depends on many factors.
  • Try to access a resource on your own server because this is your users environment (Typically i use website's favicon because the response is very light and it is not frequently updated).
  • If there is no connection to the resource, simple say "Error in connection" or "Connection lost" when you need to make notifications rather than a broad "No internet connection" which depends on many factors.
  • Try to access a resource on your own server because this is your users environment (Typically i use website's favicon because the response is very light and it is not frequently updated).
  • If there is no connection to the resource, simply say "Error in connection" or "Connection lost" when you need to notify the user rather than assume a broad "No internet connection" which depends on many factors.
Added extra info
Source Link
Giddy Naya
  • 4.5k
  • 2
  • 19
  • 32

I know this question has already been answered but i will like to add my 10 cents explaining what's better thanand what's not.

The statements above is simply trying to let you know that the browsers alone cannot tell. So basically this option is unreliable.

SimpleHere is a simple snippet to ping own resource.:

// This fetches your website's favicon, so replace path with favicon url
// Note the appended date param which helps prevent browser caching.
// This fetches your website's favicon
fetch('/favicon.ico?d='+Date.now())
  .then(response => {
    if (!response.ok)
      throw new Error('Network response was not ok');

   // At this point we can safely assume the user has connection to the internet
        console.log("Internet connection available"); 
  })
  .catch(error => {
  // The resource could not be reached
        console.log("No Internet connection", error);
  });
[CORS][2]CORS

This option involves making HTTP request to an external server resource and if reachable assume internet availability else the user is offline. The major caveat to this is the Cross-origin resource sharing (CORS) which act as a limitation. Most reputable websites blocks CORS requests but for some you can have your way.

SimpleBelow a simple snippet to ping external resource, same as above but with external resource url.:

I know this question has already been answered but i will like to add my 10 cents explaining what's better than what's not.

So basically this option is unreliable.

Simple snippet to ping own resource.

// Note the appended date param which helps prevent browser caching.
// This fetches your website's favicon
fetch('/favicon.ico?d='+Date.now())
  .then(response => {
    if (!response.ok)
      throw new Error('Network response was not ok');

   // At this point we can safely assume the user has connection to the internet
        console.log("Internet connection available"); 
  })
  .catch(error => {
  // The resource could not be reached
        console.log("No Internet connection", error);
  });
[CORS][2]

This option involves making HTTP request to an external server resource and if reachable assume internet availability else the user is offline. The major caveat to this is the Cross-origin resource sharing (CORS) which act as a limitation. Most reputable websites blocks CORS requests but for some you can have your way.

Simple snippet to ping external resource, same as above but with external url.

I know this question has already been answered but i will like to add my 10 cents explaining what's better and what's not.

The statements above is simply trying to let you know that the browsers alone cannot tell. So basically this option is unreliable.

Here is a simple snippet to ping own resource:

// This fetches your website's favicon, so replace path with favicon url
// Note the appended date param which helps prevent browser caching.
fetch('/favicon.ico?d='+Date.now())
  .then(response => {
    if (!response.ok)
      throw new Error('Network response was not ok');

   // At this point we can safely assume the user has connection to the internet
        console.log("Internet connection available"); 
  })
  .catch(error => {
  // The resource could not be reached
        console.log("No Internet connection", error);
  });
CORS

This option involves making HTTP request to an external server resource and if reachable assume internet availability else the user is offline. The major caveat to this is the Cross-origin resource sharing which act as a limitation. Most reputable websites blocks CORS requests but for some you can have your way.

Below a simple snippet to ping external resource, same as above but with external resource url:

Added incomplete edits
Source Link
Giddy Naya
  • 4.5k
  • 2
  • 19
  • 32
Loading
Source Link
Giddy Naya
  • 4.5k
  • 2
  • 19
  • 32
Loading