Home & blog  /  Tag: image  /

My article in .NET / update to Dropdown

posted: 03 Sep '11 18:54 tags: REGEXP, .NET, article, me, dropdown, image

Remember I blogged a few months back that I'd been approached to write for .NET? Well my article is in the latest (September) issue, so perhaps you've seen it.

It's a beginner's guide to tackling regular expressions - one of the most avoided and feared areas of programming for elementary-intermediate-level developers.

There's an annoying typo; the * modifier, of course, matches zero or more characters, not one or more. D'oh. I had meant the + modifier, which does match one or more characters.

In other news, following a recent update to my jQuery Dropdown script (which allowed it to be used as a form field, not just as a navigational tool), there's been another update.

In the comments on the script page, Michael Eaton requested that it be possible to include icons in the 'options'. This is now possible!

Head over here to download, get usage info or view a demo.

post a comment

How to: detect internet connection in Javascript

posted: 18 Aug '10 13:54 tags: how to, connection, offline, image

I am working on a project currently that involves content being delivered for offline use, with no certainy an internet connection will be present.

There's a feedback form, and since forms need a PHP (or other) handler to receive and process the post data, the form was to be available only if a connection was present and the handler could be loaded.

Which led me innevitably to the question: how to check for an internet connection in JS?

AJAX springs to mind, but there's the Same Origin Policy just waiting to trip you up on that one. Yes, you could have your AJAX request call a PHP script which, through something like cURL(), can check for a remote connection, but not if you don't have PHP in the environment.

Eventually I opted on a very simple technqiue, but which appears safe: run a native, DOM-zero onload event on a remote image.

The obvious - and only - risk here is that if the site hosting the image was down, your JS script would think there was no internet connection.

So load an image on the site whose remote assets you need to call (in my case, the PHP handler). If that's down, then it amounts to the same thing as there being no internet connection, as either way your handler won't be loaded.

Here's the code involved:

1var img = document.createElement('img');

2img.src = "http://www.somesite.com/myimage.jpg";

3var connection = false;

4img.onload = function() { connection = true; }

You can subsequently check for a connection by querying the boolean connection.

That's fine if your code is in a function and invoked by an event some time after load, but if it's JS that's set to run automatically, you'll need to delay it a few seconds at least to make sure the image load request has had chance to run.

In which case, put your code in a setTimeout() callback, e.g.

1setTimeout(function() {

2     /* your code here

3     ...

4     etc */

5}, 3000); //your code waits 3 seconds then runs

3 comments | post new