Lily Santa Hat
Leona Dunkin’s Funeral
Introduction To jQuery (Part 3): Waiting For The Page To Load & Anonymous Functions
By James Bruce, MakeUseOf – March 03, 2013 at 01:31PM
jQuery is arguably an essential skill for the modern web developer, and in this short mini-series I hope to give you the knowledge to start making use of it in your own web projects. In the first part of our jQuery tutorial, we looked at some language fundamentals, and how to use selectors; in part 2, we moved on to methods of manipulating the DOM.
In part 3, we’ll tackle the problem of how to delay jQuery until the page has loaded, then I’ll try to explain what anonymous functions are and why you need to know about them.
Delayed Loading: How & Why?
If you’ve been trying out some of the code from part 1 and 2, you may have come across some errors, odd behaviour, or things just plain not working. The most common error I experienced when learning jQuery was that of DOM elements not being found – even though I could plainly see them in the source of the page, jQuery kept telling me it just couldn’t find them! Why is that?
Well, it’s all to do with the order in which things are loaded by the browser. At it’s simplest, if you have a jQuery script running in the browser before the DOM element it’s looking for has actually been created, the script will load first, but not do anything because it can’t find the element, then the DOM element will load later. This is less of a problem if you place all your scripts near the footer, but it can still happen.
The solution is to wrap your scripts in whats called a document ready event. This makes the enclosed code wait until the DOM has been fully loaded (until it’s ready). Using it is simple:
$(document).ready(function(){
//your code to delay goes here
});
There’s an even shorter way of doing this outlined in the jQuery documentation, but I’d strongly suggest you use this way for code readability.
This document ready event is another good example of an anonymous function, so let’s try to understand what this means.
Anonymous Functions
If like me you’ve got some beginner level programming experience under your belt, the idea of anonymous functions – which is core to jQuery and Javascript – might be a little disconcerting. For one, it makes errors due to mismatched braces quite common, which is why I’m going to explain it now. If you’d like a thorough explanation as to why anonymous functions are better than regular named functions on a more technical level, I’d suggest reading this fairly complex blog post.
Until now, you’ve probably only come across named functions. These are functions that have been declared with a name and can therefore be called anywhere else, as many times as you like. Consider this trivial example, which will log a message to the console when the page is loaded.
function doStuffOnPageLoad(){
console.log("doing stuff!");
}
$(document).ready(doStuffOnPageLoad);
This is useful if your function is designed to be re-used, but in this case it’s kind of convoluted since we only really want it to fire once when the page is loaded. Instead, we don’t bother defining a separate function, and just declare it inline as a parameter as and when needed. The previous example would therefore be better re-written as:
$(document).ready(function(){
console.log("doing stuff");
});
You may not see many advantages of this at the moment – it’s only marginally less code in this case – but as your scripts progress in complexity you’ll appreciate not having to jump around trying to find function definitions. Unfortunately, it does make things a little more difficult for beginners – just look at all those braces – so be sure to check the following points if you’re getting errors:
- Correct number of corresponding braces – indenting your code helps.
- Curly vs round braces.
- Statement closing with a semicolon – but not needed after a closing curly brace.

Using a code editor like Sublime Text 2 can really help as it highlights corresponding braces and automatically indents code for you. A dedicated code editor is essential, really.
That’s it for this lesson, but you should get into the habit of enclosing some basic DOM manipulations in document ready event before moving on, and start editing files in a code editor if you aren’t already. Next time, we’ll take a look at events and how they are used to add interactivity to a page – such as make jQuery do something when a button is clicked. Questions or comments always welcome below.
The post Introduction To jQuery (Part 3): Waiting For The Page To Load & Anonymous Functions appeared first on MakeUseOf.
2013-03-04 Remembering John
2013-03-04 Remembering John, a set on Flickr.
2013-01-12 Carrie’s First Ballet Class
2013-01-12 Carrie’s First Ballet Class, a set on Flickr.
2013-01-12 Lily
2013-01-12 Lily, a set on Flickr.
How Power Failures Corrupt Flash SSD Data
By Soulskill, Slashdot – March 01, 2013 at 05:06PM
An anonymous reader writes “Flash SSDs are non-volatile, right? So how could power failures screw with your data? Several ways, according to a ZDNet post that summarizes a paper (PDF) presented at last month’s FAST 13 conference. Researchers from Ohio State and HP Labs researchers tested 15 SSDs using an automated power fault injection testbed and found that 13 lost data. ‘Bit corruption hit 3 devices; 3 had shorn writes; 8 had serializability errors; one device lost 1/3 of its data; and 1 SSD bricked. The low-end hard drive had some unserializable writes, while the high-end drive had no power fault failures. The 2 SSDs that had no failures? Both were MLC 2012 model years with a mid-range ($1.17/GB) price.'”
Read more of this story at Slashdot.
![]()
2013-02-27 Ang at 19 weeks pregnant
2013-02-27 Ang at 19 weeks pregnant, a set on Flickr.
Big Dog Can Now Throw Cinder Blocks, Thereby Making It The Scariest Robot Ever
By John Biggs, TechCrunch – March 01, 2013 at 09:22AM

While I’m sure there’s some scientific reason for demonstrating how the quadrupedal Big Dog can pick up and throw cinder blocks across a workshop (“Ahem, urm, we’re showing how the mass of the brick has little or no direct effect on the quadruped’s center of gravity, allowing it to carry large objects in the field, ahrm.”), I think what we’re seeing here is the first example of a nascent new robotic sport, human tossing.
What’s really going on here are experiments involving using the legs of the robots to propel objects. “This sort of dynamic approach is routinely used by human athletes and is now improving the performance of robots,” writes Big Dog’s creators at Boston Dynamics. If they only knew.
Imagine a field full of Big Dogs and some of the finest convict athletes from the off-world colonies. Zarg fakes to the left but Big Dog lunges and grabs his suit, ripping off the helmet (and head) and throwing it across the end zone. Torgo grabs a cinder block to throw at Master Big Dog but two Minor Big Dogs grab it in mid-air and toss it back, crushing a group of spectators in the stands.
Naturally, the Big Dogs always win.







