Category: Code

  • Partying like it’s 1999

    A little under twelve months ago I wrote a post on a JavaScript base file I’d set up:

    I could have used an existing frame work, such as the increasingly popular jQuery; I decided to use my own, which contains only the very basics, rather than have a larger file containing rarely used functions

    source (emphasis added for this post)

    Within a couple of months of writing that, I was a convert to jQuery. There was no single reason for my conversion, from memory some of the reasons where:

    • the selector engine,
    • plugin availability,
    • documentation — both official and unofficial tutorials, and,
    • lazyness

    It’s the last one that probably had the biggest influence, I’m too lazy to reinvent the wheel, and without using a framework that’s what I was going to be doing. Frequently. Examples in blogs I read, weather it be group blogs, such as A List Apart, or one person shows, such as CSS-Tricks were all beginning to use jQuery. If I were to stick to my guns and not use a framework, I’d be doing a lot of rewriting.

    As for the functions that are rarely used? It’s really only the Ajax group of functions that I’ve never used, for the simple reason that I don’t like Ajax. In many of the situations it’s used, it is often fanciness without necessary function. Then again, ask me about Ajax in a couple of months’ time.

  • Including WordPress’s comment-reply.js (the right way)

    Since threaded comments were enabled in WordPress 2.7, most themes include the following line in header.php

    <?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?>

    This code checks if the visitor is browsing either a page or a post and adds the JavaScript required for threaded comments if they are.

    I prefer a slight variation

    <?php
    if ( is_singular() && comments_open() && get_option('thread_comments') )
      wp_enqueue_script( 'comment-reply' );
    ?>

    My variation checks if the visitor is browsing either a page or a post, if comments are open for the entry, and finally, if threaded comments are enabled. If all of these conditions are met, the JavaScript required for threaded comments is added.

    If you run your wp_enqueue_script calls in functions.php, as I do, this is the code to use:

    <?php
    function theme_queue_js(){
    if ( (!is_admin()) && is_singular() && comments_open() && get_option('thread_comments') )
      wp_enqueue_script( 'comment-reply' );
    }
    add_action('wp_print_scripts', 'theme_queue_js');
    ?>

    The call is added to the wp_print_scripts action as is_singular and comments_open are unknown during the init action.

     

    Note: I’ve written a plugin to make the comment-reply JavaScript unobtrusive, it’s call Rapid Comment Reply.

  • Caching on the Google AJAX Libraries API

    Using the Google AJAX Libraries API, there are several options for specifying the version numbers of the library you wish to use, for example the following URLs all point to the latest version – at the time of writing – of jQuery.

    1. http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js,
    2. http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js, and,
    3. http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js

    The first uses the latest release in the version 1 tree, the second the latest in the version 1.3 tree, and the third the release 1.3.0 exactly. What I found interesting – and discovered accidently – is the browser caching times for each of these urls; the first two are cached for an hour, the third for twelve months.

    The vastly different caching times make perfect sense, in the first two cases, the developer is expecting an upgrade to the latest version, and doesn’t want to wait up to twelve months for it; in the third case the developer doesn’t expect an upgrade so an extended caching period has no effect.

    To take full advantage of the Google servers, as described in a recent article by Dave Ward, the caching times suggest it’s best to specify the full version of the library you wish to use.

    google.load Method – Update Jan 24, 2009

    (more…)

  • JavaScript Equal Height Columns

    The desire for equal height columns in a CSS layout is nothing new; there are many solutions available, some use JavaScript, others use CSS with negative margins, and then, there’s the faux columns method using background images. All of these methods have their place as perfectly valid solutions, and, depending on the situation, may be the best solution available. (more…)

  • Base JavaScript File

    Upcoming posts on JavaScript will include references to functions in my base JavaScript file; rather than explain these functions each time, they’ll be detailed in this post for future reference.

    There are many pre-written JavaScript examples and functions available, so it’s important to note that all of these functions have been written by others, some include slight changes; all moved to the namespace PWCC, as you’ll see in the source code.

    (more…)

  • Don’t start with a reset.css

    Earlier this year, Jonathan Snook wrote an article on why he doesn’t use a reset.css in which he referred to Eric Meyer’s reset, a short time later, Eric Meyer responded with an article of his own. Unlike many discussions on the web, it wasn’t a mudslinging match, but a sincere discussion of the tools available to web developers.

    My initial thoughts were that a reset style sheet, combined with a base style sheet, was a helpful place to start any web project as it reduces the incidence of unexpected results. I thought it was important for the developer to have her own reset and base styles; blindly adopting someone else’s reset and base style sheets will just lead to a different set of surprises.

    (more…)