[ Team LiB ] Previous Section Next Section

Hack 30 Add Pop-up Amazon Reviews to Your Web Site

figs/moderate.giffigs/hack30.gif

With a few lines of JavaScript, you can add Amazon customer reviews to your web site.

The Amazon pop-up review service is a simple yet powerful way to integrate your site with Amazon. It allows anyone with a web site to generate dynamic customer reviews without needing to know anything about Amazon's API (see Chapter 6). An independent developer has provided this service to anyone who would like to use it.

All you need to set up the service on your site is a product's ASIN [Hack #1]. When a visitor clicks the pop-up review link, the application receives the ASIN, connects to Amazon's API, downloads the specific XML data, and parses through it to build the product page with reviews. The data is then displayed on a stylized web page in a new browser window.

30.1 The Code

Displaying the generated code in a pop-up window takes just two easy steps. First, create a new HTML page and paste the following JavaScript between the <head></head> tags. This code creates a function called AmazonLookup that will control the state of the pop-up window and initiate a link to the remote server.

<script type="text/javascript">
    function AmazonLookUp(ASIN) {

      // Set pop-up window properties
      var winoptions = 'toolbar=no,';
      winoptions += 'menubar=no,';
      winoptions += 'location=no,';
      winoptions += 'scrollbars=yes,';
      winoptions += 'resizable=yes,';
      winoptions += 'statusbar=yes,';
      winoptions += 'width=470,';
      winoptions += 'height=500';

//Set the remote service URL, including the ASIN
var URL = "http://www.explodingfist.com/quickreview/index.php?ASIN="+ASIN

//Open a new window with the remote URL
OpenWin = this.open(URL, "Amazon", winoptions);
    }
</script>

30.2 Running the Hack

To open the pop-up window and establish a connection with the service, you simply invoke the JavaScript function you created above. For example, to generate customer reviews for The Matrix on DVD you would insert the following link anywhere in the body of the page. (Note the ASIN inside the single quotes; we are passing it to the service as a parameter.)

<a href="javascript:AmazonLookUp('B00000K19E')">The Matrix on DVD</a>

It's that simple! As you can see in Figure 3-2, the link will open a new browser window where you can read customer reviews, see product information, and even add The Matrix to your Amazon shopping cart.

Figure 3-2. Amazon pop-up review
figs/amzh_0302.gif

For further information on this hack, to download the code, or to see a working demo, visit http://www.explodingfist.com/amazonhack/.

—Reid Philpot

    [ Team LiB ] Previous Section Next Section