Madhurima Mindscape

Modules, Materials, Moments, Memories, and More

๐Ÿก Home ๐Ÿ“‘ Saved ๐Ÿ“‚ Blogs

Making Websites Interactive with Browser APIs

November 16, 2026

Calculating reading time...

A website doesn't have to be static to be simple. With JavaScript and the browser APIs already available to us, we can build useful interactive features without depending on additional libraries or a complicated backend.

While developing Madhurima Mindscape, I explored several browser capabilities to make the reading and browsing experience more engaging. These included remembering a visitor's theme preference, saving articles, sharing links, copying URLs, and reading articles aloud with word highlighting.

Browser APIs illustration

Browser APIs and Web Development (Source)

In this article, I'll walk through some of the browser APIs and JavaScript techniques I used, along with examples from my website.

"Sometimes, the most useful website features are already available in the browser. We just need to know how to use them."

๐ŸŒ 1. Understand Browser APIs

Browser APIs are built-in interfaces that allow JavaScript to interact with browser features and capabilities.

They make it possible to work with browser storage, share content, access the clipboard, produce speech, observe element dimensions, and manipulate page content.

For Madhurima Mindscape, some particularly useful browser APIs and features were:

  • LocalStorage: Stores information in the browser so it remains available across page visits.
  • Clipboard API: Allows text to be copied to the system clipboard.
  • Web Share API: Opens the device's native sharing interface.
  • Web Speech API: Provides text-to-speech functionality.
  • ResizeObserver: Detects changes in an element's dimensions.
  • CSS Custom Highlight API: Provides a way to highlight text ranges without changing the underlying document structure.

These capabilities can help create useful interactions without introducing unnecessary dependencies.

๐Ÿ’ก My tip: Before installing an additional library for a small browser-related feature, check whether a native browser API already provides what you need.

๐ŸŽจ 2. LocalStorage: Remember User Preferences

One of the first interactive features I implemented was theme selection.

Madhurima Mindscape supports multiple visual themes. Instead of asking visitors to select their preferred theme every time they open the website, I wanted the browser to remember their last selection.

That's where LocalStorage comes in. It stores key-value pairs in the browser. Unlike temporary JavaScript variables, its values remain available after a page refresh or browser restart, until they are cleared or removed.

Saving a selected theme

In my index.js file, the applyTheme() function handles theme changes and stores the selected theme:

function saveTheme(themeName) {

    try {
        localStorage.setItem(
            "madhurima-mindscape-website-theme",
            themeName
        );

        console.log("Theme saved:", themeName);

    } catch (error) {
        console.warn(
            "localStorage is unavailable:",
            error
        );
    }

}

The setItem() method stores the selected theme under a specific key.

Restoring the saved theme

When the page loads, I retrieve the previously selected value:

document.addEventListener("DOMContentLoaded", function () {

    try {
        const savedTheme = localStorage.getItem(
            "madhurima-mindscape-website-theme"
        );

        applyTheme(savedTheme || "default");

    } catch (error) {
        console.warn("Error loading saved theme:", error);

        applyTheme("default");
    }

});

Here, getItem() retrieves the stored theme. If no saved value exists, the website uses the default theme.

Saving blog articles

I also used LocalStorage to implement a Saved Articles feature.

When a visitor saves an article, its URL path is added to an array. The array is converted into JSON before being stored:

localStorage.setItem(
    savedKey,
    JSON.stringify(savedPosts)
);

To retrieve the saved articles:

const stored = JSON.parse(
    localStorage.getItem(savedKey) || "[]"
);

const savedPosts = Array.isArray(stored) ? stored : [];

I also maintain a separate record of saved dates so that the website can associate a timestamp with each saved article.

๐Ÿ’ก What I learned: LocalStorage is useful for preferences and lightweight browser-side features. However, saved information belongs to the visitor's browser, not a central database. It does not automatically synchronize across devices or browsers, and visitors can clear it at any time.

๐Ÿ“‹ 3. Clipboard API: Copy Article Links

Sharing an article should be easy, even when a visitor doesn't want to open a sharing application.

The Clipboard API provides a convenient way to copy text directly to the clipboard. In Madhurima Mindscape, I use it as a fallback when the native sharing feature isn't available.

if (
    navigator.clipboard &&
    window.isSecureContext
) {
    await navigator.clipboard.writeText(postUrl);

    showStatus("Article link copied!");
}

The navigator.clipboard.writeText() method copies the article URL.

The example also checks window.isSecureContext because clipboard access is generally restricted to secure contexts, such as HTTPS websites.

After copying the link, I display a temporary status message to let the visitor know that the action was successful.

๐Ÿ’ก What I learned: Clipboard access is subject to browser security restrictions and user permissions. It's important to provide an alternative when the API is unavailable.

๐Ÿ“ค 4. Web Share API: Share Articles Through Native Apps

The Web Share API allows websites to use the device's native sharing interface.

Instead of manually copying a link and opening another application, visitors can use the browser's sharing interface to send content through supported applications.

For my blog, I prepare the article's title, description, and URL:

const shareData = {
    title: postTitle,
    text: "Read this article on Madhurima Mindscape",
    url: postUrl
};

Then I check whether the browser supports native sharing:

if (navigator.share) {

    await navigator.share(shareData);

    showStatus("Share action completed.");

} else if (
    navigator.clipboard &&
    window.isSecureContext
) {

    await navigator.clipboard.writeText(postUrl);

    showStatus("Article link copied!");

} else {

    window.prompt(
        "Copy this article link:",
        postUrl
    );

}

This creates a fallback sequence:

  1. Use the native sharing interface when available.
  2. Otherwise, try copying the article URL.
  3. If clipboard access is unavailable, display the URL in a prompt so the visitor can copy it manually.

The sharing operation is asynchronous, so I use async and await to handle its result.

I also handle errors separately. For example, if a visitor closes the sharing interface without completing the action, the browser may report an AbortError. That should not be treated as an ordinary failure.

๐Ÿ’ก What I learned: Browser support and sharing options vary across devices. A fallback makes the feature more reliable instead of assuming every browser supports native sharing.

๐Ÿ”Š 5. Web Speech API: Read Articles Aloud

One of the more interesting features I implemented was Read Aloud.

The Web Speech API provides speech synthesis capabilities, allowing a browser to convert text into spoken audio.

I wanted this feature to do more than simply read the article. I also wanted visitors to be able to pause, resume, follow the current reading position, and see the word being spoken.

Checking browser support

Before initializing the feature, I check whether speech synthesis is available:

if (
    readAloudButton &&
    "speechSynthesis" in window
) {
    const synth = window.speechSynthesis;
}

The speechSynthesis interface provides methods to control speech playback.

Creating a speech utterance

The SpeechSynthesisUtterance interface represents the text that should be spoken. I created a helper function to configure the voice and speech properties:

function createUtterance(text) {

    const utterance = new SpeechSynthesisUtterance(text);

    utterance.lang = "en-IN";
    utterance.rate = 1;
    utterance.pitch = 1;

    const voice = getIndianVoice();

    if (voice) {
        utterance.voice = voice;
    }

    return utterance;
}

I also added voice selection logic using speechSynthesis.getVoices(). The function looks for available Indian English voices and prefers a voice whose name matches a list of familiar voice names. If no suitable voice is available, the browser's default voice is used.

The available voices depend on the operating system and browser.

Reading article content

Rather than reading the entire page indiscriminately, I collect text nodes from the main article content.

I use document.createTreeWalker() with NodeFilter.SHOW_TEXT to traverse text nodes and exclude elements such as scripts, navigation, buttons, and form controls. This keeps unrelated interface content out of the reading queue.

Highlighting the spoken word

One of the most useful parts of the implementation is word highlighting.

The onboundary event of a speech utterance can report when speech reaches a word boundary. I use its charIndex value to identify the current position in the text.

utterance.onboundary = function (event) {

    if (event.name === "word") {
        highlightWord(node, event.charIndex);
    }

};

The highlightWord() function creates a DOM Range around the current word.

Where supported, I use the CSS Custom Highlight API through CSS.highlights and Highlight. If that capability is unavailable, the implementation falls back to applying a CSS class to the current text block. This provides a visual indication of the reading position.

Pause, resume, and stop

The Web Speech API provides methods for controlling playback:

// Pause speech.
synth.pause();

// Resume speech.
synth.resume();

// Stop speech and clear the queue.
synth.cancel();

I connected these methods to the Read Aloud button, allowing it to switch between reading, paused, and resumed states.

I also implemented automatic scrolling to follow the current reading position, while allowing visitors to scroll manually without immediately losing their place.

๐Ÿ’ก What I learned: Text-to-speech can make articles more accessible and convenient to consume. However, speech voices, word-boundary events, and playback behavior can differ between browsers and operating systems. Testing on different devices is important.

๐Ÿ“ 6. ResizeObserver: Keep Carousel Layouts Responsive

While developing the website's carousel, I needed its slides to maintain the same height as the icon hero section.

A fixed height would not work well because the hero section changes dimensions depending on the viewport and its content.

I used ResizeObserver to detect changes in the hero section's dimensions.

const heroSection = document.querySelector(
    ".icon-hero-section"
);

const heroResizeObserver = new ResizeObserver(function () {
    matchCarouselHeight();
});

if (heroSection) {
    heroResizeObserver.observe(heroSection);
}

The matchCarouselHeight() function measures the hero section and applies the resulting height to each carousel slide:

function matchCarouselHeight() {

    const heroHeight =
        heroSection.getBoundingClientRect().height;

    if (!Number.isFinite(heroHeight) || heroHeight <= 0) {
        return;
    }

    carouselSlides.forEach(function (slide) {
        slide.style.height = heroHeight + "px";
    });

}

I also trigger height synchronization when the page finishes loading and when the browser window is resized.

To avoid unnecessary calculations during rapid resizing, I use a small timeout to debounce the resize handler.

๐Ÿ’ก What I learned: ResizeObserver is particularly useful when an element's dimensions depend on content or other layout changes, rather than only on the browser window size.

๐Ÿ—‚๏ธ 7. Organize Browser Features Across JavaScript Files

As interactive features grow, keeping all the JavaScript in a single file can make the project difficult to maintain.

I kept the interactions in separate JavaScript files according to their responsibilities.

  • index.js: Theme management, saved theme restoration, and desktop/mobile dropdown behavior.
  • slider-carousel.js: Dynamic carousel height synchronization.
  • post-actions.js: Article saving, sharing, comments, reading time, and read-aloud functionality.

I also used reusable Jekyll components so that post actions could be included across articles without duplicating their markup.

This separation makes the code easier to maintain and helps keep individual features independent.

๐Ÿ’ก My tip: Organize JavaScript according to feature responsibilities. It becomes easier to debug, update, and reuse individual features as your website grows.

๐Ÿงช 8. Handle Browser Compatibility and User Experience

Browser APIs are powerful, but they come with a few practical considerations. Not every browser supports every API, and some features depend on permissions, secure contexts, or user interaction.

Here are some important points to remember:

  • Check browser support: Not every browser implements every API.
  • Provide fallbacks: Clipboard and sharing features should have alternatives.
  • Handle errors: Browser permissions, unavailable storage, and interrupted operations can cause failures.
  • Respect user interaction: Some capabilities require a user action or permission before they can be used.
  • Test across devices: Mobile and desktop browsers may behave differently.
  • Consider accessibility: Interactive controls should have meaningful labels and accessible states.
  • Protect user data: LocalStorage is suitable for lightweight preferences, but it is not a replacement for secure server-side storage.

It is also worth remembering that LocalStorage is accessible to JavaScript running on the same origin. Avoid storing passwords, authentication secrets, or other sensitive information in it.

๐Ÿ’ก My tip: Don't build an interactive feature around the assumption that every browser will behave identically. Check support, handle failures gracefully, and make sure the basic website experience remains usable.

๐Ÿ“Š 9. Bring the Features Together

These browser capabilities are used across different parts of Madhurima Mindscape. Each one contributes to a more convenient browsing and reading experience.

The following table summarizes where these features are implemented:

Feature Browser API or Capability Implementation
Remembering themes LocalStorage index.js
Saving articles LocalStorage post-actions.js
Sharing article links Web Share API post-actions.js
Copying article URLs Clipboard API post-actions.js
Reading articles aloud Web Speech API post-actions.js
Following spoken text Speech boundary events and CSS Custom Highlight API post-actions.js
Synchronizing carousel heights ResizeObserver slider-carousel.js

Keeping these features separate makes the implementation easier to understand and maintain. It also allows individual features to be improved without unnecessarily affecting unrelated parts of the website.

๐Ÿ’ก My tip: Start with small, useful interactions and organize them properly. A collection of well-implemented features can make a website feel much more engaging without making its code unnecessarily complicated.

๐Ÿ’œ Building Better Experiences with Browser APIs

Building interactive features doesn't always require a large framework or a complex backend. Browser APIs provide many useful capabilities that can make websites more responsive, accessible, and convenient.

Working on Madhurima Mindscape gave me an opportunity to explore these capabilities through practical implementations rather than treating them as isolated JavaScript concepts.

From remembering a visitor's preferred theme to reading an entire article aloud with word highlighting, each feature helped me understand how browser APIs can improve the everyday experience of using a website.

The biggest takeaway is that a good interactive feature isn't just about making something work. It's about making it useful, reliable, and comfortable for the person using it.

"A good interactive feature isn't just about making something work; it's about making it useful, reliable, and comfortable for the person using it."

Interested in the website behind these implementations? Explore Madhurima Mindscape and its source code to see how the browser features are organized and maintained.

Explore the GitHub Repository