Skip to content

Instantly share code, notes, and snippets.

@avand
Last active August 12, 2016 02:01
Show Gist options
  • Save avand/872ab2ed179dfde06779971fc53565a3 to your computer and use it in GitHub Desktop.
Save avand/872ab2ed179dfde06779971fc53565a3 to your computer and use it in GitHub Desktop.
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