如何在页面加载,页面重新加载时调用一次javascript函数,不要再次调用它 [英] How to call a javascript function once on page load, page reloads, don't call it again

查看:477
本文介绍了如何在页面加载,页面重新加载时调用一次javascript函数,不要再次调用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Magento购物车上,有两种表格需要填写:运费估算和优惠券代码。每个提交表单后刷新页面。需要在优惠券表格之前提交运费估算表单,以便优惠券可以验证该国家/地区。

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.

我想做的是在加载时调用运输表单的 coShippingMethodForm.submit()。这样,国家加载,然后优惠券表格将检查国家。这样做的另一个好处是显示初始运费,而无需用户提交表格。

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.

我的挑战:如果我打电话给 coShippingMethodForm。在加载时提交() - 它将刷新页面然后不断刷新b / c coShippingMethodForm.submit()继续提交和刷新。

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).

我所指的Magento购物车页面示例

推荐答案

您可以使用cookie来防止在重新加载时重新开始。

broswer cookie的函数js可以在这里找到< a href =http://www.quirksmode.org/js/cookies.html =nofollow noreferrer> http://www.quirksmode.org/js/cookies.html
b $ b在你的JS函数中coShippingMethodForm.submit

You can use cookie to prevent resumbit on reload.
Function js for broswer cookie can be found here http://www.quirksmode.org/js/cookies.html
In your JS function coShippingMethodForm.submit

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

等等提交按钮,添加eraseCookie行:

And on submit button, add eraseCookie line:

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

这篇关于如何在页面加载,页面重新加载时调用一次javascript函数,不要再次调用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆