Last active
August 12, 2016 02:01
-
-
Save avand/872ab2ed179dfde06779971fc53565a3 to your computer and use it in GitHub Desktop.
This file contains 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
document.querySelector("form").addEventListener("submit", search); | |
function search(event) { | |
event.preventDefault(); | |
var query = document.querySelector("#query").value; | |
var url = "http://omdbapi.com/?s=" + query; | |
// Check local storage to see if the results have already been | |
// retrieved with the URL as the key... | |
var cache = localStorage.getItem(url); | |
// If the results exist in local storage... | |
if (cache != null) { | |
// Parse the JSON string and display every movie in the saved results... | |
JSON.parse(cache)["Search"].forEach(listMovie) | |
} else { | |
$.get(url, function(response) { | |
// Save the response as a JSON string in local storage with the URL | |
// as the key... | |
localStorage.setItem(url, JSON.stringify(response)); | |
response["Search"].forEach(listMovie) | |
}); | |
} | |
} | |
function listMovie(movie) { | |
// Create the <li> for the movie, add necessary child elements, | |
// and append to empty list... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment