Thursday 2 September 2010

Seperate and Different

I though I needed to split the functionality of some similar pages depending upon the name of the page... I should explain better! I have some pages that all do pretty much the same thing on a given set of data... but not all of the pages react in the same way to events. So I got to thinking about how I could branch the code depending upon which page was calling the JavaScript, so then I got to thinking about how to find the name of the page and came across this code:

var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);

But then that got far too complex and the JavaScript kept getting bigger and bigger and uglier and uglier so instead I had a different version of the same basic file for each page... but that got far to complicated in terms of remembering what I'd updated and where as I was developing... so I got the thinking about PHPs include() but JavaScript doesn't have one, it does however have fantastic DOM support so, thanks to a little research, I found this code from the really rather excellent Stephen Chapman. Now I've still got the separate JavaScript files for each page - but these pages are far smaller and concentrate on the differences rather than having to keep abreast of the similarities - but all the common functionality is kept in a separate file so that I don't need to hunt down each function in each file and update with the latest version. Cool ehh?

1 comment: