<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom"><title>Simon Willison's Weblog: unobtrusive-javascript</title><link href="http://feeds.simonwillison.net/" rel="alternate"/><link href="http://feeds.simonwillison.net/tags/unobtrusive-javascript.atom" rel="self"/><id>http://feeds.simonwillison.net/</id><updated>2008-08-29T01:48:30+00:00</updated><author><name>Simon Willison</name></author><entry><title>Capital Radio's London Guide</title><link href="https://simonwillison.net/2008/Aug/29/londonguide/" rel="alternate"/><published>2008-08-29T01:48:30+00:00</published><updated>2008-08-29T01:48:30+00:00</updated><id>https://simonwillison.net/2008/Aug/29/londonguide/</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://www.capitalradio.co.uk/london-guide/"&gt;Capital Radio&amp;#x27;s London Guide&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Worth pointing out: the search / map interface on this page is one of the best examples of progressive enhancement I’ve ever seen. Try disabling JavaScript and see what happens. It seems like most developers just can’t be bothered with this kind of attention to detail these days, which disappoints me.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/accessibility"&gt;accessibility&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/capitalradio"&gt;capitalradio&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/google-maps"&gt;google-maps&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/londonguide"&gt;londonguide&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/progressive-enhancement"&gt;progressive-enhancement&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/unobtrusive-javascript"&gt;unobtrusive-javascript&lt;/a&gt;&lt;/p&gt;



</summary><category term="accessibility"/><category term="capitalradio"/><category term="google-maps"/><category term="javascript"/><category term="londonguide"/><category term="progressive-enhancement"/><category term="unobtrusive-javascript"/></entry><entry><title>Is It OK to Require JavaScript?</title><link href="https://simonwillison.net/2008/Jun/10/ok/" rel="alternate"/><published>2008-06-10T06:41:34+00:00</published><updated>2008-06-10T06:41:34+00:00</updated><id>https://simonwillison.net/2008/Jun/10/ok/</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://blog.stackoverflow.com/index.php/2008/06/is-it-ok-to-require-javascript/"&gt;Is It OK to Require JavaScript?&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Not if you can avoid doing so. Unobtrusive JavaScript really isn’t hard if you design it in from the start, and since stackoverflow is a community forum / questions and answers site I have trouble imagining a feature that can’t be made to work without JavaScript.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/jeff-atwood"&gt;jeff-atwood&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/stackoverflow"&gt;stackoverflow&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/unobtrusive-javascript"&gt;unobtrusive-javascript&lt;/a&gt;&lt;/p&gt;



</summary><category term="javascript"/><category term="jeff-atwood"/><category term="stackoverflow"/><category term="unobtrusive-javascript"/></entry><entry><title>Unobtrusive JavaScript with jQuery</title><link href="https://simonwillison.net/2008/May/6/unobtrusive/" rel="alternate"/><published>2008-05-06T18:53:33+00:00</published><updated>2008-05-06T18:53:33+00:00</updated><id>https://simonwillison.net/2008/May/6/unobtrusive/</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://simonwillison.net/static/2008/xtech/"&gt;Unobtrusive JavaScript with jQuery&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
The online handout for the tutorial I gave this morning at XTech.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/handout"&gt;handout&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/jquery"&gt;jquery&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/unobtrusive-javascript"&gt;unobtrusive-javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/xtech"&gt;xtech&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/xtech08"&gt;xtech08&lt;/a&gt;&lt;/p&gt;



</summary><category term="handout"/><category term="javascript"/><category term="jquery"/><category term="unobtrusive-javascript"/><category term="xtech"/><category term="xtech08"/></entry><entry><title>Embedding custom non-visible data in HTML 5</title><link href="https://simonwillison.net/2008/Apr/19/html/" rel="alternate"/><published>2008-04-19T22:58:23+00:00</published><updated>2008-04-19T22:58:23+00:00</updated><id>https://simonwillison.net/2008/Apr/19/html/</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://www.w3.org/html/wg/html5/#embedding"&gt;Embedding custom non-visible data in HTML 5&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
“Every HTML element may have any number of attributes starting with the string ’data-’ specified, with any value.”—this will be incredibly useful for unobtrusive JavaScript where there’s no sensible place to store configuration data as HTML content. It will also mean Dojo has an approved method for adding custom attributes to declaratively instantiate Dojo widgets.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/customattributes"&gt;customattributes&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/dojo"&gt;dojo&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/html"&gt;html&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/html5"&gt;html5&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/standards"&gt;standards&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/unobtrusive-javascript"&gt;unobtrusive-javascript&lt;/a&gt;&lt;/p&gt;



</summary><category term="customattributes"/><category term="dojo"/><category term="html"/><category term="html5"/><category term="javascript"/><category term="standards"/><category term="unobtrusive-javascript"/></entry><entry><title>Unobtrusively Mapping Microformats with jQuery</title><link href="https://simonwillison.net/2007/Dec/12/unobtrusively-mapping-microformats-jquery/" rel="alternate"/><published>2007-12-12T04:05:45+00:00</published><updated>2007-12-12T04:05:45+00:00</updated><id>https://simonwillison.net/2007/Dec/12/unobtrusively-mapping-microformats-jquery/</id><summary type="html">
    &lt;p&gt;&lt;a href="http://microformats.org/"&gt;Microformats&lt;/a&gt; are everywhere. You can&amp;#8217;t shake an electronic stick these days without accidentally poking a microformat-enabled site, and many developers use microformats as a matter of course. And why not? After all, why invent your own class names when you can re-use pre-defined ones that give your site extra functionality for free?&lt;/p&gt;

&lt;p&gt;Nevertheless, while it&amp;#8217;s good to know that users of tools such as &lt;a href="http://blog.codeeg.com/tails-firefox-extension-03/"&gt;Tails&lt;/a&gt; and &lt;a href="https://addons.mozilla.org/en-US/firefox/addon/4106"&gt;Operator&lt;/a&gt; will derive added value from your shiny semantics, it&amp;#8217;s nice to be able to reuse that effort in your own code.&lt;/p&gt;

&lt;p&gt;We&amp;#8217;re going to build a map of some of my favourite restaurants in Brighton. Fitting with the principles of &lt;a href="http://en.wikipedia.org/wiki/Unobtrusive_JavaScript"&gt;unobtrusive JavaScript&lt;/a&gt;, we&amp;#8217;ll start with a semantically marked up list of restaurants, then use JavaScript to add the map, look up the restaurant locations and plot them as markers.&lt;/p&gt;

&lt;p&gt;We&amp;#8217;ll be using a couple of powerful tools. The first is &lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;, a JavaScript library that is ideally suited for unobtrusive scripting. jQuery allows us to manipulate elements on the page based on their &lt;span class="caps"&gt;CSS&lt;/span&gt; selector, which makes it easy to extract information from microformats.&lt;/p&gt;

&lt;p&gt;The second is &lt;a href="http://mapstraction.com/"&gt;Mapstraction&lt;/a&gt;, introduced here by Andrew Turner &lt;a href="https://24ways.org/2007/get-to-grips-with-slippy-maps"&gt;a few days ago&lt;/a&gt;. We&amp;#8217;ll be using &lt;a href="http://code.google.com/apis/maps/" title="The Google Maps API"&gt;Google Maps&lt;/a&gt; in the background, but Mapstraction makes it easy to change to a different provider if we want to later.&lt;/p&gt;

&lt;h3&gt;Getting Started&lt;/h3&gt;

&lt;p&gt;We&amp;#8217;ll start off with a simple collection of microformatted restaurant details, representing my seven favourite restaurants in Brighton. The full, unstyled list can be seen in &lt;a href="https://24ways.org/examples/unobtrusively-mapping-microformats-with-jquery/restaurants-plain.html"&gt;restaurants-plain.html&lt;/a&gt;. Each restaurant listing looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;li class=&amp;quot;vcard&amp;quot;&amp;gt;
	&amp;lt;h3&amp;gt;&amp;lt;a class=&amp;quot;fn org url&amp;quot; href=&amp;quot;http://www.riddleandfinns.co.uk/&amp;quot;&amp;gt;Riddle &amp;amp; Finns&amp;lt;/a&amp;gt;&amp;lt;/h3&amp;gt;
	&amp;lt;div class=&amp;quot;adr&amp;quot;&amp;gt;
		&amp;lt;p class=&amp;quot;street-address&amp;quot;&amp;gt;12b Meeting House Lane&amp;lt;/p&amp;gt;
		&amp;lt;p&amp;gt;&amp;lt;span class=&amp;quot;locality&amp;quot;&amp;gt;Brighton&amp;lt;/span&amp;gt;, &amp;lt;abbr class=&amp;quot;country-name&amp;quot; title=&amp;quot;United Kingdom&amp;quot;&amp;gt;UK&amp;lt;/abbr&amp;gt;&amp;lt;/p&amp;gt;
		&amp;lt;p class=&amp;quot;postal-code&amp;quot;&amp;gt;BN1 1HB&amp;lt;/p&amp;gt;
	&amp;lt;/div&amp;gt;
	&amp;lt;p&amp;gt;Telephone: &amp;lt;span class=&amp;quot;tel&amp;quot;&amp;gt;+44 (0)1273 323 008&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;
	&amp;lt;p&amp;gt;E-mail: &amp;lt;a href=&amp;quot;mailto:info@riddleandfinns.co.uk&amp;quot; class=&amp;quot;email&amp;quot;&amp;gt;info@riddleandfinns.co.uk&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Since we&amp;#8217;re dealing with a list of restaurants, each hCard is marked up inside a list item. Each restaurant is an organisation; we signify this by placing the classes &lt;code&gt;fn&lt;/code&gt; and &lt;code&gt;org&lt;/code&gt; on the element surrounding the restaurant&amp;#8217;s name (according to the hCard spec, setting both &lt;code&gt;fn&lt;/code&gt; and &lt;code&gt;org&lt;/code&gt; to the same value signifies that the hCard represents an organisation rather than a person).&lt;/p&gt;

&lt;p&gt;The address information itself is contained within a &lt;code&gt;div&lt;/code&gt; of class &lt;code&gt;adr&lt;/code&gt;. Note that the &lt;span class="caps"&gt;HTML&lt;/span&gt; &lt;code&gt;&amp;lt;address&amp;gt;&lt;/code&gt; element is not suitable here for two reasons: firstly, it is intended to mark up contact details for the current document rather than generic addresses; secondly, address is an inline element and as such cannot contain the paragraphs elements used here for the address information.&lt;/p&gt;

&lt;p&gt;A nice thing about microformats is that they provide us with automatic hooks for our styling. For the moment we&amp;#8217;ll just tidy up the whitespace a bit; for more advanced style tips consult &lt;a href="https://24ways.org/2006/styling-hcards-with-css" title="Styling hCards with CSS"&gt;John Allsop&amp;#8217;s guide&lt;/a&gt; from 24 ways 2006.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;.vcard p {
	margin: 0;
}
.adr {
	margin-bottom: 0.5em;
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To plot the restaurants on a map we&amp;#8217;ll need latitude and longitude for each one. We can find this out from their address using &lt;em&gt;geocoding&lt;/em&gt;. Most mapping &lt;span class="caps"&gt;API&lt;/span&gt;s include support for geocoding, which means we can pass the &lt;span class="caps"&gt;API&lt;/span&gt; an address and get back a latitude/longitude point. Mapstraction provides an abstraction layer around these &lt;span class="caps"&gt;API&lt;/span&gt;s which can be included using the following script tag:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://mapstraction.com/src/mapstraction-geocode.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;While we&amp;#8217;re at it, let&amp;#8217;s pull in the other external scripts we&amp;#8217;ll be using:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;jquery-1.2.1.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src=&amp;quot;http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;key=YOUR_KEY&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://mapstraction.com/src/mapstraction.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://mapstraction.com/src/mapstraction-geocode.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&amp;#8217;s everything set up: let&amp;#8217;s write some JavaScript!&lt;/p&gt;

&lt;p&gt;In jQuery, almost every operation starts with a call to the jQuery function. The function simulates &lt;a href="http://en.wikipedia.org/wiki/Method_overloading"&gt;method overloading&lt;/a&gt; to behave in different ways depending on the arguments passed to it. When writing unobtrusive JavaScript it&amp;#8217;s important to set up code to execute when the page has loaded to the point that the &lt;span class="caps"&gt;DOM&lt;/span&gt; is available to be manipulated. To do this with jQuery, pass a callback function to the jQuery function itself:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;jQuery(function() {
	// This code will be executed when the DOM is ready
});&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Initialising the map&lt;/h3&gt;

&lt;p&gt;The first thing we need to do is initialise our map. Mapstraction needs a div with an explicit width, height and ID to show it where to put the map. Our document doesn&amp;#8217;t currently include this markup, but we can insert it with a single line of jQuery code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;jQuery(function() {
	// First create a div to host the map
	var themap = jQuery(&amp;#039;&amp;lt;div id=&amp;quot;themap&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&amp;#039;).css({
		&amp;#039;width&amp;#039;: &amp;#039;90%&amp;#039;,
		&amp;#039;height&amp;#039;: &amp;#039;400px&amp;#039;
	}).insertBefore(&amp;#039;ul.restaurants&amp;#039;);
});&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;While this is technically just a single line of JavaScript (with line-breaks added for readability) it&amp;#8217;s actually doing quite a lot of work. Let&amp;#8217;s break it down in to steps:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var themap = jQuery(&amp;#039;&amp;lt;div id=&amp;quot;themap&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&amp;#039;)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here&amp;#8217;s jQuery&amp;#8217;s method overloading in action: if you pass it a string that starts with a &lt;code&gt;&amp;lt;&lt;/code&gt; it assumes that you wish to create a new &lt;span class="caps"&gt;HTML&lt;/span&gt; element. This provides us with a handy shortcut for the more verbose &lt;span class="caps"&gt;DOM&lt;/span&gt; equivalent:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var themap = document.createElement(&amp;#039;div&amp;#039;);
themap.id = &amp;#039;themap&amp;#039;;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Next we want to apply some &lt;span class="caps"&gt;CSS&lt;/span&gt; rules to the element. jQuery supports chaining, which means we can continue to call methods on the object returned by jQuery or any of its methods:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var themap = jQuery(&amp;#039;&amp;lt;div id=&amp;quot;themap&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&amp;#039;).css({
	&amp;#039;width&amp;#039;: &amp;#039;90%&amp;#039;,
	&amp;#039;height&amp;#039;: &amp;#039;400px&amp;#039;
})&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Finally, we need to insert our new &lt;span class="caps"&gt;HTML&lt;/span&gt; element in to the page. jQuery provides a number of methods for element insertion, but in this case we want to position it directly before the &lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt; we are using to contain our restaurants. jQuery&amp;#8217;s &lt;code&gt;insertBefore()&lt;/code&gt; method takes a &lt;span class="caps"&gt;CSS&lt;/span&gt; selector indicating an element already on the page and places the current jQuery selection directly before that element in the &lt;span class="caps"&gt;DOM&lt;/span&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var themap = jQuery(&amp;#039;&amp;lt;div id=&amp;quot;themap&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&amp;#039;).css({
	&amp;#039;width&amp;#039;: &amp;#039;90%&amp;#039;,
	&amp;#039;height&amp;#039;: &amp;#039;400px&amp;#039;
}).insertBefore(&amp;#039;ul.restaurants&amp;#039;);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Finally, we need to initialise the map itself using Mapstraction. The Mapstraction constructor takes two arguments: the first is the ID of the element used to position the map; the second is the mapping provider to use (in this case &lt;code&gt;google&lt;/code&gt; ):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Initialise the map
var mapstraction = new Mapstraction(&amp;#039;themap&amp;#039;,&amp;#039;google&amp;#039;);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We want the map to appear centred on Brighton, so we&amp;#8217;ll need to know the correct co-ordinates. We can use &lt;a href="http://www.getlatlon.com/"&gt;www.getlatlon.com&lt;/a&gt; to find both the co-ordinates and the initial map zoom level.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Show map centred on Brighton
mapstraction.setCenterAndZoom(
	new LatLonPoint(50.82423734980143, -0.14007568359375),
	15 // Zoom level appropriate for Brighton city centre
);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We also want controls on the map to allow the user to zoom in and out and toggle between map and satellite view.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mapstraction.addControls({
	zoom: &amp;#039;large&amp;#039;,
	map_type: true
});&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Adding the markers&lt;/h3&gt;

&lt;p&gt;It&amp;#8217;s finally time to parse some microformats. Since we&amp;#8217;re using hCard, the information we want is wrapped in elements with the class &lt;code&gt;vcard&lt;/code&gt;. We can use jQuery&amp;#8217;s &lt;span class="caps"&gt;CSS&lt;/span&gt; selector support to find them:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var vcards = jQuery(&amp;#039;.vcard&amp;#039;);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now that we&amp;#8217;ve found them, we need to create a marker for each one in turn. Rather than using a regular JavaScript for loop, we can instead use jQuery&amp;#8217;s &lt;code&gt;each()&lt;/code&gt; method to execute a function against each of the hCards.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;jQuery(&amp;#039;.vcard&amp;#039;).each(function() {
	// Do something with the hCard
});&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Within the callback function, &lt;code&gt;this&lt;/code&gt; is set to the current &lt;span class="caps"&gt;DOM&lt;/span&gt; element (in our case, the list item). If we want to call the magic jQuery methods on it we&amp;#8217;ll need to wrap it in another call to jQuery:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;jQuery(&amp;#039;.vcard&amp;#039;).each(function() {
	var hcard = jQuery(this);
});&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The Google maps geocoder seems to work best if you pass it the street address and a postcode. We can extract these using &lt;span class="caps"&gt;CSS&lt;/span&gt; selectors: this time, we&amp;#8217;ll use jQuery&amp;#8217;s &lt;code&gt;find()&lt;/code&gt; method which searches within the current jQuery selection:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var streetaddress = hcard.find(&amp;#039;.street-address&amp;#039;).text();
var postcode = hcard.find(&amp;#039;.postal-code&amp;#039;).text();&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;text()&lt;/code&gt; method extracts the text contents of the selected node, minus any &lt;span class="caps"&gt;HTML&lt;/span&gt; markup.&lt;/p&gt;

&lt;p&gt;We&amp;#8217;ve got the address; now we need to geocode it. Mapstraction&amp;#8217;s geocoding &lt;span class="caps"&gt;API&lt;/span&gt; requires us to first construct a &lt;code&gt;MapstractionGeocoder&lt;/code&gt;, then use the &lt;code&gt;geocode()&lt;/code&gt; method to pass it an address. Here&amp;#8217;s the code outline:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var geocoder = new MapstractionGeocoder(onComplete, &amp;#039;google&amp;#039;);
geocoder.geocode({&amp;#039;address&amp;#039;: &amp;#039;the address goes here&amp;#039;);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;onComplete&lt;/code&gt; function is executed when the geocoding operation has been completed, and will be passed an object with the resulting point on the map. We just want to create a marker for the point:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var geocoder = new MapstractionGeocoder(function(result) {
	var marker = new Marker(result.point);
	mapstraction.addMarker(marker);
}, &amp;#039;google&amp;#039;);   &lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For our purposes, joining the street address and postcode with a comma to create the address should suffice:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;geocoder.geocode({&amp;#039;address&amp;#039;: streetaddress + &amp;#039;, &amp;#039; + postcode});   &lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There&amp;#8217;s one last step: when the marker is clicked, we want to display details of the restaurant. We can do this with an info bubble, which can be configured by passing in a string of &lt;span class="caps"&gt;HTML&lt;/span&gt;. We&amp;#8217;ll construct that &lt;span class="caps"&gt;HTML&lt;/span&gt; using jQuery&amp;#8217;s &lt;code&gt;html()&lt;/code&gt; method on our &lt;code&gt;hcard&lt;/code&gt; object, which extracts the &lt;span class="caps"&gt;HTML&lt;/span&gt; contained within that &lt;span class="caps"&gt;DOM&lt;/span&gt; node as a string.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var marker = new Marker(result.point);
marker.setInfoBubble(
	&amp;#039;&amp;lt;div class=&amp;quot;bubble&amp;quot;&amp;gt;&amp;#039; + hcard.html() + &amp;#039;&amp;lt;/div&amp;gt;&amp;#039;
);
mapstraction.addMarker(marker);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We&amp;#8217;ve wrapped the bubble in a div with class &lt;code&gt;bubble&lt;/code&gt; to make it easier to style. Google Maps can behave strangely if you don&amp;#8217;t provide an explicit width for your info bubbles, so we&amp;#8217;ll add that to our &lt;span class="caps"&gt;CSS&lt;/span&gt; now:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;.bubble {
	width: 300px;
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&amp;#8217;s everything we need: let&amp;#8217;s combine our code together:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;jQuery(function() {
	// First create a div to host the map
	var themap = jQuery(&amp;#039;&amp;lt;div id=&amp;quot;themap&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&amp;#039;).css({
		&amp;#039;width&amp;#039;: &amp;#039;90%&amp;#039;,
		&amp;#039;height&amp;#039;: &amp;#039;400px&amp;#039;
	}).insertBefore(&amp;#039;ul.restaurants&amp;#039;);
	// Now initialise the map
	var mapstraction = new Mapstraction(&amp;#039;themap&amp;#039;,&amp;#039;google&amp;#039;);
	mapstraction.addControls({
		zoom: &amp;#039;large&amp;#039;,
		map_type: true
	});
	// Show map centred on Brighton
	mapstraction.setCenterAndZoom(
		new LatLonPoint(50.82423734980143, -0.14007568359375),
		15 // Zoom level appropriate for Brighton city centre
	);
	// Geocode each hcard and add a marker
	jQuery(&amp;#039;.vcard&amp;#039;).each(function() {
		var hcard = jQuery(this);
		var streetaddress = hcard.find(&amp;#039;.street-address&amp;#039;).text();
		var postcode = hcard.find(&amp;#039;.postal-code&amp;#039;).text();
		var geocoder = new MapstractionGeocoder(function(result) {
			var marker = new Marker(result.point);
			marker.setInfoBubble(
				&amp;#039;&amp;lt;div class=&amp;quot;bubble&amp;quot;&amp;gt;&amp;#039; + hcard.html() + &amp;#039;&amp;lt;/div&amp;gt;&amp;#039;
			);
			mapstraction.addMarker(marker);
		}, &amp;#039;google&amp;#039;);	 
		geocoder.geocode({&amp;#039;address&amp;#039;: streetaddress + &amp;#039;, &amp;#039; + postcode});
	});
});&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here&amp;#8217;s the &lt;a href="https://24ways.org/examples/unobtrusively-mapping-microformats-with-jquery/restaurants.html"&gt;finished code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There&amp;#8217;s one last shortcut we can add: jQuery provides the &lt;code&gt;$&lt;/code&gt; symbol as an alias for jQuery. We could just go through our code and replace every call to &lt;code&gt;jQuery()&lt;/code&gt; with a call to &lt;code&gt;$()&lt;/code&gt;, but this would cause incompatibilities if we ever attempted to use our script on a page that also includes the Prototype library. A more robust approach is to start our code with the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;jQuery(function($) {
	// Within this function, $ now refers to jQuery
	// ...
});&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;jQuery cleverly passes itself as the first argument to any function registered to the &lt;span class="caps"&gt;DOM&lt;/span&gt; ready event, which means we can assign a local &lt;code&gt;$&lt;/code&gt; variable shortcut without affecting the &lt;code&gt;$&lt;/code&gt; symbol in the global scope. This makes it easy to &lt;a href="http://docs.jquery.com/Using_jQuery_with_Other_Libraries"&gt;use jQuery with other libraries&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Limitations of Geocoding&lt;/h3&gt;

&lt;p&gt;You may have noticed a discrepancy creep in to the last example: whereas my original list included seven restaurants, the geocoding example only shows five. This is because the Google Maps geocoder incorporates a rate limit: more than five lookups in a second and it starts returning error messages instead of regular results.&lt;/p&gt;

&lt;p&gt;In addition to this problem, geocoding itself is an inexact science: while UK postcodes generally get you down to the correct street, figuring out the exact point on the street from the provided address usually isn&amp;#8217;t too accurate (although Google do a pretty good job).&lt;/p&gt;

&lt;p&gt;Finally, there&amp;#8217;s the performance overhead. We&amp;#8217;re making five geocoding requests to Google for every page served, even though the restaurants themselves aren&amp;#8217;t likely to change location any time soon. Surely there&amp;#8217;s a better way of doing this?&lt;/p&gt;

&lt;p&gt;Microformats to the rescue (again)! The &lt;a href="http://microformats.org/wiki/geo"&gt;geo microformat&lt;/a&gt; suggests simple classes for including latitude and longitude information in a page. We can add specific points for each restaurant using the following markup:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;li class=&amp;quot;vcard&amp;quot;&amp;gt;
	&amp;lt;h3 class=&amp;quot;fn org&amp;quot;&amp;gt;E-Kagen&amp;lt;/h3&amp;gt;
	&amp;lt;div class=&amp;quot;adr&amp;quot;&amp;gt;
		&amp;lt;p class=&amp;quot;street-address&amp;quot;&amp;gt;22-23 Sydney Street&amp;lt;/p&amp;gt;
		&amp;lt;p&amp;gt;&amp;lt;span class=&amp;quot;locality&amp;quot;&amp;gt;Brighton&amp;lt;/span&amp;gt;, &amp;lt;abbr class=&amp;quot;country-name&amp;quot; title=&amp;quot;United Kingdom&amp;quot;&amp;gt;UK&amp;lt;/abbr&amp;gt;&amp;lt;/p&amp;gt;
		&amp;lt;p class=&amp;quot;postal-code&amp;quot;&amp;gt;BN1 4EN&amp;lt;/p&amp;gt;
	&amp;lt;/div&amp;gt;
	&amp;lt;p&amp;gt;Telephone: &amp;lt;span class=&amp;quot;tel&amp;quot;&amp;gt;+44 (0)1273 687 068&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;
	&amp;lt;p class=&amp;quot;geo&amp;quot;&amp;gt;Lat/Lon: 
		&amp;lt;span class=&amp;quot;latitude&amp;quot;&amp;gt;50.827917&amp;lt;/span&amp;gt;, 
		&amp;lt;span class=&amp;quot;longitude&amp;quot;&amp;gt;-0.137764&amp;lt;/span&amp;gt;
	&amp;lt;/p&amp;gt;
&amp;lt;/li&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As before, I used &lt;a href="http://www.getlatlon.com/"&gt;www.getlatlon.com&lt;/a&gt; to find the exact locations &amp;#8211; I find satellite view is particularly useful for locating individual buildings.&lt;/p&gt;

&lt;p&gt;Latitudes and longitudes are great for machines but not so useful for human beings. We could hide them entirely with &lt;code&gt;display: none&lt;/code&gt;, but I prefer to merely de-emphasise them (someone might want them for their &lt;span class="caps"&gt;GPS&lt;/span&gt; unit):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;.vcard .geo {
	margin-top: 0.5em;
	font-size: 0.85em;
	color: #ccc;
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It&amp;#8217;s probably a good idea to hide them completely when they&amp;#8217;re displayed inside an info bubble:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;.bubble .geo {
	display: none;
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We can extract the co-ordinates in the same way we extracted the address. Since we&amp;#8217;re no longer geocoding anything our code becomes a lot simpler:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$(&amp;#039;.vcard&amp;#039;).each(function() {
	var hcard = $(this);
	var latitude = hcard.find(&amp;#039;.geo .latitude&amp;#039;).text();
	var longitude = hcard.find(&amp;#039;.geo .longitude&amp;#039;).text();
	var marker = new Marker(new LatLonPoint(latitude, longitude));
	marker.setInfoBubble(
		&amp;#039;&amp;lt;div class=&amp;quot;bubble&amp;quot;&amp;gt;&amp;#039; + hcard.html() + &amp;#039;&amp;lt;/div&amp;gt;&amp;#039;
	);
	mapstraction.addMarker(marker);
});&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And here&amp;#8217;s the &lt;a href="https://24ways.org/examples/unobtrusively-mapping-microformats-with-jquery/restaurants-geo.html"&gt;finished geo example&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Further reading&lt;/h3&gt;

&lt;p&gt;We&amp;#8217;ve only scratched the surface of what&amp;#8217;s possible with microformats, jQuery (or just regular JavaScript) and a bit of imagination. If this example has piqued your interest, the following links should give you some more food for thought.&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://microformats.org/wiki/hcard"&gt;The hCard specification&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://microformats.org/wiki/hcard-parsing"&gt;Notes on parsing hCards&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://simonwillison.net/2007/Aug/15/jquery/"&gt;jQuery for JavaScript programmers&lt;/a&gt; &amp;#8211; my extended tutorial on jQuery.&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://www.danwebb.net/2007/2/9/sumo-a-generic-microformats-parser-for-javascript"&gt;Dann Webb&amp;#8217;s Sumo&lt;/a&gt; &amp;#8211; a full JavaScript library for parsing microformats, based around some clever metaprogramming techniques.&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://austin.adactio.com/"&gt;Jeremy Keith&amp;#8217;s Adactio Austin&lt;/a&gt; &amp;#8211; the first place I saw using microformats to unobtrusively plot locations on a map. Makes clever use of hEvent as well.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;This article &lt;a href="https://24ways.org/2007/unobtrusively-mapping-microformats-with-jquery/"&gt;originally appeared on 24ways&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;
    
        &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/24-ways"&gt;24-ways&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/brighton"&gt;brighton&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/jquery"&gt;jquery&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/mapping"&gt;mapping&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/microformats"&gt;microformats&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/unobtrusive-javascript"&gt;unobtrusive-javascript&lt;/a&gt;&lt;/p&gt;
    

</summary><category term="24-ways"/><category term="brighton"/><category term="jquery"/><category term="mapping"/><category term="microformats"/><category term="unobtrusive-javascript"/></entry><entry><title>Quoting John Resig</title><link href="https://simonwillison.net/2007/Sep/26/john/" rel="alternate"/><published>2007-09-26T12:21:07+00:00</published><updated>2007-09-26T12:21:07+00:00</updated><id>https://simonwillison.net/2007/Sep/26/john/</id><summary type="html">
    &lt;blockquote cite="http://groups.google.com/group/jquery-dev/browse_thread/thread/517dd87b61515162"&gt;&lt;p&gt;I have another technique [...] that  I'll be switching jQuery to. If you attempt to insert into the  document.body before the document is fully loaded, an exception is  thrown. I take advantage of that to determine when the document is  fully loaded.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p class="cite"&gt;&amp;mdash; &lt;a href="http://groups.google.com/group/jquery-dev/browse_thread/thread/517dd87b61515162"&gt;John Resig&lt;/a&gt;&lt;/p&gt;

    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/domcontentloaded"&gt;domcontentloaded&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/internet-explorer"&gt;internet-explorer&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/jquery"&gt;jquery&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/unobtrusive-javascript"&gt;unobtrusive-javascript&lt;/a&gt;&lt;/p&gt;



</summary><category term="domcontentloaded"/><category term="internet-explorer"/><category term="javascript"/><category term="jquery"/><category term="unobtrusive-javascript"/></entry><entry><title>DOMContentLoaded for IE, Safari, everything, without document.write</title><link href="https://simonwillison.net/2007/Sep/26/sil/" rel="alternate"/><published>2007-09-26T12:19:07+00:00</published><updated>2007-09-26T12:19:07+00:00</updated><id>https://simonwillison.net/2007/Sep/26/sil/</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://www.kryogenix.org/days/2007/09/26/shortloaded"&gt;DOMContentLoaded for IE, Safari, everything, without document.write&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Stuart has taken Hedger’s recent IE technique, combined it with the others and compressed it in to a short-as-possible code snippet that you can paste in to your scripts without having to include the whole of jQuery/YUI/Dojo/Prototype.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/documentwrite"&gt;documentwrite&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/dom-scripting"&gt;dom-scripting&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/internet-explorer"&gt;internet-explorer&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/ondomready"&gt;ondomready&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/safari"&gt;safari&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/stuart-langridge"&gt;stuart-langridge&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/unobtrusive-javascript"&gt;unobtrusive-javascript&lt;/a&gt;&lt;/p&gt;



</summary><category term="documentwrite"/><category term="dom-scripting"/><category term="internet-explorer"/><category term="javascript"/><category term="ondomready"/><category term="safari"/><category term="stuart-langridge"/><category term="unobtrusive-javascript"/></entry><entry><title>IEContentLoaded</title><link href="https://simonwillison.net/2007/Sep/24/iecontentloaded/" rel="alternate"/><published>2007-09-24T12:10:02+00:00</published><updated>2007-09-24T12:10:02+00:00</updated><id>https://simonwillison.net/2007/Sep/24/iecontentloaded/</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://www.hedgerwow.com/360/dhtml/ie-dom-ondocumentready.html"&gt;IEContentLoaded&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
An alternative method of detecting DOMContentLoaded on IE; works by polling until the doScroll() method on an unattached element stops throwing errors.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/domcontentloaded"&gt;domcontentloaded&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/dom-scripting"&gt;dom-scripting&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/hedger-wang"&gt;hedger-wang&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/iecontentloaded"&gt;iecontentloaded&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/internet-explorer"&gt;internet-explorer&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/unobtrusive-javascript"&gt;unobtrusive-javascript&lt;/a&gt;&lt;/p&gt;



</summary><category term="domcontentloaded"/><category term="dom-scripting"/><category term="hedger-wang"/><category term="iecontentloaded"/><category term="internet-explorer"/><category term="javascript"/><category term="unobtrusive-javascript"/></entry><entry><title>Executing JavaScript on page load</title><link href="https://simonwillison.net/2004/May/26/addLoadEvent/" rel="alternate"/><published>2004-05-26T06:30:37+00:00</published><updated>2004-05-26T06:30:37+00:00</updated><id>https://simonwillison.net/2004/May/26/addLoadEvent/</id><summary type="html">
    &lt;p id="p-0"&gt;Peter-Paul Koch &lt;a href="http://www.digital-web.com/articles/separating_behavior_and_structure_2/" title="Separating behavior and structure"&gt;recently wrote&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote cite="http://www.digital-web.com/articles/separating_behavior_and_structure_2/"&gt;&lt;p id="p-1"&gt;In my opinion, recent advances in JavaScript theory call for the removal of the event handlers that some Web developers-and all WYSIWYG editors-deploy in large masses in their XHTML files, where they don’t belong.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p id="p-2"&gt;&lt;acronym title="Peter-Paul Koch"&gt;PPK&lt;/acronym&gt; is talking about inline event attributes such as the infamous &lt;code class="html"&gt;onclick=""&lt;/code&gt; and &lt;code class="html"&gt;onmouseover=""&lt;/code&gt; which have infested our &lt;acronym title="HyperText Markup Language"&gt;HTML&lt;/acronym&gt; ever since Netscape introduced JavaScript back in version 2.0 of their browser. The alternative to these handlers is to add event handlers to elements after the document has loaded. &lt;acronym title="Peter-Paul Koch"&gt;PPK&lt;/acronym&gt; has detailed coverage of the various ways of doing this &lt;a href="http://www.quirksmode.org/js/introevents.html" title="Introduction to Events"&gt;on his QuirksMode site&lt;/a&gt;.&lt;/p&gt;

&lt;p id="p-3"&gt;In my work with unobtrusive JavaScript, I've found that by far the most common action I take is "registering" a script to be executed once the page has finished loading. There are a number of ways of doing this, which I described in my article &lt;a href="http://www.sitepoint.com/article/structural-markup-javascript"&gt;Enhancing Structural Markup with JavaScript&lt;/a&gt;. Unfortunately, none of them are perfect if you wish to write truly reusable scripts.&lt;/p&gt;

&lt;p id="p-4"&gt;For a script (such as my blockquote citations script discussed in the article) to be properly reusable, it needs to behave nicely in the presence of other scripts. This means that assigning a callback function directly to the &lt;code class="javascript"&gt;window.onload&lt;/code&gt; handler is out of the question as doing so will over-ride previously assigned callbacks from other scripts. The correct way of adding a handler to an event without over-riding other handlers is to use modern event attachment method, which sadly differ between &lt;acronym title="Internet Explorer"&gt;IE&lt;/acronym&gt;/Windows and other browsers. Scott Andrew's &lt;a href="http://www.scottandrew.com/weblog/jsjunk#events" title="addEvent / removeEvent"&gt;addEvent function&lt;/a&gt; handles the differences for you but comes with one major and rarely discussed drawback: it fails silently in &lt;acronym title="Internet Explorer"&gt;IE&lt;/acronym&gt;5/Mac. If you care about the many Mac users still on OS9, you need to support that browser.&lt;/p&gt;

&lt;p id="p-5"&gt;Anyway, I believe I've found a solution. Check this out:&lt;/p&gt;

&lt;pre&gt;&lt;code class="javascript"&gt;
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
addLoadEvent(function() {
  /* more code to run on page load */ 
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p id="p-6"&gt;The &lt;code class="javascript"&gt;addLoadEvent&lt;/code&gt; function takes as an argument another function which should be executed once the page has loaded. Unlike assigning directly to &lt;code class="javascript"&gt;window.onload&lt;/code&gt;, the function adds the event in such a way that any previously added onload functions will be executed first.&lt;/p&gt;

&lt;p id="p-7"&gt;The way this works is relatively simple: if &lt;code class="javascript"&gt;window.onload&lt;/code&gt; has not already been assigned a function, the function passed to &lt;code class="javascript"&gt;addLoadEvent&lt;/code&gt; is simply assigned to &lt;code class="javascript"&gt;window.onload&lt;/code&gt;. If &lt;code class="javascript"&gt;window.onload&lt;/code&gt; has already been set, a brand new function is created which first calls the original onload handler, then calls the new handler afterwards.&lt;/p&gt;

&lt;p id="p-8"&gt;&lt;code class="javascript"&gt;addLoadEvent&lt;/code&gt; has one very important property: it will work even if something has previously been assigned to window.onload without using &lt;code class="javascript"&gt;addLoadEvent&lt;/code&gt; itself. This makes it ideal for use in scripts that may be executing along side other scripts that have already been registered to execute once the page has loaded.&lt;/p&gt;

&lt;p id="p-9"&gt;I've tested the above code successfully on &lt;acronym title="Internet Explorer"&gt;IE&lt;/acronym&gt; 5, 5.5 and 6 for Windows; &lt;acronym title="Internet Explorer"&gt;IE&lt;/acronym&gt; 5 and Safari for Mac; Opera 7.5 and FireFox on Mac (which should mean it works with those browsers on Windows as well). Opera 6 for Mac failed the test but has poor JavaScript support anyway and is hopefully becoming more and more rare now that Opera 7 has matured.&lt;/p&gt;

&lt;p id="p-10"&gt;I've created &lt;a href="http://simon.incutio.com/code/js/addloadevent/" title="addLoadEvent() test"&gt;a test page&lt;/a&gt; for the function. I'd be interested to here any bug reports from browsers I haven't covered.&lt;/p&gt;

&lt;p id="p-11"&gt;I'm still considering ways in which this technique could be extended to work for generic events rather than just page loads. The challenge there would be to ensure that information about the event itself was passed to the event handlers in a consistent manner. For page load events this isn't an issue as the event object does not contain any valuable information.&lt;/p&gt;

&lt;p id="p-12"&gt;&lt;strong&gt;Update:&lt;/strong&gt; I've written the new technique up &lt;a href="http://www.sitepoint.com/blog-post-view.php?id=171578" title="Closures and executing JavaScript on page load"&gt;on my SitePoint blog&lt;/a&gt; and incorporated an explanation of closures and how they are used to preserve any previously assigned onload handlers.&lt;/p&gt;

&lt;p id="p-13"&gt;&lt;strong&gt;Update 28th May 2006&lt;/strong&gt;: Billy Pan pointed out that the original code caused a runtime error in IE 7. His fix was to add an &lt;code&gt;if (oldonload)&lt;/code&gt;; I have rolled this fix in to the code shown above.&lt;/p&gt;
    
        &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/addloadevent"&gt;addloadevent&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/onload"&gt;onload&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/unobtrusive-javascript"&gt;unobtrusive-javascript&lt;/a&gt;&lt;/p&gt;
    

</summary><category term="addloadevent"/><category term="closures"/><category term="javascript"/><category term="onload"/><category term="unobtrusive-javascript"/></entry></feed>