Introduction
checkNet is a plugin for the jQuery JavaScript library which, when used on a webpage, will detect if the internet connection is lost and add a warning to the top of the page. Amongst other uses, this could be useful for your site’s users if they have to enter any significant amounts of data before submitting a form (we’ve all had that annoying situation where you type something up and then lose it all when the form doesn’t submit properly!)
Demo
You can view the demo at http://tomriley.net/checknet/
Load the page, then disable your internet connection (possibly by unplugging the network cable of your computer, or setting your device to “Airplane mode”). After a few seconds you will see a warning message appear at the top of the page, and the form’s submit button will be disabled. Re-activate your internet connection and you will see that the warning dissapears and the submit button is re-enabled.
Useage
To use the plugin on your page, first download the zip file from http://tomriley.net/checknet/checknet.zip
Place the checknet.css and checknet-1.0.min.js file in the location you are using for those file types on your site. I use /css/ and /js/ but you may have your own preference. The index.html file is there for demo purposes only, and for you to look at the code of to see how to use the plugin on your HTML files.
Inside the <head></head> tags of your site’s HTML files, add these lines of code:
<link rel=”stylesheet” href=”css/checknet.css” media=”all” />
<script src=”js/checknet-1.0.min.js”></script>
<script>
$(document).ready(function(){
$.fn.checkNet();
});
</script>
If you put the checknet.css and checknet-1.0.min.js files in different locations than /css/ and /js/, you will need to change the references to where you put them. Also, if you already have a $(document).ready() listener on your page, you can put the $.fn.checkNet(); line in there rather than adding another $(document).ready().
This all assumes that you already have jQuery included on your page. If you don’t, you can get instructions for using it from http://jquery.com/
Optional Parameters
When you call the $.fn.checkNet() function, you have the option of adding parameters which specify the interval between checking the connection (in seconds) and the URL which is used to check the connection. The default is to check the current page URL at an interval of 5 seconds. You can read about these settings under “How it works” below. To specify an interval of 10 seconds, you would call the function like this:
$.fn.checkNet(10);
To specify you want to use google.com to check for an internet connection at an interval of 10 seconds, you would call the function like this:
$.fn.checkNet(10, ‘http://google.com’);
It’s also worth noting that the warning message has the id ‘netWarn’, and that disabled submit buttons have the temporary class ‘tempDisabled’ while there is no connection detected – you can target these with your own CSS if you wish to customise them further (like, say, giving disabled buttons a red border or something?)
How it works
The plugin works by doing an AJAX request to a URL at set intervals to test the connection. If the URL is unavailable, the plugin assumes there is no internet connection available and adds the error message and disables inputs with type=”submit”. If the URL is available, it removes any error message and removes the disabled attribute from submit buttons. When looking into detecting internet connection with JavaScript, I discovered that many people have different opinions on the best way to do this effectively. JavaScript has the navigator.onLine property in more recent browsers, but I decided that testing a URL was a better option and would allow for more flexability and give more consistent accurate results. The URL to test has been set by default to the current page href (because the user has already loaded it once, giving us a point of reference) despite my first instinct to use google.com or bbc.co.uk (because those domains are not available in some countries).
Future improvements
Although I’m hoping to get feedback from users on how to improve the plugin, the two main advancements I’m planning are:
1. Making the online checking more accurate by using multiple URLs,
2. Detecting inactivity at the user’s end, and suspending checking when the user is inactive (to save bandwidth and prevent uneccesary http requests.)
3. I also plan to make this into an Extension for Google Chrome.
Permission to use
You can use the plugin for personal or commercial purposes as long as you give a link to tomriley.net or 3rdwavemedia.com in your code/comments. I’d love to hear from anyone who’s using this, whether you have suggestions on how to improve it or just want to give some feedback.
Recent Comments