Update data on a page without refreshing

Update data on a page without refreshing

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Update Data on a Page Without Refreshing

Updating a specific piece of information, like a flight status changing from "Departing" to "Landed," without forcing the entire browser to reload is a fundamental requirement for modern web applications. This pattern relies on asynchronous HTTP requests, commonly implemented via AJAX (Asynchronous JavaScript and XML) or its modern successor, Fetch API. The goal is to keep the user in the current context while silently fetching new data from the server and updating only the relevant DOM element.

The core mechanism involves intercepting the browser's default behavior before a page load completes or by making a request after the initial page has loaded. When you send an AJAX request, your browser pauses execution for that specific call, waits for the server to respond, and then executes a callback function with the returned data. This allows you to manipulate the Document Object Model (DOM) based on the new state without losing the user's current scroll position or input focus.

In a Laravel environment, the most efficient way to handle this is through LaravelCompany's built-in capabilities or standard Blade directives that leverage AJAX events. You can define specific routes to handle status updates, returning only the JSON snippet required for the update rather than rendering a full view. For example, you might create a route like GET /api/flights/{id}/status that returns a JSON response containing the new status string.

On the client side, you need a way to trigger this request automatically when the data changes or at set intervals. Since the requirement explicitly states that users should not click a button, you must rely on event listeners attached to the DOM elements that represent the changing state. A common approach involves using the MutationObserver API. You can attach an observer to the flight status container; whenever the text content inside that container changes (indicating the backend has updated it), the observer triggers a function that fetches the latest data.

Here is a conceptual example of how you might structure the JavaScript logic:

const statusElement = document.getElementById('flight-status');
const flightIdElement = document.getElementById('flight-id');

// Fetch initial state
async function loadStatus() {
    const response = await fetch(`/api/flights/${flightIdElement.textContent}/status`);
    const data = await response.json();
    statusElement.textContent = data.status;
}

// Listen for DOM changes to trigger refresh
const observer = new MutationObserver(() => {
    // Only refresh if the element indicates a change is needed
    loadStatus(); 
});

observer.observe(statusElement, { childList: true, subtree: true });
loadStatus();

A critical technical detail involves handling concurrency. If multiple events fire simultaneously—such as a user clicking a link and an automatic interval checking the status—you must ensure that only one request is active at a time to prevent race conditions. You can achieve this by using a flag or Promise.allSettled logic to manage pending requests, ensuring the DOM is updated only after the most recent data arrives.

Another consideration is error handling. If the API call fails silently because you are not reloading the page, the user might be left staring at stale information without knowing why. It is best practice to include a brief visual indicator, such as a small spinner or text like "Updating...", while the request is in progress. This provides immediate feedback and prevents the application from appearing frozen.

Security implications also exist when updating data dynamically. You must ensure that your endpoints are protected against Cross-Site Request Forgery (CSRF) attacks, especially if you are using cookies for session management. Laravel's middleware automatically handles CSRF tokens for standard forms, but AJAX calls often bypass traditional form submissions. Ensure you are properly configuring the X-CSRF-TOKEN header or using Laravel Sanctum if you are building a single-page application with stateless authentication.

Finally, consider the frequency of these updates. Polling an endpoint every few seconds can strain server resources. If the status changes infrequently, a polling interval might be overkill. In such cases, implementing Server-Sent Events (SSE) or WebSockets provides a more efficient real-time push mechanism where the server notifies the client only when data actually changes.

Our network