Asset.css claims to have an onload event in Mootools documentation, but sadly, not all browsers support this feature. Even worse, browsers like Chrome seem to support the event, but never fire it.
After some research and testing at work, we have come up with a solution that overrides Asset.css to detect when CSS gets loaded by the browser using the onload event in those browsers that support it (Internet Explorer and Opera) and doing polling in those that do not (Chrome, Safari and Firefox). Here is the code:
-
// Add callback support to Asset.css for all browsers
-
Asset.css = function(source, onLoad) {
-
var link = new Element('link', {
-
rel: 'stylesheet',
-
media: 'screen',
-
type: 'text/css',
-
href: source
-
});
-
-
if (("onload" in link) && !Browser.Engines.webkit()) {
-
if (onLoad) link.onload = onLoad;
-
} else {
-
(function() {
-
try {
-
link.sheet.cssRules;
-
} catch (e) {
-
setTimeout(arguments.callee, 100);
-
return;
-
};
-
if (onLoad) onLoad();
-
})();
-
}
-
-
link.inject(document.head);
-
return link;
-
}
-
-
// Load some CSS and show an alert when done
-
var mycss = new Asset.css('http://example.com/style.css', function() {
-
alert('CSS loaded!');
-
});
-
-
// Unload the css
-
mycss.destroy();
Enjoy!
I was watching one of the video tutorials from Apple last night about some custom JS available on Safari, only on the iPhone 3.0, that let’s you interact with the phone and obtain data like the current location, etc.
So I built a little page to see how it works. I included Mootools 1.2.3 from Google servers and the Google Maps API. And voilá, with a few lines of code we have a totally functional web based GSP navigation system. A little bit ugly, but functional. Currenlty it only centers the map on the coordinates given by the phone, and also does a reverse geolocation to obtain the address (actually city, state and country). But it could be easily extended to add markers to the map, or even paths to track the movement.
If you have an iPhone (or an iPhone simulator) go to:
http://enekoalonso.com/research/iphone/geolocation.html
The source code:
-
var Navigator = new Class({
-
initialize: function() {
-
-
// Listen for position changes
-
this.watchId = navigator.geolocation.watchPosition(
-
function(position){ this.position(position) }.bind(this),
-
function() {},
-
{ enableHighAccuracy: true, timeout: 1000, maximumAge: 10000 }
-
);
-
this.infoPanel = $('info');
-
-
// Create the Geocoder and the Map
-
this.geocoder = new google.maps.Geocoder();
-
this.map = new google.maps.Map($('map'), {
-
zoom: 12,
-
mapTypeId: google.maps.MapTypeId.ROADMAP
-
});
-
},
-
-
position: function(position) {
-
// Update labels
-
this.infoPanel.getElement('p.date').set('text', position.timestamp);
-
this.infoPanel.getElement('p.coords').set('text', position.coords.latitude + ', ' + position.coords.longitude);
-
this.infoPanel.getElement('p.altitude').set('text', position.coords.altitude);
-
this.infoPanel.getElement('p.heading').set('text', position.coords.heading);
-
this.infoPanel.getElement('p.speed').set('text', position.coords.speed);
-
-
// Center map
-
var latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
-
this.map.set_center(latLng);
-
-
// Reverse geolocation (get current address)
-
this.geocoder.geocode({'latLng': latLng},
-
function(results, status) {
-
if (google.maps.GeocoderStatus.OK) this.showAddress(results)
-
}.bind(this)
-
);
-
},
-
-
showAddress: function(results) {
-
// console.dir(response);
-
if (results[1]) {
-
this.infoPanel.getElement('p.location').set('text', results[1].formatted_address);
-
}
-
}
-
});
-
-
window.addEvent('load', function() {
-
new Navigator();
-
});
Enjoy.
I’ve been using Safari both at home and work since last Thursday when I first knew it had been released and it looks very fast and solid. They have finally added some standard keyboard shortcuts, like Ctrl+Tab and now you can restore your previous session (all pages open) the next time you open the browser (although not automatically like Firefox).
It looks very good for web development too, since it has the Javascript console integrated and you can do direct changes on the DOM and the CSS to preview how it would look. But I think Firebug is still ahead and I’m very used to it, so I’m not ready to completely stop using Firefox.
I have nothing against Firefox, which has been my favorite browser for years, but the fact that it is very, very slow. I’ll say today is the slowest browser out there. Or at least it looks like that sometimes. The only extension I have is Firebug. I’ve tried removing the cache, the history, etc. with no luck.
So yeah, so far I’m very happy with Safari 4.
