Created
August 26, 2011 18:03
-
-
Save LukeChannings/1173988 to your computer and use it in GitHub Desktop.
fetch_content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fetch_content(e) | |
{ | |
// Put the element into a variable. | |
target = e.srcElement; | |
if ( target.classList.contains("expanded") ){ | |
target.classList.remove("expanded"); | |
} | |
else { | |
target.classList.add("expanded"); | |
} | |
// If the content is already loaded do not proceed. | |
if ( target.classList.contains("contentloaded") ){ | |
return; | |
} | |
if ( target.parentNode.classList.contains("artists") ) var type = "albums"; | |
else if ( target.parentNode.classList.contains("albums") ) var type = "tracks"; | |
// Instantiate a new XHR. | |
var XHR = new XMLHttpRequest(); | |
var uri = "query_library.php?artist="; | |
if ( type === "albums" ){ | |
XHR.open("GET",uri + target.innerText); | |
} | |
else if (type === "tracks") { | |
var artist = String(target.parentElement.parentElement.innerHTML).match(/(.*?)\</)[1]; | |
XHR.open("GET",uri + artist + "&album=" + target.innerText); | |
} | |
// Create a list element. | |
var list = document.createElement("ol"); | |
// Give the list a class. | |
list.classList.add(type); | |
XHR.send(); | |
XHR.onreadystatechange = function(){ | |
if ( XHR.readyState === 4 ){ | |
// Turn the response into an object. | |
var result = JSON.parse(XHR.responseText); | |
for ( var i = 0; i < result.length; i++ ) | |
{ | |
var item = document.createElement("li"); | |
item.innerHTML = result[i]; | |
list.appendChild(item); | |
} | |
target.appendChild(list); | |
} | |
} | |
target.classList.add("contentloaded"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment