how to check if an internet connection exists ON PAGE LOAD??

I want to use (the now famous) kirupa code for checking if an internet connection exists using javascript… but I can’t for the life of me figure out how to make it work with an “onload” instead of on “mousedown,” It’s probably a no brainer for others on this forum but for me (am not too bright) it’s a headbanger! To reiterate, how does one check if an internet connection exists on page load??

Further (for extra karma kredits ) how can I use the darn thing behind a submit button?
Darnit!

PS - It works flawlessly for me ‘when clicking’ which is more than can be said for all the code I’ve scoured the net for, thus far… hats off to you kirupa!

Have you tried replacing the mousedown here:

var link = document.querySelector("#mainContainer a");
link.addEventListener("mousedown", checkConnection, false);

…with Document: DOMContentLoaded event - Web APIs | MDN ? Or just running checkConnection immediately in a bare script tag?

Submit buttons are usually in a <form />, so they dispatch a submit event, right? Then inside of that you’d preventDefault(), probably return false, and submit the actual form once checkConnection runs and figures out its answer?

1 Like

Will try it out… and forgive me if I don’t reply for a week or so… am in a remote part of the South Pacific (where the net is almost non existent) and don’t get a chance to log on as much as I’d like to. Thanks for your help! Will be back in touch.

Ok… am still on… checked the link you gave… and am still clueless
(did I mention that I’m not too bright?).

Firstly, yes, I tried running ‘checkConnection’ immediately in a bare script tag?
body onload=“javascript:doesConnectionExist()”
But nothing… also tried through ““a refresh”” …but nothing.

AM A COMPLETE NOVICE (which I guess is short for fool).

At the MozDev link you gave under “Related Events” I found info on the Load Event
but once again was clueless; couldn’t make heads or tails out of it.

LET ME NOT EVEN TRY THE SUBMIT BUTTON ISSUE. DARNIT.

I think you thought I was smarter than I actually am. :wink: Could you be more explicit; specific.
I’d appreciate any help you can give.

Of course. Check out this page: http://reclipse.net/kirupa/testIC.html

If you view the source code in your browser, it’s very short. I have a bare script tag that tries to load another image off of my server; it’s 95% me copying @kirupa’s tutorial code, with a bit of a different URL since it’s my own site.

There’s a bit of an interesting aspect of your problem: say you don’t have internet at all, how are you going to load kirupa.com, which is only available on the internet? I’m curious, because I fear this might be causing you some trouble given your internet situation.

THAT’S IT!

Will now try ‘dubbing in’ an if/else so it can go to an offline page (that’ll exist within my simple little app) or if connection is live the app will show an actual site.

As for my ““internet situation”” …when I’m on, am on at 100% and when am not, well, am not :blush: AND in case you’re curious (or for that matter, anyone else reading this) I am doing relief work in a rather rural part of the Philippines and literally have to come down to the city to get a good net connection. Not like back home (NYC) where everyone’s connected, all the time.

BTW… may I tap your brain further with regards to finding an appropriate way to handle a submit button? I need a button that checks the connection (in the background) before sending a form and returns an offline page if the connection craps-out?
Am looking for an elegant solution but I have no real budget. I can offer my design services as barter… or sweat equity… or my first born… :wink: and if you’re just too busy, trust me, I understand. A guy like you probably get’s paid more than what I can offer at this stage in the development of my experimental app!

Just out of curiosity - wouldn’t it be better to check for the internet connection before the form is filled out in the first place? For example, the user clicks a button, and if the internet connection exists, they get taken to a page containing the form. If there is no internet connection, they see a message telling them there is no internet connection. (Taking them to a new page when there is no internet connection kinda won’t work…since you’ll need an internet connection to load the “error” page in the first place haha.

:stuck_out_tongue: