Skip to content

Instantly share code, notes, and snippets.

@rtekie
Created August 26, 2012 19:58
Show Gist options
  • Save rtekie/3483154 to your computer and use it in GitHub Desktop.
Save rtekie/3483154 to your computer and use it in GitHub Desktop.
// When a new url is added, save it in the local storage and display the home page.
$("#addurl").live("submit" , function(e, data) {
var url = $("#url").val();
addUrl(url);
$.mobile.changePage("#home");
return false;
});
// Add a URL to the list.
function addUrl(url) {
var myUrls = getMyUrls();
// Check for duplicates
if (findUrl(url) === -1) {
myUrls = myUrls.concat(encodeURIComponent(url));
localStorage.setItem("myUrls", JSON.stringify(myUrls));
}
}
// Find URL in the url list.
// Return index or -1 if not found.
function findUrl(url) {
var index = -1;
var myUrls = getMyUrls();
for (var i=0; i < myUrls.length; i++) {
if (myUrls[i] === encodeURIComponent(url)) {
return i;
}
}
return index;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment