How to call a javascript function once on page load, page reloads, don't call it again

How to call a javascript function once on page load, page reloads, don't call it again

September 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to call a javascript function once on page load, page reloads, don't call it again

Solving Magento Cart Form Submission Challenges

In modern e-commerce platforms like Magento, managing form submissions across multiple page refreshes can pose complex challenges, especially when sequential validation is required for subsequent forms. This article addresses how to call a JavaScript function once on initial page load, even after refreshing the page, without causing an infinite submission loop.

The Challenge: Sequential Form Submission in Magento

Let’s first understand the scenario presented:

  1. Shipping Estimate Form: The first form (coShippingMethodForm) is responsible for calculating shipping rates based on the user’s selected options.
  2. Coupon Code Form: The second form requires the shipping rates to be calculated before validating the coupon code against the chosen country and other factors.
  3. Continuous Refresh Issue: If we call coShippingMethodForm.submit() directly on page load, it will refresh the page with updated shipping rates. However, since the JavaScript function remains active in the browser’s memory after the refresh, it could trigger an infinite loop of submissions.

The Solution: Event-Driven Single Execution

To solve this issue without causing excessive page reloads or JavaScript errors, we can leverage event-driven programming along with a single-use flag stored locally. Here’s a step-by-step approach:

Step 1: Store a Flag for Single Use

Use the localStorage API to set a flag indicating whether the form has already been submitted once after the page loaded.

if (!localStorage.getItem('shippingFormSubmitted')) {
    localStorage.setItem('shippingFormSubmitted', true);

    // Proceed with submission
    coShippingMethodForm.submit();
}

Step 2: Reset Flag on Form Submission

Whenever a form is submitted, reset the flag to allow for subsequent submissions if required.

// Add this code inside the submit event handler or click event of your forms
localStorage.removeItem('shippingFormSubmitted');

Step 3: Handle Page Reloads Gracefully

When the page reloads due to a form submission (which is normal in Magento), ensure that the flag remains set until the user manually triggers a new submission.

The combination of these steps ensures that:

  • The coShippingMethodForm.submit() function will only be executed once on the initial page load.
  • It won’t run again when the page is refreshed due to a form submission or any other reason.
  • The user can still manually submit the form by clicking the button, which resets the flag for potential future submissions.

Best Practices and Further Reading

This approach leverages native browser APIs (localStorage) and event-driven programming principles for efficient and maintainable JavaScript code. To learn more about managing state across page refreshes and handling asynchronous operations in Magento, I recommend exploring these resources:

Conclusion

In this article, we’ve identified the core challenge of calling a JavaScript function once on page load while avoiding an infinite loop caused by subsequent refreshes. The solution involves setting and resetting a single-use flag using the localStorage API to ensure that the form submission only occurs during the initial page load or when manually triggered by the user.

By following this approach, you can optimize your Magento store’s performance and provide a smoother user experience without compromising form validation requirements.

Checkout API Errors JavaScript

Our network