zozjr

AHMED (ZOZ)

Posted on November 24, 2024

Bookmarker

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

  1. Add Bookmarks: Save bookmarks with a name and URL.

  2. Search Bookmarks: Quickly find bookmarks by name.

  3. Update Bookmarks: Edit existing bookmark details.

  4. Delete Bookmarks: Remove bookmarks permanently.

  5. Data Persistence: Bookmarks are stored in the browser's Local Storage, ensuring they remain available even after the page is refreshed.

  6. Validation: Input validation ensures that only valid bookmark names and URLs are accepted.


Key JavaScript Snippets and Concepts

  1. 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');
    }
});
Enter fullscreen mode Exit fullscreen mode

This code initializes the bookmark container and ensures that bookmarks saved in Local Storage are loaded when the page is refreshed.

  1. 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;
}
Enter fullscreen mode Exit fullscreen mode

The display function dynamically renders the bookmarks in a table, making it easy for users to manage their saved links.

  1. Deleting Bookmarks
function deleteItem(index) {
    bookmarksContainer.splice(index, 1);
    display();
    localStorage.setItem('book', JSON.stringify(bookmarksContainer));
}
Enter fullscreen mode Exit fullscreen mode

This function removes the selected bookmark and updates Local Storage to ensure data consistency.

  1. 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;
    }
}

Enter fullscreen mode Exit fullscreen mode

This validation ensures that users enter meaningful names and properly formatted URLs.

  1. 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');
}
Enter fullscreen mode Exit fullscreen mode

The update functionality lets users modify existing bookmarks and save the changes seamlessly.


Lessons Learned

  1. Data Management: Storing and retrieving data from Local Storage taught me the importance of client-side storage.

  2. Dynamic DOM Manipulation: Creating, updating, and deleting DOM elements dynamically improved my JavaScript skills.

  3. Input Validation: Ensuring only valid inputs are accepted made the application more robust.

  4. 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.

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
zozjr
AHMED (ZOZ)

Posted on November 24, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About