views:

145

answers:

2

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&amp;sc=4&amp;n=1 (or go to http://www.avaline.com, hit "checkout", and login)

You can log in with these credentials: Email:[email protected] 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;

A: 

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.

Fopfong
It's possible I suppose. For some reason it was listed as a global variable in firebug although I don't know where that was declared, and I didn't hit problems with it before when I've used 'var i' in for loops....Why store the options.length in a variable?
Lauren
A: 

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;
Lauren