Loading Content Only When the User is Online
I was messing around with a project that pulled in my Gravatar tonight. The project had the possibility of offline access, and I only wanted the page to poll for the image from Gravatar’s site if the user was online. I used the handy navigator.online call, and was able to load just what I needed, only when the user is connected to a network:
Javascript
// this self invoking function will check if we are online, and load some goodies if we are (function offLizzle() { // store our element in a var var grav = document.getElementById('grav'); // if we are online, load the gravatar, if not set display to none, as we won't use this element if (navigator.onLine) { grav.innerHTML = '<img class="pic" src="http://www.gravatar.com/avatar/c8feb28847d8f335a05a233cbd490536.png" alt="Photo of Rob Stinogle" width="40" height="40" />'; } else { grav.style.display = 'none'; } })();
Leave a Reply