How can I force [removed] to make an HTTP request instead of using the cache?

后端 未结 5 1723
难免孤独
难免孤独 2021-01-11 17:18

In my web application I am setting window.location to navigate to a different page, but for some reason Firefox shows an old version of that page.

Usin

5条回答
  •  天涯浪人
    2021-01-11 18:10

    You have to validate if there is any existing query parameter in the url. If any query parameter exist, you should append the timestamp using "&". I have written a quick snippet that might be of your help.

    window.newLocation = function( location ) {
        var newLocation = ( typeof location === "string" ) ? location : window.location.href,
           appendType = ( newLocation.indexOf("?") < 0 ) ? "?" : "&";
        window.location = newLocation + appendType + "_t=" + (new Date()).getTime();
    }
    

    Usage:

    newLocation("index.html") or window.newLocation("index.html") 
    // opens "index.html?_t="
    
    newLocation("index.html?existingQuery=true") or window.newLocation("index.html?existingQuery=true") 
    // opens "index.html?existingQuery=true&_t=

    You could further modify the snippet to remove exiting timestamp in the query parameter to avoid duplication

提交回复
热议问题