Tag Archives: test

jQuery: How to Test if a div is Visible Programming 17 JUL 2013

jquery-logoThe wonderful jQuery library actually makes it pretty easy for us to test whether or not a div (or pretty much any other element for that matter) is currently visible, by providing us with a ‘:visible’ selector.

Basically elements are considered visible by jQuery if they consume space in the document, meaning that visible elements have a width or height that is greater than zero. Combining this with the .is filter forms the basis for our test.

In practice:

//assuming a div with id "div1"
$('#div1').show();
alert ($('#div1').is(':visible')); //true
$('#div1').hide();
alert ($('#div1').is(':visible')); //false

Nifty. As always, play with around with it yourself in the awesome JSFiddle online JavaScript playground.

How to test if a Crontab is working in Ubuntu CodeUnit 15 JUN 2012

Editing your cron jobs with the crontab -e command will ensure that your crontab is at least syntactically correct, by not installing it unless it is a valid cron file.

Outside of that though, it falls to you to test that your jobs are in fact running correctly. This can be achieved by piping the cron command output to a file that you control for each one of your jobs.

In practice:

00 01 * * * bash mycommand1.sh > /tmp/mycronjob.log 
00 02 * * * bash mycommand2.sh > /tmp/mycronjob.log

And in the same vein, you can check that the crontab as a whole is being executed by creating a job right at the end of the cron list that runs every minute and pipes out its output to a file that you control:

*/1 * * * * echo "Success! $(date)" >> /tmp/cronwatch.log

Note that > means rewrite the target file every time, whilst >> means append to the end of the file (and create if not exist).

Nifty

Related Link: https://help.ubuntu.com/community/CronHowto

PHP: Test if an Image URL is Online CodeUnit 25 MAY 2011

Here is a quick and dirty simple function to check whether or not an image URL is valid, in other words the image is online, by running a quick get_headers check combined with some REGEX wizardry.

The function returns a simple true if the image/URL is active and false if it is not (i.e. the server returns a 404).

function url_exists($url) {
    $hdrs = @get_headers($url);
    return is_array($hdrs) ? preg_match('/^HTTP\/\d+\.\d+\s+2\d\d\s+.*$/',$hdrs[0]) : false;
}

Nifty.

How to Check if Your Javascript Function Exists CodeUnit 28 MAR 2010

So I updated a panel generating script to adhere to some campaign rules, meaning that certain graphing functions were no longer being generated by the PHP script. Great, as easy as that I thought to myself as I patted myself on the back and uploaded the updated code, only to recoil in horror when the site stopped working correctly all of a sudden.

Debugging the errors, I discovered that the thing was falling over whenever a previously untouched bit of code was trying to reference my Javascript scripts – some of which were obviously no longer in existence!

So the question I was then left with is how to check for the existence of a function before actually calling that function, the solution to which turns out to be fairly simple.

All you need to do is this:

if(typeof yourFunctionName == 'function') {
yourFunctionName();
} 

where obviously yourFunctionName is the name of the function you wish to test.

Works like a charm and after implementing this on all of my function calls, no more broken site, meaning happy me and thus a well deserved pat on the back.

Nice.