Don’t Use jquery-latest.js!!!!!!!1

I’m guilty.

So guilty.

For years I have pointed pages to jquery-latest.js. Of course, I don’t think anyone would classify hack.premo.biz as a “production site.” I just want the latest damn version of code. I like to experiment with new features.

But I hate babysitting blogs, downloading point releases, and uploading them to my web server. So I used jquery-latest.js.

But now jQuery has taken this away. Sort of. jquery-latest.js is now frozen in time at version 1.11.1.

Well that’s no good. I can’t putter around on an old version for the rest of my life. But I hate babysitting blogs. And downloading point releases. And uploading them to my web server.

What’s a modern coder to do?

Scrape that s.

I made a two part solution. Part one scans the file structure on includes.premo.biz for files that look like jquery 2.x and returns the newest one it finds. It also sends a custom header so the browser reads it as JavaScript. I’m not going to give the URL here because I don’t want to be the next jquery-latest.js. But here’s the (PHP) code:


header( 'Content-Type: application/javascript' );
$contents = scandir( './' );
foreach ($contents as $file) {
if ($file !== '.' && $file !== '..') {
$ext = '';
$parts = explode('.', $file);
$ext = $parts[count($parts) - 1];
if ($ext == 'js') {
if ($parts[0] == 'jquery-2') {
include $file;
}
}
}
}

The second part is more complicated. It pulls in the jQuery Blog RSS feed, looks for a post about a new release, reads the version number, determines if it is newer than the version my server already has and if so pulls it down, then archives the older version. Here’s the PHP for that:


// run on cron.
// check jquery blog rss for updated version news
// http://blog.jquery.com/feed/
header('Content-Type: text/html; charset=UTF-8');
// what's the latest version we've got? (current?)
$ourVersion = '0';
$contents = scandir( './' );
foreach ($contents as $file) {
//if( strpos( $file,'.' ) !== 0 ) {
if ($file !== '.' && $file !== '..') {
$ext = '';
$parts = explode('.', $file);
$ext = $parts[count($parts) - 1];
if ($ext == 'js') {
if ($parts[0] == 'jquery-2') {
//include $file;
$ver = $file;
$ver = str_ireplace('jquery-', '', $ver);
$ver = str_ireplace('.min.js', '', $ver);
$ver = str_ireplace('.js', '', $ver);
$ourVersion = $ver;
}
}
}
}
echo 'ourVersion : ' . $ourVersion;
$rssString = file_get_contents('http://blog.jquery.com/feed/');
$xmle = simplexml_load_string($rssString);
//print_r($xmle);
$articles = $xmle->channel->item;
foreach ($articles as $article) {
$titleLength = strlen($article->title);
$check = trim(substr($article->title, (strlen($article->title) - 8)));
if (trim(substr($article->title, (strlen($article->title) - 8))) == 'Released') {
if (strstr($article->title, 'RC')) {
continue;
echo 'release candidate. BOO!';
}
if (stristr($article->title, 'beta')) {
continue;
echo 'beta. BOO!';
}
echo 'RELEASE POST';
echo $article->title;
// find '2.'
$words = explode(' ', $article->title);
$ver = '0';
foreach ($words as $word) {
if (substr($word, 0, 2) == '2.') {
$ver = $word;
if (strlen($ver) == 3) {
$ver = $ver . '.0';
}
echo 'version : ' . $ver;
if ($ver > $ourVersion) {
echo $ver . ' is greater than ' . $ourVersion . '!';
$jqFilename = 'jquery-' . $ver . '.min.js';
$jqUrl = 'http://code.jquery.com/' . $jqFilename;
echo $jqUrl;
// http://code.jquery.com/jquery-2.1.1.js
$jqueryContents = file_get_contents($jqUrl);
file_put_contents($jqFilename, $jqueryContents);
// move old version
rename('jquery-' . $ourVersion . '.min.js', './jquery_archive/jquery-' . $ourVersion . '.min.js');
break 2;
} else {
echo $ver . ' is NOT greater than ' . $ourVersion . '!';
}
}
}
echo '----------';
}
}

And that’s that. My very own jquery-latest.js.

HTML5 and jQuery

The last time I was unemployed, I put together Fav.Premo.biz.  Unemployed again, I’ve dipped into code all over the various projects I’ve got up and running.

With the exception of the main site (i.e., the page you’re reading), I’ve decided to change all of my sites from HTML4 or XHTML to HTML5.  In addition, I’ve decided that now is a good time to stop using Prototype and script.aculo.us and start using jQuery.

I’ve decided to move to HTML5 for two main reasons.  First, HTML5 is more elegant than HTML 4.x or XHTML 1.x.  I love elegance when it comes to programming.  I find that the criteria which determines what is and is not valid HTML5 code is less draconian than for HTML4.x and XHTML1.x.  Also, HTML5 introduces new features that allow developers to add greater functionality with hand-written markup.  Greater flexibility and greater functionality sound pretty elegant to me.  Second, due to these new features, HTML5 provides for greater use of open source technologies.  The long and short of my feelings in this area is that HTML5 gets us one step close to a world without Adobe Flash.  The <audio> and <video> tags allow developers to add rich content to sites without relying on a closed source, proprietary plugin that is a resource hog and a security risk.  Who couldn’t love code that’s more elegant combined with a better user experience?

I’ve decided to move to jQuery because jQuery is updated far more frequently than the JavaScript frameworks I’ve been using up until now — Prototype and script.aculo.us.  For example, jQuery 1.4 was released in January 2010 and jQuery 1.3 was released in January 2009.  When it comes to Prototype’s progression over that time, I can only estimate that it’s seen two bug releases (0.0.0.x) and one maintenance release (0.0.x).  Although putting out regular updates to a code base may be virtuous, it’s not the whole story.  Every time jQuery is updated, its release notes contain graphs showing speed improvements for all major browsers.  Speed improvements on an annual basis.  This just isn’t happening in Prototype or script.aculo.us.

I have a few public-facing sites that use JavaScript here and there, but only one makes heavy use of it: Fav.Premo.biz.  Since modifying code to use both HTML5 and jQuery would be painful at best, I’m going to rewrite Fav.Premo.biz using these new technologies.  It’ll be an undertaking, and it might not be finished soon, but it should be interesting.