Ajax and Ajax Frameworks

I have been a developer for nearly 20 years and a web developer for about 10 years. Over the years, I have considered creating a blog to share the things I have learned (and am learning) about web design and development, but I never seemed to have the time. When I began learning about web development using Ajax and Ajax Frameworks, I decided to take the time to create this blog.

My intention for this blog is to focus primarily on Ajax Frameworks, but since web development requires knowledge of many technologies, I will occasionally write about things such as CSS, Javascript, PHP, MySQL, Flash, etc.

Lately, I have been using JQuery as my primary Ajax tool. Although I have used other Ajax Frameworks in the past (Dojo Toolkit, Yahoo! User Interface Library, Scriptaculous/Prototype), JQuery has thus far been the easiest to learn. In my first few posts I will discuss some of the JQuery Plugins that I have created. Some of these include: a plugin for Google Maps (jquery.imGoogleMaps), Form validation and submission  (jquery.imValidateForm), Page Populater (jquery.imPagePopulate), and a plugin to create lists (jquery.imList).

While I am still learning about some of the other Ajax Frameworks, I hope that what I have learned will be helpful to others. Just remember, “When you can pull the pebbles from my hand…”

JQuery Plugin: imBannerRotater

Monday, October 26th, 2009

I created simple JQuery plugin that rotates images on a page. Actually, there are two modes. In the default mode, the plugin will display a randomly selected image. In the other mode, the plugin will rotate images on a page (fading in/out). In this mode, you can select the speed of the rotation. The image data can be retrieved ajaxally (json or a comma separated list). If the data is Json, a url can be added for each image.

Plugin Options

Setting the imBannerRotater is simple:

mode: ‘random’ or ‘rotate’. Tells the plugin whether to display images randomly or to rotate the images on the page (fade in/out). The default mode is ‘random’.

image_url: This option really should be named data_url. This the location of the file list that will be retrieved ajaxally.

return_type: ‘list’ or ‘json’. This option tells the plugin whether the data retrieved via the image_url is a comma separated list or a json object. The defualt is ‘list’.

images: This option can be used instead of the image_url option (return_type = list). Just enter a comma separated list of image names.

base_path: Your file list can contain the full path of each image. If your list contains the file name only, you can use the base_path option to prefix all of the image in the list.

data_map: This option must be used if your return_type = json. This tells the plugin where to find the image name in the json object. You can also specify a url for each image and the url_target (if the mode is set to random). The default for the url_target is ‘_blank’. The format for the data_map:

?View Code JAVASCRIPT
{image_name: 'the image name field', url_name: 'the url for the image', url_target: '_blank'}
 
//if no url
{image_name: 'the image name field'}

speed: This option is used with the mode = rotate. This is the speed in which each image fades in/out. The default is 1500. It will accept any value that is accepted by JQuery’s fadein effect.

Setting Up Plugin

To setup the plugin, first you need to load jquery and the imBannerRotater script:

<script src="js/jquery/jquery-1.3.2.min.js" type="text/javascript"><!--mce:0--></script>
<script src="js/jquery/jquery.imBannerRotater-0.5.js" type="text/javascript"><!--mce:1--></script>

Now we instantiate the plugin:

?View Code JAVASCRIPT
$(document).ready(function(){
	$("#cast").imBannerRotater({
		image_url: 'random.php',
		base_path: 'images/'
	});
});

In the example above, the ‘cast’ id selector is the container for the images. The base_path option tells the plugin to look for the images in the images directory. No mode is set, so the default is random. The return_type is not set. The default is list, so the plugin will be looking for a comma separated file list. The random.php will look similar to:

$(document).ready(function(){
	$("#cast").imBannerRotater({
	    mode: 'rotate',
	    return_type: 'json',
	    image_url: 'rotate.php',
	    base_path: 'images/',
	    data_map: {image_name: 'name'}
	});
});

The rotate.php file contains a json object:

echo '[{name: "joneil.jpg"},{name: "scarter.jpg"},{name: "djackson.jpg"},
          {name: "tealc.jpg"},{name: "cmitchell.jpg"},{name: "vala.jpg"}]';

The ‘name’ attribute is linked to the data_map.image_name option of the plugin.
To view a demo of the imBannerRotater plugin, go to: http://grasshopperpebbles.com/demos/.

The plugin can be downloaded from Google Code or JQuery Plugins.

Related posts

10 Responses to “JQuery Plugin: imBannerRotater”

  • imBannerRotater – jQuery Plugin | jQuery Wisdom Says:


    [...] ajaxally (json or a comma separated list). If the data is Json, a url can be added for each image. Web Site Demo Download Share and [...]
  • goldlilys Says:


    With the jQuery 1.4 update, your banner rotator doesn’t work anymore. Can you fix the “parsererror” problem that’s currently occurring before I can start using the newest version of jQuery?
  • admin Says:


    I’ll look into it. Since I will be updating the plugin, is there any functionality that you would like to be added?
  • admin Says:


    I just tested the plugin using JQuery 1.4. The plugin does not have to be updated. Place quotes around the name, not just the value.

    Demo example:
    {name: “joneil.jpg”, url: “http://www.imdb.com/character/ch0005968/”}

    JQuery 1.4
    {”name”: “joneil.jpg”, “url”: “http://www.imdb.com/character/ch0005968/”}

  • JQuery 1.4, YUI: JSON ParseError – Quotes Required | GrasshopperPebbles.com Says:


    [...] just sent me a message asking me to update my imBannerRotater plugin because they were receiving a parseerrormessage with JQuery [...]
  • goldlilys Says:


    Oh, thanks for that. Wow what a simple fix, but it gave me a headache trying to figure out which script was causing a problem on my site. When I figured it was because jQuery was updated, then I had to comment out script one by one and yours was the only one that made that parseerror. Anyways, thanks for fixing this problem quickly.
  • admin Says:


    Is there anything that you would like to have added to the plugin?
  • Michael Says:


    Can’t seem to get this working correctly. Got it all in, it comes up on screen but gets missing image symbol as the image filenames never get added to the url for the image… any ideas?

    I think it has something to do with the rotate.php, the page seems to cancel loading when it gets to that.

    jquery 1.4 and i’ve added the extra ” ” needed.

  • admin Says:


    I’m not sure. Use Firebug to see what gets loaded. If it looks okay in Firebug, send me a snippet of the Json object and I will take a look at it.
  • Tracy Says:


    Thanks for your plugin!
    This works fine for me in normal browsers, I’m still using jquery 1.3 …
    Android works fine.

    iphone doesn’t work for me, but your demo works. The Safari console points to line 71, but I see that’s just where there error message is generated.
    Maybe I’ll upgrade to 1.4.

CommentLuv Enabled