views:

37

answers:

1

I'm trying to build a music search using Amazon mp3 widgets to play each result. Instead of building a page full of these widgets, I'd like the user to click the album art and then the widget would load.

The code that I have looks like this:

function loadPlayer(asin) {
var amzn_wdgt={widget:'MP3Clips'};
amzn_wdgt.tag='widgetsamazon-20';
amzn_wdgt.widgetType='ASINList';
amzn_wdgt.ASIN=asin;
amzn_wdgt.title='What I\'ve been listening to lately...';
amzn_wdgt.width='250';
amzn_wdgt.height='250';
amzn_wdgt.shuffleTracks='True';
amzn_wdgt.marketPlace='US';
}

In their documentation (https://widgets.amazon.com/Widget-Source/), they have a script to load the widget:

<script type='text/javascript' src='http://wms.assoc-amazon.com/20070822/US/js/swfobject_1_5.js'&gt;&lt;/script&gt;

The issue that I'm running into is that I can't seem to find a way to load that script dynamically. The script loads the widget into the element that contains the code, so I don't knw how to do this with jQuery or javascript. Any help would be appreciated.

+1  A: 

Check out jquery's getScript: http://api.jquery.com/jQuery.getScript/

Update To answer your question below, the script expects a global variable, so you'll need something more like:

var amzn_wdgt;
function loadPlayer(asin) {
  amzn_wdgt={widget:'MP3Clips'};
  amzn_wdgt.tag='widgetsamazon-20';
  ...yada yada...
  $.getScript(...)
}
Isaac Cambron
Thanks. The problem with that approach is that the script is referencing the amzn_wdgt variable, but it isn't in the same scope. Any idea how to make that work?
horizens
@horizens - see update. The issue you're seeing isn't caused getScript, it's caused by the way your scoping works.
Isaac Cambron