简体   繁体   English

停止JavaScript的缓存

[英]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. 我正在使用zend创建一个Web应用程序,但我的jquery和javascripts遇到了问题。

I have a page showing a order with jquery code written on the page within script tags. 我有一个页面,该页面显示脚本标记内的页面上写有jquery代码的订单。

I'm using jquery $.post to load each page on the website. 我正在使用jquery $ .post加载网站上的每个页面。

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; 我的问题是,当您选择一个新的订单页面(首次访问订单页面)时,然后单击一个按钮,该按钮将打开一个jquery对话框,以向发送正确过帐变量的订单添加信息。 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. 我认为以前的javascript信息已被缓存。 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. 我正在使用php将有问题的变量(订单ID)粘贴到嵌入式javascript代码中。

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. 我不明白的是,该javascript可以在页面的初始加载中正常运行,因为它随后可以使用ajax来捕获页面的更多部分。 That works fine but when I use my button it reverts back to the original order ID used at the beginning. 效果很好,但是当我使用按钮时,它会恢复为开始时使用的原始订单ID。

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. 我的php在代码中放置了正确的订单ID,但是javascript恢复为旧版本。

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. 我在网站的另一部分也遇到了类似的问题,即使我在其他计算机上使用它并清除了缓存,javascript仍从3周前恢复为javascript代码。 This is freaking me out. 这吓到我了。

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 . 您可以使用jQuery的.ajax()指定cache: false ,这将强制请求不缓存请求-请注意,您必须重写函数以不再使用$.post ,而是使用$.ajax If you don't want to do that, you can also add to your url in order to trick the browser: 如果您不想这样做,也可以将其添加到URL中以欺骗浏览器:

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

It isn't the best way to do things, but they both work. 这不是做事情的最佳方法,但是它们都可以工作。

I have tried cache : false 我尝试过cache : false

I have tried adding the random get variable at the end. 我尝试在最后添加随机get变量。

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. 我知道该帖子没有被缓存,但是每个页面上的javascript必须是因为所使用的javascript是页面初始视图中的第一个版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM