Get songs from a playlist URL

Given a playlist's URL, we will create a Playlist object and display the songs in that playlist. Even though we're using the fromURI method, we can use URLs here too (Facebook's API returns URLs, not URIs).

The Javascript

/* Instantiate the global sp object; include models */
var sp = getSpotifyApi(1);
var models = sp.require("sp://import/scripts/api/models");

var playlist_url = 'http://open.spotify.com/user/jkeck99/playlist/21iMSq0jzTie7m7v4UcMlV';

var pl = models.Playlist.fromURI(playlist_url, function(playlist) {
    for(i=0;i<playlist.tracks.length;i++){
        var link = '
  • ' + playlist.tracks[i].name + '
  • '; $('#playlist').append(link); } });

    The HTML

    <ul id="playlist"></ul>

    Live Example