Beleiu Iulian
Beleiu Iulian

Reputation: 15

How to detect the new version of a Javascript file?

the problem is that I have a remote js file to include in a page and it has to be included the last version of it. To be more specific: The file is for example: http://example.com/javascript/v03/header.js The problem is that when they generate a new javascript file the url will be changed to http://example.com/javascript/v04/header.js for example.

The URL will be changed to a new version number.

My question is what will be the best/clean way to detect a new version of that file in Php?

Thank you!

Upvotes: 0

Views: 864

Answers (1)

Andreas
Andreas

Reputation: 23958

Untested but should work.
You file_get_contents the versions and when it returns false you have gone to far.

$link = "http://example.com/javascript/VERSION/header.js";

For($i=1; $i<100; $i++){
    $test= str_replace("VERSION", "v" . str_pad($i, 2, "0", STR_PAD_LEFT), $link);
    If(file_get_contents($test)){
        $latest = $test;
    }else{
        Break;
    }
}

Echo "latest version is " . $latest;

Then optionally you save the $i number somewhere and use that the next time you run it so that you don't need to recheck version 1-4 every time.

EDIT, noticed I had $str instead of $link.

Upvotes: 1

Related Questions