Building a Bookmark Management System: Practicing JavaScript and Local Storage
As I continue my front-end development journey, I realized the need to practice advanced JavaScript concepts and create functional projects to solidify my skills. To do so, I built a Bookmark Management System using HTML, CSS, Bootstrap, and JavaScript. The project leverages Local Storage for data persistence and includes features such as adding, updating, deleting, and searching bookmarks.
This project not only helped me improve my JavaScript skills but also gave me insights into creating user-friendly interfaces and managing client-side data effectively.
Features of the Bookmark Management System
Add Bookmarks: Save bookmarks with a name and URL.
Search Bookmarks: Quickly find bookmarks by name.
Update Bookmarks: Edit existing bookmark details.
Delete Bookmarks: Remove bookmarks permanently.
Data Persistence: Bookmarks are stored in the browser's Local Storage, ensuring they remain available even after the page is refreshed.
Validation: Input validation ensures that only valid bookmark names and URLs are accepted.
Key JavaScript Snippets and Concepts
- Saving Bookmarks to Local Storage
let bookmarksContainer = [];
if (localStorage.getItem('book')) {
bookmarksContainer = JSON.parse(localStorage.getItem('book'));
display();
}
addbtn.addEventListener('click', function() {
if (bookName.classList.contains('is-valid') && bookUrl.classList.contains('is-valid')) {
let bookmark = {
name: bookName.value,
url: bookUrl.value
};
bookmarksContainer.push(bookmark);
localStorage.setItem('book', JSON.stringify(bookmarksContainer));
clearInput();
display();
} else {
errorBox.classList.remove('d-none');
}
});
This code initializes the bookmark container and ensures that bookmarks saved in Local Storage are loaded when the page is refreshed.
- Displaying Bookmarks
function display() {
let row = '';
for (let i = 0; i < bookmarksContainer.length; i++) {
let httpURL = 'https://' + bookmarksContainer[i].url;
row += `
<tr>
<td>${i + 1}</td>
<td>${bookmarksContainer[i].name}</td>
<td><a href="${httpURL}" class="btn btn-success">Visit</a></td>
<td><button onclick="deleteItem(${i})" class="btn btn-danger">Delete</button></td>
<td><button onclick="setFormForUpdate(${i})" class="btn btn-warning">Update</button></td>
</tr>
`;
}
document.getElementById('tableOfContent').innerHTML = row;
}
The display function dynamically renders the bookmarks in a table, making it easy for users to manage their saved links.
- Deleting Bookmarks
function deleteItem(index) {
bookmarksContainer.splice(index, 1);
display();
localStorage.setItem('book', JSON.stringify(bookmarksContainer));
}
This function removes the selected bookmark and updates Local Storage to ensure data consistency.
- Input Validation
function
validateInput(element) {
const regex = {
bookmarkName: /^([A-Z]|[a-z]|[0-9]){3,}$/,
URL: /^(https?:\/\/)?(w{3}\.)?\w+\.\w{2,}\/?(:\d{2,5})?(\/\w+)*$/
};
if (regex[element.id].test(element.value)) {
element.classList.add('is-valid');
element.classList.remove('is-invalid');
return true;
} else {
element.classList.add('is-invalid');
element.classList.remove('is-valid');
return false;
}
}
This validation ensures that users enter meaningful names and properly formatted URLs.
- Updating Bookmarks
function setFormForUpdate(i) {
updatedIndex = i;
addbtn.classList.add('d-none');
updatebtn.classList.remove('d-none');
bookName.value = bookmarksContainer[i].name;
bookUrl.value = bookmarksContainer[i].url;
}
function update() {
bookmarksContainer[updatedIndex].name = bookName.value;
bookmarksContainer[updatedIndex].url = bookUrl.value;
display();
localStorage.setItem('book', JSON.stringify(bookmarksContainer));
clearInput();
addbtn.classList.remove('d-none');
updatebtn.classList.add('d-none');
}
The update functionality lets users modify existing bookmarks and save the changes seamlessly.
Lessons Learned
Data Management: Storing and retrieving data from Local Storage taught me the importance of client-side storage.
Dynamic DOM Manipulation: Creating, updating, and deleting DOM elements dynamically improved my JavaScript skills.
Input Validation: Ensuring only valid inputs are accepted made the application more robust.
User Experience: Adding error handling and form validation improved the overall usability of the project.
Future Enhancements
Implement category-based organization for bookmarks.
Add drag-and-drop functionality for sorting bookmarks.
Use external APIs to fetch website metadata for better bookmark previews.
This project was a fun and challenging way to deepen my understanding of JavaScript and enhance my skills in building interactive, user-focused web applications. If you're on a similar journey, I encourage you to build and experiment with projects like this to grow your skills.
Top comments (0)