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 9131217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:06:28+00:00 2026-06-17T08:06:28+00:00

I have some simple code that will run to check Internet connectivity and I’m

  • 0

I have some simple code that will run to check Internet connectivity and I’m using localStorage to hold the variable value and yet when I run it the value of that variable isn’t effected. So, what can I do to fix it. Please help. If the state is changed to true the submit will happen else otherwise it will be prevented.

localStorage.setItem("state","");

function checkConnection(){             
   $.ajax({
       url:"assets/ping.js",
       success:function(res){
            localStorage.setItem("state","true");
       },
       onerror:function(){
            localStorage.setItem("state","false");
       } 
   });
 }

$("form").submit(function(e){

   checkConnection();

   if(localStorage.getItem("state") == "false"){
        return false;
   } else if(localStorage.getItem("state") == "true") {
        return true;
   }

});
  • 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-06-17T08:06:28+00:00Added an answer on June 17, 2026 at 8:06 am

    This is a sychronization isssue. Your API call to check the connection does not complete before the form is submitted. You want the AJAX call to block until it gets a response. (actually I guess it should be called SJAX for synchronous).

    You have to add async is false to the settings parameter.
    Read: http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings

    Here is how you do that with jQuery:

    $.ajax({
       url:"assets/ping.js",
       async: false
       success:function(res){
           localStorage.setItem("state", "true");
       },
       error: function(){
            localStorage.setItem("state","false");
       }           
    });
    

    Also note that the error handler should be under error not onerror.

    Finally, if all you are trying to do is detect if the Internet connection was lost you can do that on most browsers by checking the value of window.navigator.onLine. See: http://www.whatwg.org/specs/web-apps/current-work/#browser-state

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

Sidebar

Related Questions

I'have written a simple code, that gets accelerometer&orientation meter and display some graphics based
So I have a few properties that I'm using in some sample code I'm
Trying to get comfortable with jQuery and I have encountered some sample code that
I have some VERY simple code to return the title for a section header:
I have simple HTML code with some JavaScript. It looks like: <html> <head> <script
Question simple and quick: I have started to use Netbeans to write some code
I have some simple constraints involving multiplication of reals in z3 that are producing
Possible Duplicate: Is JavaScript’s Math broken? I wrote some simple C# code that runs
I have one simple program that's using Qt Framework. It uses QProcess to execute
I have some simple code: function testing(){ for (a=1; a<=4; a++) { this[btn+a].enabled =

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.