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

On a Magento shopping cart, there are 2 forms that need to be filled out: shipping estimate and coupon code. Each refreshes the page after submitting the form. The shipping estimate form needs to be submitted before the coupon form so that the coupon can validate the country.

What I'd like to do is to call the shipping form's coShippingMethodForm.submit() on load. That way the country is loaded, and then the coupon form will check the country. This has the added advantage that the initial shipping rates are displayed without requiring the user to submit the form.

My challenge: if I call coShippingMethodForm.submit() on load - it will refresh the page and then continuously refresh b/c the coShippingMethodForm.submit() keeps submitting and refreshing.

How do I call that function once, page refreshes, and not run again (unless the user clicks the submit button).

Example Magento cart page I'm referring to



You can use cookie to prevent resumbit on reload.
Function js for broswer cookie can be found here
In your JS function coShippingMethodForm.submit

coShippingMethodForm.submit = function(){
     //check cookie here
     createCookie('already_submit', 1, 1); //for 1 day
     //old code here

And on submit button, add eraseCookie line:

<input type="submit" id="your_submit_button" 
       onclick="eraseCookie('already_submit');" />

I see you're using PHP, why not just have a hidden form <input type='hidden' name='countrySubmitted' value='true' /> and then when php renders the page check to see if the value is set, if not then call the function to submit.

echo "<script type='text/javascript'>";
if (isset($_POST['countrySubmitted']))
echo "document.coShippingMethodForm.Submit();";
echo "</script>";


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.