I finally took the time to write a Ajax-code necessary to pull random quote from an xml-file. I've been doing the very same for years using Perl-script, so the reason for all the trouble was to get my head around on how to use XMLHttpRequest, a cornerstone of Ajax. This isn't used at6 the moment though.
As a side product I started to ponder whether I could use the same technique to randomly pick an image. Well, to pick up just the image would be easy (doesn't really differ from randomly picking up the quote). What differs though, that in order to properly handle the image, I would need to have access image metadata as well (for caption) and size attributes. Given that I had earlier employed Exiftool to automatically generate xmp-sidecar files, I have that data available in xml-files. So basically what I needed to do was to
- Select random item from the list containing a list of files I want to rotate
- Request the xmp-file of the chosen image and extract the metadata
- Return properly formatted html to be written into specified div within the calling document
Sounds simple enough. And surprisingly, it didn't turn out to be much more complicated that it sounded. Well, in principle anyway. Once I figured out how to traverse the xml-structure returned as XMLHttpResponse, pulling off the functionality I was after in a separate proof-of-concept project was easier than anticipated. Xmp-files contain several namespaces, something that I fully expected to cause major headaches. Turns out they don't. Ok, so far so good.
My next step of action was trying to implement that in real-life scenario. Which is about the time when I ran into some peculiar functionality. Turns out I had tumbled on notorious domain restrictions of Ajax. Well, sort of anyway. Everything is part of the same domain, but trying to read from file using relative addressing so that I would have to go one directory level backwards just didn't seem to work. Which is a major PITA as I pretty much would have to do exactly that. After considerable amount of frustration and wasted time, I found out that this issue doesn't really exist; it is only encountered when not running the site on a web server.
That being said, I don't particularly like to run a local web server just for this very purpose. Luckily, it isn't too difficult to circumvent (well, few things are if you happen to know how to). In this particular case I found that in Firefox, you can bypass strict local file security policy by going into Firefox config panel (type 'about:config' to address bar) and set property "security.fileuri.strict_origin_policy" to 'false' by double clicking on it. That allows you to continue to access local files that are 'outside' of the directory structure you loaded from via XHR. IE seems to have the same limitation, not sure whether it can be circumvented somehow (and if it can, how). Neither Opera or Chrome seem to have such a restriction. Anyway, now that this is sorted, I'll probably tidy up the code, then implement it so that I can rotate images on mountain page.
When tinkering with that, I found out that this site causes quite a few CSS errors, which clutters browsers error console, thus making it more difficult to track down real issues. At least the majority, if not all, CSS issues are caused by hacks used to overcome and circumvent browser-related bugs. It is very likely, that I will get rid of those hacks in the near-future; if someone still used either IE 5.5 or old Netscape browsers, they are clearly gluttons for punishment anyway. The same probably goes for conditional comments used to target various older IE versions, they make the code cluttered and difficult to edit with some editors. Anyway, I moved some dirty IE hacks from the main CSS to IE targeted CSS-file, which minimizes the implications of the issue.