www.webdeveloper.com
Results 1 to 3 of 3

Thread: how do I check if connection to site is lost with jQuery

  1. #1
    Join Date
    Feb 2007
    Posts
    66

    how do I check if connection to site is lost with jQuery

    Hey i want to have a popup that shows up if the user loses connection to my website, i would prefer it to be in jQuery as that's what I'm using, and it seems more light weight on the page

    I have this code, but it doesn't seem to work... it should check on document ready then keep trying every 5 seconds
    PHP Code:
    $(document).ready(function() {
        checkConnection();
    });

    function checkConnection() {
        $.ajax({
            type:"post",
            url:"http://<?php echo $_SERVER['SERVER_NAME']; ?>/connectionCheck.php",
            error:function(XMLHttpRequest, textStatus, errorThrown) { 
                alert('you seem to have lost connetion to the site.');
            },
            success:function(data) {
            }        
        });
        
        setTimeout(function() {
            checkConnection();
        }, 5000);
    }
    Last edited by vinny619; 08-02-2010 at 06:37 PM.

  2. #2
    Join Date
    Feb 2007
    Posts
    66
    hey, i got it working... I just moved the alert into the success function instead, and it worked...

    but i changed it to display a div on disconnect and hide it on connect, like below

    PHP Code:
    $(document).ready(function() {
        checkConnection();
    });

    function checkConnection() {
        $.ajax({
            type:"post",
            url:"http://<?php echo $_SERVER['SERVER_NAME']; ?>/connectionCheck.php",
            error:function(XMLHttpRequest, textStatus, errorThrown) { 
                hideError();
            },
            success:function(data) {
                showError('Page Error!!', 'you seem to have lost connetion to the site.');
            }        
        });
        
        setTimeout(function() {
            checkConnection();
        }, 5000);
    }

  3. #3
    Join Date
    Feb 2007
    Posts
    66
    okay it worked for a while but now its showing up even if i am connected to the server... even on localhost, has any one got a working code for this...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles