In this tutorial, we'll show you a smarter, faster way of embedding Phish.net setlists into a website using javascript. For this tutorial, I'll be employing API v3's setlists.latest method.
The simplest way to use the javascript API is detailed in Tutorial 1, but it's not the best way. The problem with the standard method is that it is rendered inline, which means that if there's a delay reaching phish.net, the rest of the page will pause as it waits. When the data is returned, it will reflow and drop the rest of the data in. This is not desirable for a number of reasons, but mostly if you're embedding the setlist near the top of the page, where your content can be needlessly delayed. We're going to fix that with the following script(s). Let's start by pretending we're trying to insert the latest setlist into a div called "setlist."
<div id="setlist"></div>
Just before you close your </body> tag, let's add this:
<script type="text/javascript">
$.getJSON('https://api.phish.net/v3/setlists/latest/?apikey=<APIKEY>&callback=?',
function callback(json) {
$('#setlist').append("<div id=\"pnetsetlist\">");
for(i=0;i<json.response.data.length;i++) {
var n = json.response.data[i];
$('#setlist').append("<h3>"+n.short_date+" <a href=\"http://phish.net/setlists/"+n.permalink+".html\">"+n.venue+"</a></h3><p class='pnetsl'>"+n.setlistdata+"</p>");
if(n.setlistnotes.replace(/^\s+|\s+$/g,"")!='') { ('#setlist').append("<p class='pnetsn' style=\"font-size:10px;\">" + n.setlistnotes + "</p>"); }
}
$('#setlist').append("</div>");
}
);
</script>
<script type="text/javascript">
function pnet(json) {
document.getElementById('setlist').innerHTML +="<div id=\"pnetsetlist\">";
for(i=0;i<json.response.data.length;i++) {
var n = json.response.data[i];
document.getElementById('setlist').innerHTML += "<h3>"+n.short_date+" <a href=\"http://phish.net/setlists/"+n.permalink+".html\">"+n.venue+"</a></h3><p class='pnetsl'>"+n.setlistdata+"</p>";
if(n.setlistnotes.replace(/^\s+|\s+$/g,"")!='') { document.getElementById('setlist').innerHTML += "<p class='pnetsn' style=\"font-size:10px;\">" + n.setlistnotes + "</p>"; }
}
document.getElementById('setlist').innerHTML +="</div>";
}
</script>
<script type="text/javascript" src="https://api.phish.net/v3/setlists/latest/?apikey=<APIKEY>&callback=pnet"></script>">
Instead of calling the code as we flow through rendering the page, we're calling it later and backfilling it into an already rendered page. In other words, we're telling javascript to add the data into a div called "setlist". We're using JSONP here, which allows you to call a script from a remote server, something standard javascript AJAX requests can't do because of the same origin policy. This, however, lets us retrieve the JSON from Phish.net, parse it, and backfill it after the document has already rendered, speeding up the page load.
Note that the second argument to "getJSON" (in the JQuery example) is a function. It's the same function as the bundled Phish.net setlist callback, except instead of writing it via "document.write", we're appending it to the "setlist" div.
Phish.net is a non-commercial project run by Phish fans and for Phish fans under the auspices of the all-volunteer, non-profit Mockingbird Foundation.
This project serves to compile, preserve, and protect encyclopedic information about Phish and their music.
Credits | Terms Of Use | Legal | DMCA
The Mockingbird Foundation is a non-profit organization founded by Phish fans in 1996 to generate charitable proceeds from the Phish community.
And since we're entirely volunteer – with no office, salaries, or paid staff – administrative costs are less than 2% of revenues! So far, we've distributed over $2 million to support music education for children – hundreds of grants in all 50 states, with more on the way.