Here’s a very handy script which will scan through a directory of images, form an array of the contents of that directory and then return the array in JSON format to the client-side where the images will be preloaded into the user’s cache.
There are two components to this: the PHP script and some simple JavaScript:
Recently I’ve been non-stop with JSONP, I love and hate it! (to find out why, see my last post!)
Anyway, David Walsh recently posted a new How-to tip on his site: "Create a TinyURL with PHP" – His tutorial taps into the TinyURL API to retrieve the shortened URL which you can then display on a page.
For those that don’t want to get their hands dirty with PHP there is a very simple way to do it with just JavaScript, Ajax is not needed!
To Mr. Joe Average web developer JSON may just seem like yet another pointless buzzword but to the enquiring mind it is so much more!
For me, the opportunities JSON presents are much more appreciated when looking at JSONP, a slight variation which stands for ‘JavaScript Object Notation with Padding‘. They both essentially refer to the same thing but just to clear, there is a difference:
// JSON:
{
'name' : 'john',
'age' : 23,
'hobbies' : ['football','cooking','rock climbing'],
'temper' : 'moody'
}
// JSONP:
newPerson({
'name' : 'john',
'age' : 23,
'hobbies' : ['football','cooking','rock climbing'],
'temper' : 'moody'
});
As you can see, the only difference is that JSONP format requires some type of callback function with the object passed as a parameter whereas regular JSON is just a regular inline JavaScript object (the theory behind JSONP is that the callback name is specified by the requester (within the query string), not by the party which receives the request).