jQuery: The Write Less, Do More JavaScript Library

Changeset 4006

Show
Ignore:
Timestamp:
12/04/07 04:43:45 (5 months ago)
Author:
davidserduke
Message:

Fixed #1999 by replacing the 'no-cache' parameter if it is there instead of just appending.

Location:
trunk/jquery
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/jquery/src/ajax.js

    r3984 r4006  
    198198            s.cache = false; 
    199199 
    200         if ( s.cache === false && s.type.toLowerCase() == "get" ) 
    201             s.url += (s.url.match(/\?/) ? "&" : "?") + "_=" + (new Date()).getTime(); 
     200        if ( s.cache === false && s.type.toLowerCase() == "get" ) { 
     201            var ts = (new Date()).getTime(); 
     202            // try replacing _= if it is there 
     203            var ret = s.url.replace(/(\?|&)_=.*(&|$)/, "$1_=" + ts + "$2"); 
     204            // if nothing was replaced, add timestamp to the end 
     205            s.url = ret + ((ret == s.url) ? (s.url.match(/\?/) ? "&" : "?") + "_=" + ts : ""); 
     206        } 
    202207 
    203208        // If data is available, append data to url for get requests 
  • trunk/jquery/test/unit/ajax.js

    r4005 r4006  
    227227        // Re-enabled because a bug was found in the unit test that probably caused the problem 
    228228        if(++count == 5) 
    229         start(); 
     229            start(); 
    230230    }; 
    231231     
     
    235235    ok( $.getJSON(url("data/json_obj.js"), success), "json" ); 
    236236    ok( $.ajax({url: url(target), success: success}), "generic" ); 
     237}); 
     238 
     239test("ajax cache", function () { 
     240    expect(18); 
     241    stop(); 
     242     
     243    var count = 0; 
     244 
     245    $("#firstp").bind("ajaxSuccess", function (e, xml, s) { 
     246        var re = /_=(.*?)(&|$)/g; 
     247    var oldOne = null; 
     248        for (var i = 0; i < 6; i++) { 
     249      var ret = re.exec(s.url); 
     250            if (!ret) { 
     251                break; 
     252            } 
     253      oldOne = ret[1]; 
     254        } 
     255        equals(i, 1, "Test to make sure only one 'no-cache' parameter is there"); 
     256        ok(oldOne != "tobereplaced555", "Test to be sure parameter (if it was there) was replaced"); 
     257        if(++count == 6) 
     258            start(); 
     259    }); 
     260 
     261    ok( $.ajax({url: "data/text.php", cache:false}), "test with no parameters" ); 
     262    ok( $.ajax({url: "data/text.php?pizza=true", cache:false}), "test with 1 parameter" ); 
     263    ok( $.ajax({url: "data/text.php?_=tobereplaced555", cache:false}), "test with _= parameter" ); 
     264    ok( $.ajax({url: "data/text.php?pizza=true&_=tobereplaced555", cache:false}), "test with 1 parameter plus _= one" ); 
     265    ok( $.ajax({url: "data/text.php?_=tobereplaced555&tv=false", cache:false}), "test with 1 parameter plus _= one before it" ); 
     266    ok( $.ajax({url: "data/text.php?name=David&_=tobereplaced555&washere=true", cache:false}), "test with 2 parameters surrounding _= one" ); 
    237267}); 
    238268