rotateBanner.js
Summary
This program insert a random image using jquery library
The configuration of the images to be randomly display are in the banner.json file.
This file can contain either images (jpg, png) or flash.
- Images can have the attributes link and onclick. The src attribute is mandatory.
the onclick attribute aims to get statistics on the click on a image
- Flash can have the attribute width (default : 728) and height (defaut : 90). The attribute src is mandatory.
here is a example of the content of the banner.json file
[
{"src":"http://www.numerikids.com/alpha/wp-content/uploads/2009/06/mario_bros.jpg",
"link":"http://www.google.fr",
"onclick":"$.get(\"http:\/\/172.16.31.180\/rotateBanner\/vide.gif\");",
"options":"target=\"_blank\" style="\"border:purple dotted 3px;\""
},
{"src":"http://monophonik.com/wp-content/uploads/2009/06/mario-wii.jpg",
"link":"http://www.bing.com",
"onclick":"$.get(\"http:\/\/172.16.31.180\/rotateBanner\/vide.gif\");"
},
{"src":"http://www.nintendolesite.com/images/news/news_Nouveau_Mario_en_approche_titre.jpg",
"link":"http://www.yahoo.fr",
"onclick":"$.get(\"http:\/\/172.16.31.180\/rotateBanner\/vide.gif\");"
},
{"src":"http://www.vidalonline.com/vidal-theme/images/pub.swf",
"width":"728",
"height":"90"
}
]
To install in your website, follow these steps :
- configure the multiple banners in the banner.json file
- create a <div id="banner"></div> in your html file
- load jquery in your html file
- load this banner.js in your html file and call rotateBanner()
Version: 1.0
Author: Cédric Levasseur
|
Method Summary
|
static void
|
rotateBanner(<object> target, json_url)
rotateBanner is the main function of this script.
|
$(document).ready(function() {
rotateBanner($("#banner"),"http://www.otherdomain.com/rotateBanner/banner.php?callback=?");
});
function rotateBanner(target, json_url){
if(typeof target === 'undefined' || !target) {
target=$("#banner");
}
if(typeof json_url === 'undefined' || !json_url) {
json_url="banner.json";
}
$.ajax({
url : json_url,
dataType : 'jsonp',
success : function(data){
elem=randomize(data);
insertElement(target,elem);
}
});
}
function randomize(data){
var numLow = 0;
var numHigh = data.length-1;
var adjustedHigh = (parseFloat(numHigh) - parseFloat(numLow)) + 1;
var numRand = Math.floor(Math.random()*adjustedHigh) + parseFloat(numLow);
return data[numRand];
}
function insertElement(target,elem){
ext=elem.src.substring(elem.src.length-3, elem.src.length);
var params=""
var source="";
var options="";
if(ext=='swf'){
if(elem.width){
params=params+'width="'+elem.width+'" ';
}else{
params=params+'width="728" ';
}
if(elem.height){
params=params+'height="'+elem.height+'" '
}else{
params=params+'height="90" '
}
if(elem.options && elem.options.indexOf('=',0)>1 ){
options=elem.options;
}
source='<object '+params+' data="'+elem.src+'" type="application/x-shockwave-flash" '+options+'><param value="'+elem.src+'" name="movie"><param value="true" name="loop"></object>'
}else{
var source='<img src="'+elem.src+'" />'
var onclck="";
if(elem.onclick){
onclck=onclck+'onclick='+elem.onclick.replace('/\"/g','\\\"')+' '
}
if(elem.options && elem.options.indexOf('=',0)>1 ){
options=elem.options;
}
if(elem.link){
source='<a href="'+elem.link+'" '+onclck+' '+options+' >'+source+'</a>';
}
}
target.append(source);
}
Documentation generated by
JSDoc on Fri Nov 19 19:39:02 2010