Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 646095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:34:27+00:00 2026-05-13T21:34:27+00:00

How do you check if there is an internet connection using jQuery? That way

  • 0

How do you check if there is an internet connection using jQuery? That way I could have some conditionals saying "use the google cached version of JQuery during production, use either that or a local version during development, depending on the internet connection".

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:34:28+00:00Added an answer on May 13, 2026 at 9:34 pm

    The best option for your specific case might be:

    Right before your close </body> tag:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.10.2.min.js"><\/script>')</script>
    

    This is probably the easiest way given that your issue is centered around jQuery.

    If you wanted a more robust solution you could try:

    var online = navigator.onLine;
    

    Read more about the W3C’s spec on offline web apps, however be aware that this will work best in modern web browsers, doing so with older web browsers may not work as expected, or at all.

    Alternatively, an XHR request to your own server isn’t that bad of a method for testing your connectivity. Considering one of the other answers state that there are too many points of failure for an XHR, if your XHR is flawed when establishing it’s connection then it’ll also be flawed during routine use anyhow. If your site is unreachable for any reason, then your other services running on the same servers will likely be unreachable also. That decision is up to you.

    I wouldn’t recommend making an XHR request to someone else’s service, even google.com for that matter. Make the request to your server, or not at all.

    What does it mean to be “online”?

    There seems to be some confusion around what being “online” means. Consider that the internet is a bunch of networks, however sometimes you’re on a VPN, without access to the internet “at-large” or the world wide web. Often companies have their own networks which have limited connectivity to other external networks, therefore you could be considered “online”. Being online only entails that you are connected to a network, not the availability nor reachability of the services you are trying to connect to.

    To determine if a host is reachable from your network, you could do this:

    function hostReachable() {
    
      // Handle IE and more capable browsers
      var xhr = new ( window.ActiveXObject || XMLHttpRequest )( "Microsoft.XMLHTTP" );
    
      // Open new request as a HEAD to the root hostname with a random param to bust the cache
      xhr.open( "HEAD", "//" + window.location.hostname + "/?rand=" + Math.floor((1 + Math.random()) * 0x10000), false );
    
      // Issue request and handle response
      try {
        xhr.send();
        return ( xhr.status >= 200 && (xhr.status < 300 || xhr.status === 304) );
      } catch (error) {
        return false;
      }
    
    }
    

    You can also find the Gist for that here: https://gist.github.com/jpsilvashy/5725579

    Details on local implementation

    Some people have commented, “I’m always being returned false”. That’s because you’re probably testing it out on your local server. Whatever server you’re making the request to, you’ll need to be able to respond to the HEAD request, that of course can be changed to a GET if you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using jQuery, how do you check if there is an option selected in a
Is there any way to check whether a file is locked without using a
When I need some complex algorithm I first check if there's anything relevant already
Is there a way to check if a file has been opened by ReWrite
Is there a way to check to see if a date/time is valid you
Is there a way with SVN to check out from a remote repository to
Is there any way to check if a given index of an array exists?
Is there a way to check how much a flash file is (down)loaded to
Is there any way in Linq to check to see if a record of
Below is a stored procedure to check if there is a duplicate entry in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.