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

  • SEARCH
  • Home
  • 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

Ask A Question

Stats

  • Questions 322k
  • Answers 322k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I personally choose names that represent how the cars will… May 14, 2026 at 12:50 am
  • Editorial Team
    Editorial Team added an answer Looks ok on the client side. Just use URLDecode on… May 14, 2026 at 12:50 am
  • Editorial Team
    Editorial Team added an answer Overriding "change_form.html" is the right way to go. You can… May 14, 2026 at 12:50 am

Related Questions

Possible Duplicate: How to check if internet connection is present in java? I want
I'm programming an online game for two reasons, one to familiarize myself with server/client
I am planning on putting up a web service, or some other service exposed
I'm working on a multi-user internet database-driven website with SQL Server 2008 / LinqToSQL
How do you check if there is an attribute on an element in jQuery?

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.