IE缓存问题正在破坏我的查找字段

时间:2010-01-15 22:22:40

标签: javascript internet-explorer caching

我正在做一个项目,它使用javascript从视图中获取信息(用Python编写并使用Django界面),基于用户在字段中输入的文本(查询每个keyup),然后显示该信息背部。基本上,这会显示“找不到工作”或显示该工作的名称,用户名和余额。在Firefox中,这一切都很棒。我可以输入JobID,它告诉我ID是新的,我可以创建工作。然后,我可以立即返回页面并输入该ID,我的查找将返回有关该作业的正确信息。

事实是,Internet Explorer 8很懒惰。如果我在IE8中键入作业ID,我的函数会调用查找页面(/ deposits / orglookup /?q = 123)并获取一个值。因此,例如,如果它变为False,我就可以创建一个具有该ID的新作业。如果我然后浏览并在同一个查找字段中输入相同的数字,则Internet Explorer不会刷新查找页面,因此它会再次返回false。如果我浏览到该查找页面,我会看到错误值,但如果我刷新它,我会再次获得正确的信息。我是否可以在每次输入查询字段时强制执行此查询,而不是像IE一样参考缓存页面?

我将补充一点,在每个用户的基础上解决这个问题并不是很好,因为这是一个组织范围的应用程序,所以我真的可以使用一个修复程序,我可以写入我的代码某处以强制IE实际上每次应该刷新查找页面。

这是查找功能的代码,如果有帮助的话。它有点乱,但我没有写它所以我会尝试包括所有相关的东西:

$("#id_JobID").keyup( 

    function(event){

        //only fire gets on 0-9, kp 0-9, backspace, and delete
        if (event.keyCode in { 96:1, 97:1, 98:1, 99:1, 100:1, 101:1, 102:1, 103:1, 104:1, 105:1,
                                46:1,48:1, 49:1, 50:1, 51:1, 52:1, 53:1, 54:1, 55:1, 56:1, 57:1, 8:1}) 
        {

            if ($("#loadimg").attr("src") != "/static/icons/loading.gif") {
                $("#loadimg").attr("src", "/static/icons/loading.gif");
            }

            if ($("#loadimg").length < 1) {
                $("#id_JobID").parent().append("<img id=loadimg src=/static/icons/loading.gif>");
            }

            clearTimeouts(null); //clear all existing timeouts to stop any running lookups
            GetCounter++; 
            currLoc = window.location.href.slice(window.location.href.indexOf('?') + 1).split('/').slice(-2,-1);

            if (currLoc == 'restorebatch') {
                var TimeoutId = setTimeout(function() {dynamicSearch('restorelookup');}, 400);
            } else {
                var TimeoutId = setTimeout(function() {dynamicSearch('orglookup');}, 400);
            }

            //alert(TimeoutID);
            TimeoutBag[GetCounter] = {
                'RequestNumber': GetCounter,
                'TimeoutId': TimeoutId
            }
        }
    }
);

function clearTimeouts(TimeoutBagKeys) //TimeoutBagKeys is an array that contains keys into the TimeoutBag of Timeout's you want to clear
{
    if(TimeoutBagKeys == null) //if TimeoutBagKeys is null, clear all timeouts.
    {
        for (var i = 0; i < TimeoutBag.length; i++)
        {
           if (TimeoutBag[i] != null) {
            clearTimeout(TimeoutBag[i].TimeoutId);
           }
        }
    }
    else //otherwise, an array of keys for the timeout bag has been passed in. clear those timeouts.
    {
        var ClearedIdsString = "";
        for (var i = 0; i < TimeoutBagKeys.length; i++)
        {
            if (TimeoutBag[TimeoutBagKeys[i]] != null)
            {
                clearTimeout(TimeoutBag[TimeoutBagKeys[i]].TimeoutId);
                ClearedIdsString += TimeoutBag[TimeoutBagKeys[i]].TimeoutId;
            }
        }        
    }
}
function dynamicSearch(viewname) {

        $(".lookup_info").slideUp();


        if ($("#id_JobID").val().length >= 3) {
            var orgLookupUrl = "/deposits/" + viewname + "/?q=" + $("#id_JobID").val();
                getBatchInfo(orgLookupUrl);

        }
        else if ($("#id_JobID").val().length  == 0) {
            $("#loadimg").attr("src", "/static/icons/blank.gif");
            $(".lookup_info").slideUp();
        }
        else {
            $("#loadimg").attr("src", "/static/icons/loading.gif");
            $(".lookup_info").slideUp();
        }
}
function getBatchInfo(orgLookupUrl) {
                $.get(orgLookupUrl, function(data){ 
                    if (data == "False") {
                        $("#loadimg").attr("src", "/static/icons/red_x.png");
                        $(".lookup_info").html("No batch found - creating new batch.");
                        $("#lookup_submit").val("Create");
                        $(".lookup_info").slideDown();
                        toggleDepInputs("on");
                    }
                    else {  
                        $("#loadimg").attr("src", "/static/icons/green_check.png");
                        $("#lookup_submit").val("Submit");
                        $(".lookup_info").html(data);
                        $(".lookup_info").slideDown()
                        toggleDepInputs("off");
                    };
                 });
}

3 个答案:

答案 0 :(得分:1)

有三种解决方案:

无论

  • 使用$ .post代替$ .get
  • 向您的网址添加随机GET参数,例如“?update = 10202203930489”(当然,每次请求都需要不同)
  • 或禁止通过发送正确的标头(if-modified-since
  • 在服务器端进行缓存

答案 1 :(得分:0)

您需要为每个请求设置唯一的网址。防故障方法是引入新的GET参数,该参数具有时间戳作为其值 - 因此URL对于每个请求都是唯一的,因为时间戳总是在变化,因此IE无法缓存它。

url = "/deposits/orglookup/?q=123&t=" + new Date().getTime()

因此,您现在只有一个参数(q),而不是一个参数(qt),但由于服务器通常不关心额外的参数,所以它可以正常

答案 2 :(得分:0)

通常可以使用的一个技巧是将查询URL附加时间戳作为查询字符串参数,从而在每次发出请求时生成唯一的URL。

  

var orgLookupUrl =“/ deposit /”+   viewname +“/?q =”+   $(“#id_JobID”)。val()+“&amp; time =”+ new Date()。getTime();;