2

Im working on bookmarklets and my issue is js not getting update

I load js file with my bookmarklet, And my problem is its not getting update with my latest version. even also if i have updated it in my database.

So what i want it to check and load latest version when ever bookmark is clicked. so i want to know if there something to add in js file so it get load latest version. Or if there is way to check date and time or check version. etc

I have no problem with codes as they working fine.

Suppose this is my js

function start() {
    // codes
};


//  ---------------------------------------
//  Ajax
//  ---------------------------------------
function AJAXInteraction(url, callback) {
    var req = init();
    req.onreadystatechange = processRequest;

    function init() {
        if (window.XMLHttpRequest) {
            return new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    function processRequest() {
        if (req.readyState == 4) {
            if (req.status == 200) {
                if (callback) callback(req.responseText);
            }
        }
    }
    this.doGet = function () {
        req.open("GET", url, true);
        req.send(null);
    }
    this.doPost = function (str) {
        req.open("POST", url, true);
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        req.send(str);
    }
};
deerox
  • 1,045
  • 3
  • 14
  • 28
  • out of curiosity, why are you using a mix of sub and dot notation to access object properties? (i.e.: using toolbox['innerHTML'] instead of toolbox.innerHTML, but ajax.doPost instead of ajax['doPost']). – MicronXD Sep 24 '12 at 18:40
  • You have a syntax error, unterminated string, which comes from here: `var postdata = "action=buy&" + value + "&" + price + ";` Simply remove the last `+ "` – Zirak Sep 24 '12 at 19:00
  • I have no problem with codes. as i said. only want my js file get fresh copy everytime i load it – deerox Sep 25 '12 at 04:00

1 Answers1

2

If you are appending a JS file to the page and you want to force your browser to always get a fresh copy of the file and not use the cache, then you can append any random text as part of the query string. Here is example code.

document.body.appendChild(document.createElement('script')).src = 'http://server.com/script.js?ts=' + new Date().getTime();

Alternatively, the server from which the file is being loaded could specify certain expiration information. To ensure you always get the latest, you could specify an expiration of sometime in the past.

Take a look at this: Setup HTTP expires headers using PHP and Apache

Community
  • 1
  • 1
DG.
  • 3,417
  • 2
  • 23
  • 28
  • so bookmark will look like this : `javascript:(function(){document.body.appendChild(document.createElement('script')).src = 'http://server.com/script.js?ts='+new Date().getTime();})();` – deerox Sep 25 '12 at 05:31