javascript function won't stop looping - It's on a netsuite website
I need to change the shipping carrier drop-down and shipping method radio button开发者_开发问答 once via a javascript function, not forever.
However, when I use this function, which executes when on the Review and Submit page when the order is < $5, it goes into an endless loop:
function setFreeSampShipping(){
var options = document.forms['checkout'].shippingcarrierselect.getElementsByTagName('option');
for (i=0;i<options.length;i++){
if (options[i].value == 'nonups'){
document.forms['checkout'].shippingcarrierselect.value='nonups';
document.forms['checkout'].shippingcarrierselect.onchange();
document.location.href='/app/site/backend/setshipmeth.nl?c=659197&n=1&sc=4&sShipMeth=2035';
}
}
}
It gets called from within this part of the function setSampPolicyElems() which you can see I've commented out to stop the loop:
if (carTotl < 5 && hasSampp == 1) {
/*
if (document.forms['checkout'].shippingcarrierselect.value =='ups'){
setFreeSampShipping();
}
*/
document.getElementById('sampAdd').style.display="none";
document.getElementById("custbody_ava_webshiptype").value = "7";//no charge free freight
if (document.getElementById("applycoupon")) {
document.location.href='/app/site/backend/setpromocode.nl?c=659197&n=1&sc=4&kReferralCode=SAMPLE'
}
document.write("<div id='msg'><strong>Sample & Shipping:</strong></span> No Charge. (Your sample order is < $5.)</div>");
}
To see the issue, go to the order review and submit page here: https://checkout.netsuite.com/s.nl?c=659197&sc=4&n=1 (or go to http://www.avaline.com, hit "checkout", and login)
You can log in with these credentials:
Email : test2@gmail.com Pass : test03
My solution so far was to swap out the onchange() event trigger line with this, since then I'm not running document.location.href twice: instead, I'm passing the two variables in the one URL query string:
var dropdown = document.getElementById('shippingcarrierselect');
document.body.style.cursor = 'wait';
document.location.href='/app/site/backend/setshipmeth.nl?c=659197&n=1&sc=4&sShipMeth=2035&sShipCarrier='+dropdown.value;
Your variable "i" is global. Also, you should store options.length in the variable. "i" might be set by some scripts which cause infinite loop.
This was my solution:
var dropdown = document.getElementById('shippingcarrierselect'); document.body.style.cursor = 'wait'; document.location.href='/app/site/backend/setshipmeth.nl?c=659197&n=1&sc=4&sShipMeth=2035&sShipCarrier='+dropdown.value;
精彩评论