.unload () function - jQuery

2

I make two ajax requests for an external file from which to add and remove products, but this is not very relevant to this issue. I wanted every time the page was updated, the removeCartaotoCart function was triggered so that depending on the id of the selected product, it was removed before the page was reloaded.

Ajax Requests Code:

var productSelected = "";

            function addCartao(product){
                if( productSelected != "" ){
                    removeCartaotoCart(productSelected);
                  }
                $j('#cartaoMensagem'+product).hide();
                $j('#cartaoMensagemRemover'+product).show();
                $j('#cartaoMensagemRemover'+product).css({'background-color': '#000000'});
                $j.ajax({
                  type: "POST",
                  url: "adiciona.php",
                  data: {
                    product: product
                  },
                  dataType: 'json',
                  cache : false,
                  beforeSend: function () {

                  },
                  success: function (retorno) {
                    var button = $j('#cartaoMensagemRemover'+product);
                    productSelected = product;                   
                    $j('.item-custom').append('<tr id="trAppend'+product+'"><td class="a-center lc-thumbnails"><img src="' + retorno['imagem'] + '" width="50" height="50" alt="' + retorno['name'] + '"></td><td><h3 class="product-name">' + retorno['name'] + '</h3></td><td class="a-center">1</td><td class="a-right"><span class="cart-price"><span class="price"> R$ ' + retorno['price'] + '</span></span></td></tr>');
                    getSubTotal();
                    getGrandTotal();
                  },
                  complete: function () {

                  },
                  error: function (x,y,z) {
                    alert("error");
                    alert(x);
                    alert(y);
                    alert(z);
                  }
              });
            }

            function removeCartaotoCart(itemId){
                console.log('Entrou');
                console.log(itemId);
                productSelected = ""; 
                $j('#cartaoMensagemRemover'+itemId).hide();
                $j('#cartaoMensagem'+itemId).show();
                $j.ajax({
                    type:"POST",
                    url:"remove.php",
                    data:{
                        itemId: itemId
                    },
                    cache: false,
                    beforeSend: function(){

                    },
                    success: function(retorno){
                        var button = $j('#cartaoMensagemRemover'+itemId);  
                        $j('.item-custom #trAppend'+itemId+'').remove();
                        getSubTotal();
                        getGrandTotal();

                    },
                    complete: function () {

                    },
                    error: function (x,y,z) {
                    alert("error");
                    alert(x);
                    alert(y);
                    alert(z);
                  }
                });
            }

        $j(window).unload(function(){
            console.log('Recarregando ...');
            console.log(productSelected);
            removeCartaotoCart(productSelected);
        });

Note: I tried to do the function .unload() , but it returns me errors ( [object Object] ) and the product is not removed.     

asked by anonymous 09.09.2017 / 15:37

1 answer

0

The problem is that you can not do this request for the .unload() function, because in addition to the error, it would "break" this request in the middle. In this case, it should be done when loading the page, using the following code:

$j(document).ready(function(){
    console.log('Carregando ...');
    console.log(productSelected);
    removeCartaotoCart(productSelected);
});
    
28.10.2017 / 16:06