The past present and future, all at your command with HTML5

November 8th, 2011 No comments

Dear Diary,

With the onset of HTML5 becoming increasingly supported among the many browsers out there, there are going to be a number of important changes coming to the way things work. Things such as… the MathML and SVG support, a large number of new elements and deprecation of some older obsolete ones, and most importantly (for the purposes of this article) an important change to the history API functionality!

Unfortunately, HTML5 isn’t supported in every browser… largely because older versions of browser either can’t support it or just aren’t being updated to support it. People should update their browser anyway… so many unnecessary headaches caused by having to go back and debug some random old version of IE or Firefox because someone is reluctant to update. I will say, props to Chrome for silently updating itself to the latest version and indicating that a core app restart is required with those little coloured arrows!

Due to this lack of support in all browser, *glares at IE*, another solution is required to properly combat this looming headache menace! One of the best tools at our disposal as web developers is the History.js HTML5/HTML4 wrapper.

You can find it here: https://github.com/balupton/history.js
…and a demo here: http://balupton.github.com/history.js/demo/

Straight from the description on github: “History.js gracefully supports the HTML5 History/State APIs (pushState, replaceState, onPopState) in all browsers. Including continued support for data, titles, replaceState.”

The key thing that the History.js adds is complete cross browser support for history behaviour on all websites. Of course, browsers that support HTML5 have a much nicer experience, but it still works very neatly in general. Thankfully, it also includes plugins for major javascript frameworks, including jQuery, Mootools and Prototype.

An implementation using History.js

One of the tasks I was working on at work was to transition an aging web application which was still using a frameset for it’s layout into a single-page application. The catch was that part of the page load was an EXTREMELY heavy client side javascript menu. As a result, simply loading the entire page each time while navigating around the application was a bit of a performance issue, resulting in page load times nearly doubled.

My solution was to implement a chromeless loading system based around caching pages in the DOM and utilizing HTML5 history API calls wherever possible to allow for fast page transitions going back and forward in the browser.

I quickly found that HTML5 wasn’t fully supported (as expected), and also that it there was still inconsistent behaviour between different browsers. After a bit of searching around online, I came across History.js, realized its potential, and began implementing it into the application.

The basic idea behind what I wanted to do was keep enough meta information about each page the user navigated to such that I could determine if it was either cached already and could be swapped in, or if it needed to be requested and cached again. This was particularly important, because closing the browser would typically cause you to lose your history for that session, but it is possible to keep track of your history even between browser loads with History.js.

As a result, the general behaviour was to to either push a new page on to the history when performing new navigation, replace an existing cached page when a forced load was required (stale data or no longer cached due to browser reload), and wrap normal browser back and forward with a history state change event.

I actually found a funny little bug while working on this solution (which may be fixed now, I haven’t bothered to confirm), where if you are using the amplify.store.js storage solution for page caching via History.js, after 1000 page loads it would simply go into an infinite loop because it couldn’t find the next “unique” page identifier to use. Page identifiers were generated by selecting a number at random between 1 and 1000… and of course testing this on development, I would constantly be reloading pages and changing states… which lead to some fun times debugging the infinite loop.

Ultimately I had to skip using the storage system, and I implemented a version of it myself in the DOM itself. I like this solution better anyway, because it gave me direct control over what was cached and how it was manipulated more so than the storage system would.

At any rate, here are a few simple code snippets of how I was using History.js:

// set up history change event
var History = window.History;
History && History.Adapter.bind(window, 'statechange', function(){});
 
// pushing new state data (History.pushState and History.replaceState have identical syntax)
History.pushState({
	state: customState,
	timestamp: pageLoadTimestamp,
	page: pageKey,
	extraData: dataObject
}, pageTitle, pageUrl, false);
 
// state change event
var onHistoryStateChange = function(e){
	var HistoryState = History.getState();
 
	// access the custom data you've assigned to the history state
	// HistoryState .data.state;
	// HistoryState .data.timestamp;
	// HistoryState .data.page;
	//
	// handle state change by either loading new page or loading cached page etc ...
}

And lastly for more reading about manipulating the browser history, Mozilla Dev has a nice article that I found useful on it here:

https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history

Your friend,
Jeff

Books I’ve recently read

August 1st, 2011 2 comments

Home Fires by Gene Wolfe

While it wasn’t quite what I’d expected, that is very much a good thing. I enjoyed it.

If you’re looking for something a little science-fiction-y and a little adventure-y you might enjoy this one, especially if you take to the writing styles of Gene Wolfe (which I tend to). I would recommend it.

 

Harry Potter (all seven)

Apparently after they made the sixth movie, somone decided to write some books! At least that’s how it seemed to me, as I read all seven within this last month. There’s a good chance you’ve read these and that’s now true of me as well!

It did take me about three books to get into the series, but in the end I enjoyed it and even now miss my friends Ron and Hermoine a little (not so much Harry, I never truly considered him a friend).

There are things I liked and things I didn’t, though that is true of all books but the truly and completely terrible. I would recommend them, though with some qualifications.

 


The Sorcerer’s House by Gene Wolfe

More fantastic than future-set, I’d recommend it for anyone looking for that set in present-day. It features the same sort of protagonist I’ve become familiar with from some of his other works.

 

 


Rant: An Oral Biography of Buster Casey by Chuck Palahniuk

A friend and I agreed to read each other’s recommendations, and this was one of hers. It was interesting, and I might recommend it (though with more than a few qualifications).

 

 


Lightning by Dean Koontz

Another book that she recommended; though fine, it really wasn’t my style.

 

 

 

But what to read next?

Categories: books, Personal Tags:

PHP autoload and smarty 3

July 25th, 2011 No comments

I was setting up for some code today. Code which would use, among other things, Smarty 3 since that’s a thing now apparently. (It used to be Smarty 2, but I guess times change and numbers change.)

There’s an __autoload function that I wrote a while ago and often use to, you know, automatically include files when instantiating classes. But it didn’t work at all.
The file itself with the __autoload function included fine, but autoload function was not being called. It was pretty confusing, to be honest.

Jeff and I eventually traced this back to one line in my code — the line including the Smarty source file.

So I had a look in there and found this:

/**
 * register the class autoloader
 */
if (!defined('SMARTY_SPL_AUTOLOAD')) {
    define('SMARTY_SPL_AUTOLOAD', 0);
}
 
if (SMARTY_SPL_AUTOLOAD && set_include_path(get_include_path() . PATH_SEPARATOR . SMARTY_SYSPLUGINS_DIR) !== false) {
    $registeredAutoLoadFunctions = spl_autoload_functions();
    if (!isset($registeredAutoLoadFunctions['spl_autoload'])) {
        spl_autoload_register();
    }
} else {
    spl_autoload_register('smartyAutoload');
}

Which looked suspiciously culpable. So I added this line to my code:

spl_autoload_register('__autoload');

And then it worked!

youtube powered video gallery and isometry

June 29th, 2011 1 comment

Are those two related? They may not be. (They might be; who knows?)

Even so, I wanted to try out some isometry. You know, get some stuff displaying and sorting. That sort of thing? Big news: I did it! :D

 

That’s all for isometry, on to youtube powered video galleries!



  • http://www.youtube.com/watch?v=L5JHMpLIqO4
  • http://www.youtube.com/watch?v=VG82USg5mtE
  • http://www.youtube.com/watch?v=-jMruFHTwrY
  • http://www.youtube.com/watch?v=zxK8y7XuEu8
  • http://www.youtube.com/watch?v=ADBKdSCbmiM
  • http://www.youtube.com/watch?v=28sAsvzMW6s
  • http://www.youtube.com/watch?v=V-24m-KRkn0

 

That’s one I made quickly. It works like this:

First you have some HTML.

<ul class="video library">
	<li>http://www.youtube.com/watch?v=L5JHMpLIqO4</li>
	<li>http://www.youtube.com/watch?v=VG82USg5mtE</li>
	<li>http://www.youtube.com/watch?v=-jMruFHTwrY</li>	
	<li>http://www.youtube.com/watch?v=zxK8y7XuEu8</li>	
	<li>http://www.youtube.com/watch?v=ADBKdSCbmiM</li>	
	<li>http://www.youtube.com/watch?v=28sAsvzMW6s</li>	
	<li>http://www.youtube.com/watch?v=V-24m-KRkn0</li>
</ul>

 

Just throw the Youtube URLs into a list and add the classes video and library to it, and that’s all there is to the HTML! (Well, there’s some minor javascript to include).

 

<link href="http://www.gigglingcorpse.com/dev/video-gallery/style.css" rel="stylesheet" type="text/css" media="screen" />
<script src="http://www.gigglingcorpse.com/dev/video-gallery/video-library.js" type="text/javascript"></script> 
<script src="http://www.google.com/jsapi" type="text/javascript"></script> 
<script type="text/javascript"> 
		google.load("jquery", "1");
		google.setOnLoadCallback(function() {
			videofy();
		});		
</script>

 

I included the Javascript file for you know doing stuff, as well as CSS file for some basic formatting. It makes use of JQuery, which i’ve included by way of Google’s JsAPI (out of laziness). And then when everything has loaded, there’s the one call to videofy().

After that, the list gets converted into the video gallery you should see above!

Thank you for your time.

Categories: Dev, Uncategorized Tags: