Click here to Skip to main content
15,894,646 members
Articles / Web Development / ASP.NET

Rating Demystified: Ajax Way, Amazon Way*

Rate me:
Please Sign up or sign in to vote.
4.73/5 (77 votes)
16 Aug 2008CPOL3 min read 180.5K   1.2K   136  
Ever wondered, how the amazon rating system works with multiple items in the same page, here is a simple article to describe the basic bare bones needed to create an asynchronous rating module using ASP.NET , SQL Server and ..... AJAX
	var starImages = new Array("images/0star.gif",
	"images/1star.gif",
	"images/2star.gif",
	"images/3star.gif",
	"images/4star.gif",
	"images/5star.gif");
	var nullStarMessage = "images/0.gif"
	var starMap = new Array('0,0,22,20',
	'23,0,36,20',
	'37,0,50,20',
	'51,0,64,20',
	'65,0,78,20',
	'79,0,101,20');
	var starMessages = new Array("images/0.gif",
	"images/1.gif",
	"images/2.gif",
	"images/3.gif",
	"images/4.gif",
	"images/5.gif",
	"images/saved.gif");

	var savedRatings = new Array();
	var changedRatings = new Array();
	var starTwinkler = new Array();
	var msgTwinkler = new Array();
	var isRatingsBarChanged = false;
	var delayTime = 500;
	var allImages = new Array();
function preloadImages(){
for (i=0; i < preloadImages.length ;i++){
	allImages[i] = new Image();
	allImages[i].src = preloadImages.arguments[i];
	}
}
preloadImages(starImages);
preloadImages(starMessages);

function SaveRating(id, ratingType, ratingValue){

	var submitURL = ''
	+ 'rated.aspx?'
	+ 'id=' + id
	+ '&Rating=' + ratingValue ;

	isRatingsBarChanged = true;
	window.location.href = submitURL;
}
function SwapStars(id, rating){
	if (rating == undefined){
	rating = savedRatings[id];
	}
	document.images["stars." + id].src = starImages[rating];
}
function SwapStarMsg(id, rating){
	if (rating == undefined){
	if ( changedRatings[id] ) {
	document.images["messages." + id].src = starMessages[6];
	} else {
	document.images["messages." + id].src = nullStarMessage;
	}
	} else {
	document.images["messages." + id].src = starMessages[rating];
	}
}
function SaveStars(id, rating){

if ((rating==1)||(rating==2)||(rating==3)||(rating==4)||(rating==5))
	{
	savedRatings[id] = rating;
	changedRatings[id] = 1;
	SaveRating(id, 'onetofive', rating);
	SwapStarMsg(id, 6);	}
	else
	{
	alert("Rating Value out of the bound, Values can only be 1/2/3/4/5. Current rating value: " + rating);
	}
}

function StarMouseOver(id, rating){
	if (starTwinkler[id] != 0){
	window.clearTimeout(starTwinkler[id]);
	starTwinkler[id] = 0;
}
if (msgTwinkler[id] != 0){
	window.clearTimeout(msgTwinkler[id]);
	msgTwinkler[id] = 0;
	}
	SwapStars(id, rating);
	SwapStarMsg(id, rating); 
}
function StarMouseOut(id){
	starTwinkler[id] = window.setTimeout("SwapStars('"+id+"')", delayTime);
	msgTwinkler[id] = window.setTimeout("SwapStarMsg('"+id+"')", delayTime);
}
function DisplayStars (id, rating){
	var starID = "stars." + id;
	starTwinkler[id] = 0;
	msgTwinkler[id] = 0;
	document.write("<map name='starmap" + id +"'>");
	var i = 0;
	for (i = 1; i < 6; i++) {
	document.write("<area shape=rect " + 
	"coords='" + starMap[i] + "' " +
	"onMouseOver=\"StarMouseOver('" + id + "'," + i + ");\" " +
	"onMouseOut=\"StarMouseOut('" + id + "');\" " +
	"onClick=\"SaveStars('" + id + "'," + i + ");" +
	"\" >");
	}
	document.write("</map>");
	document.write("<img vspace=2 title = 'Rate Picture' src='" + starImages[rating] + "'");
	document.write(" border=0 usemap='#starmap" + id);
	document.write("' id='" + starID + "'>");
}
function DisplayMsg (id, rating){
	var msgID = "messages." + id;
	if ( rating == undefined ) {
	document.write("<img vspace=2 height=11 src='" + nullStarMessage + "'");
	}
	else {
	document.write("<img vspace=2 height=11 src='" + starMessages[rating] + "'"); 
	}
	document.write("' id='" + msgID + "'>");
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Teamcal AI
United States United States

Comments and Discussions