roguecoder
roguecoder

Reputation: 1

stop javascript from caching

I have a problem.

I am using zend to create a web application and I have come across a problem with my jquery and javascripts.

I have a page showing a order with jquery code written on the page within script tags.

I'm using jquery $.post to load each page on the website.

My problem is that when you select a fresh order page (first time accessing a order page), then click on a button that opens a jquery dialog box to add information to the order the correct post variables are sent; but if you then went on to another order and clicked that add button again it sends the post variables from the previous order.

I assume the previous javascript information had been cached. If I empty my cache it works fine for the order again but fails on the order after it.

I am using php to stick the variable in question (order ID) into the inline javascript code.

I'm guessing this is my downfall.

What I don't understand is the javascript works fine for the initial loading of the page because it then goes and uses ajax to grab a few more sections of the page. That works fine but when I use my button it reverts back to the original order ID used at the beginning.

Attached is my code:

function getFreightTable()
    {
        $.post("order/freighttable", "OrderID=<?= $this->orderID; ?>" , function(data){
            $("#tabs-3").html(data);
        });
    }

    $("#OrderAddFreightButton").live('click',function() {
        $.post("freight/new", "OrderID=<?= $this->orderID; ?>", function(data) {
            $("#orderAddFreightDialog").html(data);
            $("#orderAddFreightDialog").dialog({    
                autoOpen: true, 
                hide: 'slide',
                show: 'slide', 
                title: 'Add New Freight Information',
                closeOnEscape: true,
                close: function(event, ui) 
                { 
                    $("#orderAddFreightDialog").empty();
                    getFreightTable();
                }
            });
        });
        return false;
    });

My php is placing the correct order id's within the code but the javascript is reverting to an older set.

I am having a similar issue with another section of the site and javascript reverting back to javascript code from 3 weeks ago even though I use it on a different computer and cleared the cache. This is freaking me out.

Upvotes: 0

Views: 551

Answers (2)

roguecoder
roguecoder

Reputation: 1

I have tried cache : false

I have tried adding the random get variable at the end.

I am aware that post is not cached but the javascript that is on each page must be because the javascript being used is the first version from the initial view of the page.

Upvotes: 0

Reid
Reid

Reputation: 19419

You can use jQuery's .ajax() to specify cache: false, which will force the request not to cache the request -- note that you have to rewrite your function to no longer use $.post and instead use $.ajax. If you don't want to do that, you can also add to your url in order to trick the browser:

$.post("url...?c=" + Math.random())

It isn't the best way to do things, but they both work.

Upvotes: 4

Related Questions