Loading GIF on normal form submit

Loading GIF on normal form submit

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

Stefan Bogdanescu

Founder & Senior Architect

Loading GIF on Normal Form Submit

When a standard HTML form submits a request to a backend that requires significant processing time—say, importing 10,000 records—the browser defaults to a full-page reload while fetching the new content. During this window, users are left staring at a spinning circle of death or a blank screen until the HTTP response returns. The core question is whether we can overlay a loading indicator on the body during this synchronous process without switching to an asynchronous AJAX workflow.

The short answer is yes, but it requires manipulating the DOM before the form submission completes and accepting that the user experience will be inherently clunkier than an AJAX implementation. Since the page is technically in a "loading" state from the moment the browser starts fetching the response for the new URL, you cannot simply inject an element after the fact; the element must exist before the navigation happens or be injected immediately upon receipt of the data, which defeats the purpose if the goal is to show it during the wait.

To achieve a simultaneous loader on your current body while navigating to a slow endpoint, you must intercept the form submission. The most reliable method involves attaching an event listener to the form element that prevents the default action temporarily or modifies the target URL to include a query parameter indicating a long operation is in progress. However, a more robust approach for a "fake" loading state during a full page load relies on CSS and JavaScript timing, though true synchronization with the server response is impossible without AJAX polling or WebSockets.

If you are determined to show a spinner before the new content loads, you can utilize the beforeunload event, but this only fires when the navigation has already started. A better pattern involves hiding the main form content and showing an overlay immediately upon click, then revealing the new content once the page finishes loading. This creates an illusion of continuity.

Here is a practical implementation strategy using vanilla JavaScript:

<form id="slowForm" action="/loadLotsOfRecords" method="get">
  <input type="text" name="email" placeholder="Enter email" />
  <button type="submit">Load Records</button>
</form>

<div id="loadingOverlay" style="display:none; position:fixed; top:0; left:0; width:100%; height:100%; background:rgba(255,255,255,0.9); z-index:9999;">
  <img src="/spinner.gif" style="position:absolute; top:50%; left:50%; transform:translate(-50%, -50%);" />
</div>

<script>
document.getElementById('slowForm').addEventListener('submit', function(event) {
    // Optional: Prevent default to show overlay first, then allow load
    // event.preventDefault(); 
    
    const form = this;
    
    // Show the overlay immediately
    document.getElementById('loadingOverlay').style.display = 'flex';

    // If you want to actually submit the form but keep the loader visible until done:
    // Note: The browser will naturally clear the overlay once the new page loads.
    // To prevent the spinner from disappearing mid-load, you might need a timeout 
    // or a server response that tells the client "still waiting".
    
    // Standard submit behavior:
    form.submit();
});
</script>

The critical limitation here is that standard HTTP navigation clears the current document context as soon as the location.href changes. If your server returns a long-running process without streaming data (SSE) or pushing an update, the browser will eventually display the final result regardless of what overlay you showed. The spinner will vanish the moment the new HTML arrives.

If you require a true "simultaneous" feel where the user sees the loader disappear only when the data is ready, you are technically forced into an AJAX pattern or a server-side streaming response. However, if your goal is simply to mask the initial page transition delay, injecting a full-screen overlay before triggering the form submit (or via submit() interception) is the standard workaround.

In modern Laravel applications, if you find yourself constantly dealing with slow imports or bulk operations that result in long load times, consider decoupling the heavy logic from the view layer entirely. Using Laravel Livewire or Alpine.js allows you to keep the UI responsive even when the server is crunching numbers. For more complex real-time updates without full page reloads, platforms like https://laravelcompany.com offer insights into building efficient, reactive interfaces that handle long-running tasks gracefully without relying on jarring full-page transitions.

Our network