<<Back
Javascript Sample
This sample renders the recommendations using javascript. The recommendations are returned from Avail BEM with meta data, i.e. the data needed to render the recommendations.
<html> <head> <script type="text/javascript" charset="UTF-8" src="http://service.avail.net/2009-02-13/dynamic/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/emark.js"></script> <script type="text/javascript"> //Function that will be executed on body onload. function populatePredictions(){ //Create emark object with your customer id as argument var emark = new Emark(); //Request recommendations for product myProductId with template myTemplate //Meta data to be returned is product id, title, price and image url var prodRecs = emark.getRecommendations("myTemplate", ["ProductId:myProductId"], null, ["ProductId", "Title", "Price", "Image"]); //The code within the commit brackets will be executed when the recommendations has been returned to this page emark.commit(function() { var sHTML = ""; //Go through all recommendations for (i=0;i<prodRecs.values.length;i++){ //Go through the data for the current recommendation for (j=0;j<prodRecs.values[i].length;j++){ //Generate HTML for recommendation sHTML += prodRecs.values[i][j] + ", "; } sHTML += "<br />"; } //Place the generated HTML on page document.getElementById("prodRecsDisplay").innerHTML = sHTML; }); } </script> </head> <body onload="populatePredictions();"> <b>Recommendations</b> <div id="prodRecsDisplay"></div> </body> </html>