Detecting if you are online with jquery

I spent a day trying to do something that should have been simple, trying to detect if I’m still connected to the webserver.

I knew I would have to use jQuery, to keep my life simple, but I didn’t know where to start.
After spending way too long investigating, I finally found my answer.. $.get().

I tried using the jQuery plugin for detecting, but it was way to primitive and restricting. So I decided to bypass that all together.

I also tried $(object).load(ulr), but it tries to parse.

By using “get”, I can have a 1 char txt file on the server. If I hit the file, I am connected. If I “fail” to hit the file, then I have either called the wrong URL, or I am disconnected. Usually the later, unless I am debugging.

Also to prevent browser caching, use a URL with some sort of incremental value on the end. I’m using “live.txt?{timestamp}”.

Have fun scripting!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.