I came across this Stack Overflow question today about replacing radio buttons with images, however the answers there didn't sit quite right with me. They were completely reliant on javascript, which was fine according to the OP, but I feel that javascript enhancements like this, especially when they deal with things which require user input, [...]
Javascript default values
Just a small post here to share a bit of benchmarking I just did. In Javascript, the boolean operators (|| and &&) behave a little differently to how many people expect. In PHP, the result of using || or && is always a boolean:
// PHP:
$x = "hello" || false; // bool(true)
$x = false || 0; [...]
Common Mistakes Part 1: SJAX
Here's another common mistake I see on Stack Overflow from time to time. The basic pattern goes like this:
// get the contents from some remote file
$('#myElement').load('lipsum.html');
alert($('#myElement').html());
// wtf, it's still empty?!?
This is something that I struggled with when I started playing with AJAX techniques, too. It's quite understandable how so many people [...]
Common Mistakes Part 0: 83.333% of jQuery
I've decided to start writing a short series of posts highlighting some common mistakes I see in questions and, more importantly, answers on Stack Overflow. I enter this knowing fully well that I am but an amateur who is also learning new things daily. Anyway, the series kicks off with Part Zero, titled 83.333% of [...]
Regex for PHP function declarations
Here's a little snippet of the day for you. A regex to grab the names of functions from a PHP file.
/^\s*(?:(?:public|private|protected|static|abstract|final)\s+)*
function\s+&?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/
Linebreak added for legibility.
If you were wanting to know the functions which exist in a particular class or file for an application or similar, you'd be better off using the PHP Reflection functions which are [...]
Avoiding conflicts in jQuery
A very useful jQuery tip which I've picked up, but also noticed that isn't widely known is to do with the common document.ready function. The old way to do it is like this:
$(document).ready(function() {
});
This method is preferred by some developers, because they see it as being more expressive: you are writing a function to be [...]
Crazy commenting ideas
Continuing with my fascination with the important things in programming languages — that's right, comment control structures — I have another idea!
Something which is a constant source of annoyance when using block comments is that sometimes you already have a block comment inside the larger area you are wishing to comment out:
function foo() {
[...]
jQuery is not optimised how you’d think
I've come into the habit of altering my jQuery queries to hopefully optimise them to run faster. For example, if I wanted to get the html of the first "a" element...
// i'd use this:
$("a:first").html()
// instead of this, which is functionally identical
$("a").html()
It makes sense right? Don't bother getting all the elements, just stop at the first [...]
Unix timestamp confusion
There seems to be quite some confusion when it comes to working with dates and Unix timestamps, especially when there are multiple timezones involved. I thought I would write up a bit of an explanation to help clarify the situation, and show some of the better ways to deal with this in PHP.
First up, some [...]
Pro commenting tip
Here's a little, mostly useless tip. If you need to toggle between two blocks of code (for example, comment out a block of text and replace with debugging values, and then switch back again), instead of doing it like a noob...
// $myArray = $database->getObject();
// $myOtherArray = doSomethingElse();
$myArray = array("foo");
$myOtherArray = array("fu", "bar");
.. use this technique [...]
Recent Entries
- Image radio buttons
- Javascript default values
- Ubuntu, nooooo!
- The great rep recalc of 2010
- A suggestion for Ford
- jQuery 1.4 released
- Common Mistakes Part 1: SJAX
- Anyone have any questions?
- Common Mistakes Part 0: 83.333% of jQuery
- Regex for PHP function declarations
Recent Comments
- Damo in Image radio buttons
- alex in The great rep recalc of 2010
- Fisher in Anyone have any questions?
- fake in Anyone have any questions?
- alex in Anyone have any questions?
- Fisher in Common Mistakes Part 0: 83.333% of …
- alex in Common Mistakes Part 0: 83.333% of …
- Fisher in Perhaps the most epic Stack Overflo…
- alex in Perhaps the most epic Stack Overflo…
- alex in jQuery is not optimised how you'd t…
Categories
Archives
- June 2010
- May 2010
- March 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- June 2009
- May 2009
- April 2009
- March 2009
- January 2009
- December 2008
- September 2008
- July 2008
- June 2008
- April 2008
- March 2008
- December 2007
- November 2007
- September 2007
- August 2007
- July 2007
- May 2007
- April 2007
- March 2007
Pages
Blogroll
- Chris Dot Spadgos
- Fisher Dot Spadgos
- Furious Steals - Highly destructive UT2004 racing mod
- Marshmallow Duel - The 1996 cult classic and delicious fan remakes
- NB Dot Spadgos
- Pospi Dot Spadgos