Meules
Meules

Reputation: 1389

Update script works in all browsers except Edge

I've made a script that submit multiple forms to a shopping cart with one button. After the submit is done a new function updateCartAjax() is fired to update the cart content without actually going to the cart page.

This script works fine in all browsers except for Edge. Somehow it looks like that adding the forms to the cart functions correctly but updating the cart isn't.

When I clear my cookies it function correctly the first time. When adding other stuff to the cart then the function updateCartAjax() isn't fired. However pressing F5 works.

Is there anything in my script that I have missed and could cause the updateCartAjax() not to work? I don't get any errors in my console in any browser whatsoever!!

My script

// forms look like
<div class="temp-cart">
    <form id="form_19093981" method="post" action="/cart/add/19093981/"></form>
    <form id="form_19093982" method="post" action="/cart/add/19093982/"></form>
    <form id="form_19093983" method="post" action="/cart/add/19093983/"></form>
    <form id="form_19093984" method="post" action="/cart/add/19093984/"></form>
    <button type="submit" class="btn btn-custom-2 addToCart">Submit!</button>
</div>


function updateCartAjax() {
    $.get("/cart/?format=json", function(e) {
        var a = getAjaxTranslation,
            t = [],
            i = e.shop.currency2;
        if (e.page.cart) {
            e.page.cart.products.forEach(function(e) {
                t.push('<li class="item clearfix">  <a href="cart/delete/' + e.qid + '" title="' + a("Delete") + '" class="delete-item" data-qid="' + e.qid + '"><i class="fa fa-times"></i></a>  <figure>    <a href="' + e.url + '" title="' + e.fulltitle + '">      <img src="' + imageIdToUrl(e.image, "60x60x2") + '" width="60" height="60" alt="' + e.fulltitle + '" title="' + e.fulltitle + '" />    </a>  </figure>  <div class="dropdown-cart-details">    <p class="item-name">      <a href="' + e.url + '" title="' + e.fulltitle + '">       ' + (e.brand.title ? "<span>" + e.brand.title + "</span>" : "") + "       " + e.title + '        <span class="variant">' + e.variant + "</span>      </a>    </p>    <p>      " + e.quantity + 'x      <span class="item-price">' + i.symbol + e.price.price + "</span>    </p>  </div></li>")
            });
            var s = '<div class="dropdown-cart-menu-container">  <div class="btn-group dropdown-cart">    <div class="dropdown-toggle" data-toggle="dropdown">      <div class="minicart"></div>      <div class="info">        <span class="cartitems"><b>' + e.page.cart.products.length + "</b> " + a("Items") + '</span>        <span class="cartprice">' + i.symbol + e.page.cart.total.price.toFixed(2).replace(/\./g, ',') + '</span>      </div>    </div>    <div class="dropdown-menu dropdown-cart-menu pull-right clearfix" role="menu">      <ul class="dropdown-cart-product-list">' + t.join("") + '</ul>      <ul class="dropdown-cart-total">        <li><span class="dropdown-cart-total-title">        ' + a(e.shop.b2b ? "Total excl. VAT" : "Total") + "</span>" + i.symbol + e.page.cart.total.price.toFixed(2) + '</li>      </ul>       <div class="dropdown-cart-action">          <span><a href="/cart" title="' + a("My shopping cart") + '">' + a("Edit") + '</a></span>          <span><a href="' + a("Checkout") + '" class="btn btn-custom" title="' + a("Checkout") + '">' + a("Checkout") + "</a></span>        </div>    </div>  </div></div>"
        }
        $("#cart").html(s)
    })
}



  var state = false;

  $(document).ready(function() {
    $('.temp-cart .addToCart').click(function() {
      go();

      // get all forms on the page
      $forms = $('.temp-cart form');
      sent = 0;

      // post the form (non-async)
      $forms.each(function() {
        if(state) {
          var qty = $(this).find('.amt').text();
          var qty2 = parseInt(qty);
          var url = $(this).attr('action')+'?quantity='+qty2+'/';
          $.ajax({
            type: "post",
            async: false,
            url: url,
            data: $(this).serialize(),
            success: function(data) {
              if(++sent == $forms.length) {
                console.log('All forms are submitted!');

               // updateStuff();
                updateCartAjax();

              }
            }
          });
        } else { return false; }
      });
      stop();
    });

    function go() {
      if(!state) {
        state = true;
        $('.temp-cart .addToCart').button('loading');
        $('input[type=button], input[type=submit]').attr("disabled", "disabled");
      }}

    function stop() {
      if(state) {
        state = false;
        $('.temp-cart .addToCart').button('reset');
        $('input[type=button], input[type=submit]').removeAttr("disabled");
      }}
  });

Upvotes: 0

Views: 64

Answers (1)

Rion Williams
Rion Williams

Reputation: 76577

Sounds Like Caching

This sounds like a scenario that might be prone to being frequently cached. Internet Explorer and Edge in my experience can be quite aggressive with regards to AJAX caching and disabling it might help you resolve this.

You can explicitly turn off AJAX caching via the following snippet of code within your document-ready function :

$(document).ready(function() {
     // Disable AJAX-based caching
     $.ajaxSetup({ cache: false });

     // Other code omitted for brevity
});

This is obviously a more global solution, however you can set the cache property during individual AJAX requests as you might expect :

$.ajax({
    method: 'GET',
    url: '...',
    cache: false,
    success: function(){

    }
});

or by just appending a querystring value to the URL that you are requesting :

// Using a timestamp will ensure the request isn't cached
$.get("/cart/?format=json&dt=" + (new Date()).getTime(), function(e) {

});

Use The Developer Tools (F12) If the issue persists, consider using the Developer Tools (F12) within your browser and monitoring the Network tab to see if the actual requests are being made and if they are either failing or just being returned from the cache.

Upvotes: 1

Related Questions