<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mozilla Hacks - the Web developer blog</title>
	<atom:link href="http://hacks.mozilla.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://hacks.mozilla.org</link>
	<description>hacks.mozilla.org</description>
	<lastBuildDate>Mon, 08 Feb 2010 22:02:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>About:hacks newsletter &#8211; issue 2</title>
		<link>http://hacks.mozilla.org/2010/02/abouthacks-newsletter-issue-2/</link>
		<comments>http://hacks.mozilla.org/2010/02/abouthacks-newsletter-issue-2/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 22:02:54 +0000</pubDate>
		<dc:creator>Alix Franquet</dc:creator>
				<category><![CDATA[MDN]]></category>
		<category><![CDATA[about:hacks]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=3943</guid>
		<description><![CDATA[Last week we sent out the second issue of about:hacks, Mozilla&#8217;s newsletter for web developers. 
Here are highlights from the topics covered in this new issue:

Firefox 3.6 released: what&#8217;s new, how to give feedback
Firefox for the Nokia N900 with Weave Sync released
Developer Tools: Firebug 1.5
Demos: an HTML5 image editor and uploader, a WebGL demo
Behind the [...]]]></description>
			<content:encoded><![CDATA[<p>Last week we sent out the <a href="http://bit.ly/abouthacksissue2">second issue of about:hacks</a>, Mozilla&#8217;s newsletter for web developers. </p>
<p>Here are highlights from the topics covered in this new issue:</p>
<ul>
<li><a href="http://us1.campaign-archive1.com/?u=168bf22f976f5a68fe5770d19&#038;id=f3cf5476ac#firefox36">Firefox 3.6 released:</a> what&#8217;s new, how to give feedback</li>
<li><a href="http://us1.campaign-archive1.com/?u=168bf22f976f5a68fe5770d19&#038;id=f3cf5476ac#mobile">Firefox for the Nokia N900 with Weave Sync released</a></li>
<li><a href="http://us1.campaign-archive1.com/?u=168bf22f976f5a68fe5770d19&#038;id=f3cf5476ac#firebug">Developer Tools: Firebug 1.5</a></li>
<li><a href="http://us1.campaign-archive1.com/?u=168bf22f976f5a68fe5770d19&#038;id=f3cf5476ac#demo">Demos:</a> an HTML5 image editor and uploader, a WebGL demo</li>
<li><a href="http://us1.campaign-archive1.com/?u=168bf22f976f5a68fe5770d19&#038;id=f3cf5476ac#behindscenes">Behind the Scenes:</a> Firefox release timeline, experimenting with sound and light</li>
<li><a href="http://us1.campaign-archive1.com/?u=168bf22f976f5a68fe5770d19&#038;id=f3cf5476ac#mdc">New on MDC:</a> Firefox.next, WebGL docs</li>
<li><a href="http://us1.campaign-archive1.com/?u=168bf22f976f5a68fe5770d19&#038;id=f3cf5476ac#hacksblog">Hacks blog redesign</a></li>
</ul>
<p>If you do not subscribe to about:hacks, you will find the <a href="http://bit.ly/abouthacksissue2">second issue</a> in the <a href="http://bit.ly/abouthacksarchive">archives</a>. If you enjoy the content, consider <a href="http://bit.ly/abouthacks">subscribing</a> to make sure you receive the third issue, coming in March.</p>
<p>Finally, we&#8217;d love to get your feedback on the newsletter: what do you like, what would you change, what topics would you like us to cover. Please take a minute to fill out the <a href="http://www.surveygizmo.com/s/233389/about-hacks-issue-2-jan-10">feedback form</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2010/02/abouthacks-newsletter-issue-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Firefox 3.6 feedback</title>
		<link>http://hacks.mozilla.org/2010/02/firefox-3-6-feedback/</link>
		<comments>http://hacks.mozilla.org/2010/02/firefox-3-6-feedback/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 02:27:42 +0000</pubDate>
		<dc:creator>Alix Franquet</dc:creator>
				<category><![CDATA[Firefox 3.6]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=3924</guid>
		<description><![CDATA[Firefox 3.6 was released on Jan 21st and has already been downloaded more than 35 million times! It features a faster JavaScript engine, faster DOM performance and a bunch of new HTML5 features. Highlights for web developers include support for the WOFF font format, new CSS features like gradients and multiple backgrounds, drag and drop, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://hacks.mozilla.org/2010/01/firefox-3-6-is-here/">Firefox 3.6</a> was <a href="http://blog.mozilla.com/blog/2010/01/28/firefox-3-6-release-day-behind-the-scenes/">released on Jan 21st</a> and has already been downloaded more than <a href="http://www.mozilla.com/en-US/firefox/stats/">35 million times</a>! It features a faster JavaScript engine, faster DOM performance and a bunch of new HTML5 features. Highlights for web developers include support for the WOFF font format, new CSS features like gradients and multiple backgrounds, drag and drop, File API, device orientation, and more. Want to see the full list? Look at our <a href="https://developer.mozilla.org/en/Firefox_3.6_for_developers">Firefox 3.6 for Developers</a> page on developer.mozilla.org.</p>
<p>We hope that you took the time to test Firefox 3.6 on your web sites before the release. If you didn&#8217;t, it&#8217;s not too late to let us know about issues. Here are a few problems that we&#8217;ve seen in the wild so far:</p>
<ul>
<li>We&#8217;ve seen a couple of issues with the <a href="http://ckeditor.com/">FCKEditor</a> component, used widely on the web. It apparently <a href="http://www.petefreitag.com/item/737.cfm">doesn&#8217;t handle dates after 2009</a> properly (this is the first major browser release in 2010, a new decade!) and it also has some problems with <a href="http://www.usefulconcept.com/index.cfm/2010/1/21/FireFox-36-KTML-Editor-Fix">document.readyState</a>.</li>
<li>People who had the <a href="https://addons.mozilla.org/en-US/firefox/addon/5369">YSlow extension</a> installed saw crashes with Firefox 3.6. YSlow has been updated to version 2.0.6 to fix the issue</li>
<li>Some Facebook apps were broken by a change we made to comply with the upcoming HTML5 standard. We updated <a href="https://developer.mozilla.org/en/DOM/element.getElementsByTagNameNS">element.getElementsByTagNameNS</a> and <a href="https://developer.mozilla.org/en/DOM/document.getElementsByTagNameNS">document.getElementsByTagNameNS</a> to no longer case-fold when doing tag name lookups. (We strongly suggest that you only use lower-case for tag names for many reasons, including this.) For more information, please see this note from <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=542185#c5">Henri Sivonen</a> about what&#8217;s changed. Facebook has since fixed the issue in their code, but other sites may also be tripping over this.</li>
</ul>
<p>We&#8217;re looking for feedback on any developer-facing regressions you&#8217;ve seen in 3.6 from 3.5.  Please comment on this post if you have feedback.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2010/02/firefox-3-6-feedback/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>an HTML5 offline image editor and uploader application</title>
		<link>http://hacks.mozilla.org/2010/02/an-html5-offline-image-editor-and-uploader-application/</link>
		<comments>http://hacks.mozilla.org/2010/02/an-html5-offline-image-editor-and-uploader-application/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 16:00:14 +0000</pubDate>
		<dc:creator>Paul Rouget</dc:creator>
				<category><![CDATA[Demo]]></category>
		<category><![CDATA[Drag and Drop]]></category>
		<category><![CDATA[Featured Demo]]></category>
		<category><![CDATA[FileAPI]]></category>
		<category><![CDATA[Firefox 3.6]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Offline]]></category>
		<category><![CDATA[XMLHttpRequest]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=3569</guid>
		<description><![CDATA[Many web applications use image uploaders: image hosting websites, blog publishing applications, social networks, among many others. Such uploaders have limitations: you can&#8217;t upload more than one file at a time and you can&#8217;t edit the image before sending it. A plugin is the usual workaround for uploading more than one image, and image modifications [...]]]></description>
			<content:encoded><![CDATA[<p>Many web applications use image uploaders: image hosting websites, blog publishing applications, social networks, among many others. Such uploaders have limitations: you can&#8217;t upload more than one file at a time and you can&#8217;t edit the image before sending it. A plugin is the usual workaround for uploading more than one image, and image modifications are usually done on the server side, which can make the editing process more cumbersome.</p>
<p>Firefox 3.6 offers many <a href="https://developer.mozilla.org/en/Firefox_3.6_for_developers">new Open Web features</a> to web developers, including even more HTML5 support. This post describes how to create a sophisticated image editor and uploader built using Open Web technologies.</p>
<p>See below for a video of the demo with some background.</p>
<h2>Hosted on hacks, publishes to twitpic</h2>
<p>Our web application uploads pictures to twitpic, an image hosting service for Twitter.</p>
<p>Note that code for this application is actually hosted on the <a href="http://hacks.mozilla.org/">hacks web server</a> but can still upload to Twitpic. Uploading to Twitpic is possible because they opened up their API to <a href="https://developer.mozilla.org/En/HTTP_Access_Control">Cross Domain XMLHttpRequests</a> for applications like this. (Thank you <a href="http://twitpic.com">twitpic</a>!).</p>
<p><a href="http://twitpic.com"><img src="http://hacks.mozilla.org/wp-content/uploads/2010/01/twitpic.png" title="twitpic" style="border: 0" class="aligncenter"/></a></p>
<h2>Web Features</h2>
<p>The <a href="http://demos.hacks.mozilla.org/openweb/imageUploader/">demo</a> uses the following features from HTML5 included in Firefox 3.6:</p>
<ul>
<li><a href="http://hacks.mozilla.org/2009/12/file-drag-and-drop-in-firefox-3-6/">HTML5 Drag and Drop</a>: You can drag and drop items inside of the web page and drag images from your desktop directly into the browser.</li>
<li><a href="http://hacks.mozilla.org/2009/06/localstorage/">HTML5 <code>localStorage</code></a>: to store the image data across browser restarts</li>
<li><a href="http://hacks.mozilla.org/2010/01/offline-web-applications/">HTML5 Application Cache</a>: This allows you to create applications that can be used when the browser isn&#8217;t connected to the Internet.  It stores the entire app in the browser and also gives you access to online and offline events so you know when you need to re-sync data with a server.</li>
<li><a href="http://hacks.mozilla.org/2009/06/pushing-pixels-with-canvas/">HTML5 Canvas</a>: The HTML5 Canvas element is used through this demo to edit and render images.
<li><a href="http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/">Cross-Origin Resource Sharing</a> to host an application at one site and publish data to another.
</ul>
<p><video controls="true"><br />
<source src="http://videos.mozilla.org/serv/marketing/html5_img_uploader/html5_img_uploader.ogv" type="video/ogg"/><br />
<source src="http://videos.mozilla.org/serv/marketing/html5_img_uploader/html5_img_uploader.mp4" type="video/mp4"/><br />
<embed src="http://www.youtube.com/v/wbSoSCStodA&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="500"></embed><br />
</video></p>
<h2>What&#8217;s in the demo?</h2>
<p>See the <a href="http://demos.hacks.mozilla.org/openweb/imageUploader/">live demo</a> to try the image uploader for yourself. You will need <a href="http://www.firefox.com">Firefox 3.6</a> and a <a href="http://twitter.com/">twitter</a> account.</p>
<p>Here&#8217;s a full list of the things that this application can do:</p>
<ul>
<li>You can drag images from your desktop or the web into the application.</li>
<li>You can see a preview of each image you want to upload.</li>
<li>You can drag previews to the trash to delete an image.</li>
<li>Images are automatically made smaller if they are bigger than 600px wide.</li>
<li>You can edit any of the images before uploading.  This includes being able to rotate, flip, crop or turn an image black and white.</li>
<li>If you edit an image it&#8217;s saved locally so you can still edit when you&#8217;re offline.  If you close the tab, restart Firefox or your computer they will be there when you load the page again so you can upload when you&#8217;re re-connected.</li>
<li>It will upload several files at once and provide feedback as the upload progresses.</li>
<li>The HTML5 Offline Application Cache makes the application load very quickly since it&#8217;s all stored offline.</li>
</ul>
<h2>Under the Hood</h2>
<p>Let&#8217;s quickly go over all of the technology that we&#8217;re using in this application.</p>
<h3>Cross-XMLHttpRequest</h3>
<p><a href="http://twitpic.com/">Twitpic</a> was nice enough to open their API to allow XMLHttpRequests from any other domain. This means that you can now use their API from your own website and offer your own image uploader.</p>
<p>If you&#8217;re running a web site with an API you want people to use from other web sites, you can do that with <a href="https://developer.mozilla.org/En/HTTP_Access_Control">Cross-site HTTP requests</a>.  In order for you to support it you will need to add an HTTP header to responses from your web server that says which domains you allow.  As an example, here&#8217;s how twitpic allows access from all domains:</p>
<pre>
Access-Control-Allow-Origin: *
</pre>
<p>It&#8217;s important to note that opening your API does have security implications so you should be careful to understand those issues before blindly opening an API.  For more details, <a href="https://developer.mozilla.org/En/HTTP_access_control">see MDC documentation on CORS</a>.</p>
<h3>Drag and Drop</h3>
<p><a href="https://developer.mozilla.org/En/DragDrop/DataTransfer">Drag and Drop</a> is a mechanism with two important features:</p>
<ul>
<li>Dragging files from your Desktop to your web page.</li>
<li>Native Drag and Drop inside your web page (not just changing the coordinates of your elements).</li>
</ul>
<p>The image uploader uses Drag and Drop to allow the user the add files from the Desktop, to remove files (drag them to the trash) and to insert a new image into a current image.</p>
<p>For more on <a href="http://hacks.mozilla.org/category/drag-and-drop/">Drag and Drop</a>, see previous hacks articles, in particular <a href="http://hacks.mozilla.org/2009/12/file-drag-and-drop-in-firefox-3-6/">how to use Drag and Drop in your application</a>.</p>
<h3>Canvas to Edit Images</h3>
<p>Once images have been dragged and dropped into the web page, the image uploader lets you edit them before uploading. This is possible because images are actually copied to <code>canvas</code> elements via the <a href="http://hacks.mozilla.org/category/fileapi/">File API</a>.</p>
<p>In this case, the editing process is really basic: rotate, flip, add text, black and white, crop. However, you can imagine offering many other features in your version of the editor (see <a href="http://editor.pixastic.com/">Pixastic</a> for example, or this inlay feature <a href="http://people.mozilla.com/~prouget/demos/fennec/imgManipulation/">here</a>).</p>
<p>Using <code>canvas</code> and the <a href="http://hacks.mozilla.org/category/fileapi/">File API</a> also let you resize the image before sending it. Here, every image is converted to a new image (canvas) that is less than 600px.</p>
<h3>localStorage: Save Local Data</h3>
<p>It&#8217;s possible to <a href="http://hacks.mozilla.org/2010/01/offline-web-applications/">store local data</a> persistently in a web page using <code>localStorage</code>, up to 5Mb of data per domain. </p>
<p>In the image uploader, localStorage is used to store images and credentials. Since images are actually <code>canvas</code>, you can store them as data URLs:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;2d&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toDataURL</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;image/png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
localStorage.<span style="color: #660066;">setItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;image1&quot;</span><span style="color: #339933;">,</span> url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>LocalStorage support means that you can edit an image, close Firefox, switch off your computer, and the edited image will still be there when you restart Firefox.</p>
<h3>Offline</h3>
<p>If you add a manifest file listing all remote files needed to display your web application it will work even when you aren&#8217;t connected to the Internet. A nice side effect is that it will also make your application load much faster.</p>
<p>Here, the <code>html</code> element refers to a manifest file:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html</span> <span style="color: #000066;">manifest</span>=<span style="color: #ff0000;">&quot;offline.manifest&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></pre></div></div>

<p>And the manifest file looks like:</p>
<pre style="border-left: 2px solid grey; padding-left: 10px;">
CACHE MANIFEST

# v2.4
index.xhtml
fonts/MarketingScript.ttf
css/desktop.css
css/fonts.css
css/mobile.css
[...]
</pre>
<p>You can also catch offline and online events to know if the connection status changes.<br />
<a href="http://hacks.mozilla.org/2010/01/offline-web-applications/">For more information see our last article about offline</a>.</p>
<h2>Conclusion</h2>
<p>Firefox 3.6 allows millions of people to take advantage of modern standards, including HTML5.<br />
The image uploader described here shows how a web page should really be considered as an application since it interacts with your Desktop and works offline. </p>
<p>Here are a few tips for writing your next application using Open Web technologies:</p>
<p><em><strong>Allow Cross-XMLHttpRequest:</strong></em><br />
If it makes sense for your service, allow people to access your API from a different domain, you&#8217;ll be amazed at the apps people will come up with.</p>
<p><em><strong>Allow multiple input:</strong></em><br />
Let people Drag files to your application and use <code>&lt;input&nbsp;type="file"&nbsp;multiple=""&gt;</code> so they can select several files at once. <em>In this demo, we use a multiple input which is visible only in the mobile version, but for accessibility consideration, don&#8217;t forget to use it to propose an alternative to Drag&#8217;n Drop.</em></p>
<p><em><strong>Use native Drag and Drop:</strong></em><br />
Drag and Drop mechanisms are usually simulated (updating coordinates on the mousemove event.)  When you can, use the native mechanism.</p>
<p><em><strong>Use the File API</strong></em><br />
To pre-process a file before even talking to a server.</p>
<p><em><strong>Support offline</strong></em><br />
Store data and use a manifest to make your application data persistent while offline.</p>
<p><em><strong>Use Canvas</strong></em><br />
Canvas is the most widely implemented HTML5 element. It works everywhere (even if it has to be <a href="http://code.google.com/p/explorercanvas/">simulated</a>), use it!</p>
<p><em>Think &#8220;Client Side&#8221;</em>: HTML5, CSS3 and the new powerful JavaScript  engines let you create amazing applications, take advantage of them!</p>
<p>We look forward to seeing the great new applications you&#8217;ll come up with using Open Web technologies! </p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2010/02/an-html5-offline-image-editor-and-uploader-application/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
<enclosure url="http://videos.mozilla.org/serv/marketing/html5_img_uploader/html5_img_uploader.ogv" length="5921036" type="video/ogg" />
		</item>
		<item>
		<title>ClassList in Firefox 3.6</title>
		<link>http://hacks.mozilla.org/2010/01/classlist-in-firefox-3-6/</link>
		<comments>http://hacks.mozilla.org/2010/01/classlist-in-firefox-3-6/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 18:29:40 +0000</pubDate>
		<dc:creator>Paul Rouget</dc:creator>
				<category><![CDATA[Firefox 3.6]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=3549</guid>
		<description><![CDATA[This article was writt by Anthony Ricaud, French OpenWeb enthusiast.
Why you need classList
A dynamic web application usually needs visual feedback from its inner mechanism or needs to display different visual elements based on users&#8217; actions.
To change the user interface easily, you can add/remove/edit elements through the DOM API (document.createElement, div.removeChild, elt.style.color, &#8230;) but it&#8217;s easier [...]]]></description>
			<content:encoded><![CDATA[<p><em>This article was writt by <a href="http://hanblog.info/blog/">Anthony Ricaud</a>, French OpenWeb enthusiast.</em></p>
<h3>Why you need classList</h3>
<p>A dynamic web application usually needs visual feedback from its inner mechanism or needs to display different visual elements based on users&#8217; actions.</p>
<p>To change the user interface easily, you can add/remove/edit elements through the DOM API (<code>document.createElement</code>, <code>div.removeChild</code>, <code>elt.style.color</code>, &#8230;) but it&#8217;s easier to just update the elements&#8217; <code>class</code> attribute to change how they are displayed and styled by CSS.</p>
<p>Let&#8217;s take an example. Suppose you want to display a form with two modes: a <em>basic</em> mode, and an <em>expert</em> mode with extra options.</p>
<p>This can be done with CSS rules: each mode has its own class and set of CSS code.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#anexpertinput</span><span style="color: #6666ff;">.basic</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#anexpertinput</span><span style="color: #6666ff;">.expert</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">inline</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>To dynamically change the classes of elements, you can use <code>element.className</code>. However, you may want to add, remove, or toggle just one class. There used to be two ways to do this, by using a library or by writing complex code with regular expressions. There is now another way with the new HTML5 API called <code>classList</code>, which is implemented in Firefox 3.6.</p>
<p>Let&#8217;s see how it can simplify your code and improve performance at the same time.</p>
<h3>The classList API</h3>
<p>Here is an example to show you what the classList API looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// By default, start without a class in the div: &lt;div class=&quot;&quot;/&gt;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Set &quot;foo&quot; as the class by adding it to the classList</span>
div.<span style="color: #660066;">classList</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// now &lt;div class=&quot;foo&quot;/&gt;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Check that the classList contains the class &quot;foo&quot;</span>
div.<span style="color: #660066;">classList</span>.<span style="color: #660066;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns true</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Remove the class &quot;foo&quot; from the list</span>
div.<span style="color: #660066;">classList</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// now &lt;div class=&quot;&quot;/&gt;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Check if classList contains the class &quot;foo&quot;</span>
div.<span style="color: #660066;">classList</span>.<span style="color: #660066;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns false: &quot;foo&quot; is gone</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Check if class contains the class &quot;foo&quot;,</span>
<span style="color: #006600; font-style: italic;">// If it does, &quot;foo&quot; is removed, if it doesn't, it's added</span>
div.<span style="color: #660066;">classList</span>.<span style="color: #660066;">toggle</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// class set to &lt;div class=&quot;foo&quot;/&gt;</span>
div.<span style="color: #660066;">classList</span>.<span style="color: #660066;">toggle</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// class set to &lt;div class=&quot;&quot;/&gt;</span></pre></div></div>

<h3>Demo</h3>
<p>Let&#8217;s go back to our initial example of a form with both a basic and an expert mode &#8211; check out the <a href="http://demos.hacks.mozilla.org/openweb/classList/">live demo</a> to see it in action.</p>
<p>As you can see in the code below, you can switch between the two modes with one line of JavaScript. </p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;button</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;document.getElementById('box').classList.toggle('expert');&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  Toggle expert mode
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/button<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;box&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;label</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;nick&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Name: <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;nick&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/label<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;label</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;status&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Status: <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;status&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/label<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;help&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> Blablablablabla...<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;label</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;postpone&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Postpone: <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;checkbox&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;postpone&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/label<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;label</span> <span style="color: #000066;">for</span>=<span style="color: #ff0000;">&quot;lang&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Lang: <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;lang&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/label<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#box</span><span style="color: #6666ff;">.expert</span> <span style="color: #00AA00;">&gt;</span> <span style="color: #cc00cc;">#help</span><span style="color: #00AA00;">,</span>
<span style="color: #cc00cc;">#box</span><span style="color: #6666ff;">.expert</span> <span style="color: #00AA00;">&gt;</span> label<span style="color: #00AA00;">&#91;</span>for<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;postpone&quot;</span><span style="color: #00AA00;">&#93;</span><span style="color: #00AA00;">,</span>
<span style="color: #cc00cc;">#box</span><span style="color: #6666ff;">.expert</span> <span style="color: #00AA00;">&gt;</span> label<span style="color: #00AA00;">&#91;</span>for<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;lang&quot;</span><span style="color: #00AA00;">&#93;</span> <span style="color: #00AA00;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>See the <a href="https://developer.mozilla.org/en/DOM/element.classList">Mozilla documentation</a> and the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/urls.html#domtokenlist-0">HTML5 specification</a> for more details on classList.</p>
<h2>Performance</h2>
<p>Using the classList API is not only easier, it&#8217;s also more powerful. Take a look at <a href="http://demos.hacks.mozilla.org/openweb/classList/">what we observed</a> using Firefox 3.6.</p>
<p><a href="http://hacks.mozilla.org/wp-content/uploads/2010/01/benchmark.png"><img src="http://hacks.mozilla.org/wp-content/uploads/2010/01/benchmark-500x335.png" alt="benchmark classList" title="benchmark classList" width="500" height="335" class="aligncenter size-large wp-image-3550" /></a></p>
<h2>Interoperability</h2>
<p>Since other browser vendors have not yet implemented the HTML5 classList API, you still need fallback code. You can use <a href="http://demos.hacks.mozilla.org/openweb/classList/classList.js">this sample code</a> as fallback.</p>
<p>To know more about the current implementation of classList in well-known JavaScript libraries, see:</p>
<ul>
<li><a href="http://yuilibrary.com/projects/yui3/ticket/2528188">YUI</a></li>
<li><a href="https://prototype.lighthouseapp.com/projects/8886/tickets/969-usage-of-native-classlist-for-classname-methods-when-available">Prototype</a></li>
<li><a href="http://dev.jquery.com/ticket/5087">jQuery</a> (and <a href="http://dev.jquery.com/ticket/5766">here</a>)</li>
<li><a href="http://code.google.com/p/base2/issues/detail?id=118">Base2</a></li>
</ul>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2010/01/classlist-in-firefox-3-6/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>industry support for WOFF and Firefox 3.6</title>
		<link>http://hacks.mozilla.org/2010/01/industry-support-for-woff-and-firefox-3-6/</link>
		<comments>http://hacks.mozilla.org/2010/01/industry-support-for-woff-and-firefox-3-6/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 21:51:21 +0000</pubDate>
		<dc:creator>Christopher Blizzard</dc:creator>
				<category><![CDATA[Firefox 3.6]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=3752</guid>
		<description><![CDATA[Today we announced the release of Firefox 3.6 and users are starting to upgrade.  One of the more important features we included for developers was support for a new font standard called WOFF.
WOFF has received wide support from the type community and we&#8217;re starting to see the results of that.  There are a [...]]]></description>
			<content:encoded><![CDATA[<p>Today we announced the release of Firefox 3.6 and users are starting to upgrade.  One of the more important features we included for developers was support for a new font standard called <a href="http://hacks.mozilla.org/2009/10/woff/">WOFF</a>.</p>
<p>WOFF has received wide support from the type community and we&#8217;re starting to see the results of that.  There are a couple of specific instances that we&#8217;d like to point out on day zero of our release:</p>
<p>1. FontFont, one of the largest type foundries in the world, announced a free-as-in-beer demo font in the WOFF format called <a href="http://www.fontfont.com/downloads/index.ep#FFNuvoMediumWoffDemo">FF Nuvo Medium Demo</a>.  This is part of FontFont&#8217;s commitment to support the new format and to start to give people the tools to start to adopt it.</p>
<p>2. Typekit, a commercial hosting provider for fonts, <a href="http://blog.typekit.com/2010/01/21/typekit-supports-woff-in-firefox-3-6/">announced that they will start serving up WOFF fonts to Firefox 3.6 users</a>.</p>
<p>It&#8217;s great to see commercial support already ramping up to support the new format.  It&#8217;s going to really improve the experience for users on the web.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2010/01/industry-support-for-woff-and-firefox-3-6/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Firefox 3.6 is here!</title>
		<link>http://hacks.mozilla.org/2010/01/firefox-3-6-is-here/</link>
		<comments>http://hacks.mozilla.org/2010/01/firefox-3-6-is-here/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 17:09:45 +0000</pubDate>
		<dc:creator>Paul Rouget</dc:creator>
				<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[Firefox 3.6]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=3604</guid>
		<description><![CDATA[
Firefox 3.6 has some cool consumer facing features like Personas and a better Plug-in Updater, but developers have a lot to be excited about too.&#160; Developers will appreciate the increased stability, especially the work done to prevent crashes with third party software.&#160; There are also enhancements like improved JavaScript performance and optimizations to speed up [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.firefox.com"><img src="http://hacks.mozilla.org/wp-content/uploads/2010/01/logo-only-250x250.png" alt="" title="logo-only" width="200" height="200" class="alignleft size-medium wp-image-3666" /></a>
<p>Firefox 3.6 has some cool consumer facing features like <a href=http://www.mozilla.com/en-US/firefox/video/?video=personas id=sc48 title=Personas>Personas</a> and a better <a href=http://www.mozilla.com/en-US/plugincheck/ id=a::t title="Plug-in Updater">Plug-in Updater</a>, but developers have a lot to be excited about too.&nbsp; Developers will appreciate the increased stability, especially the work done to prevent crashes with third party software.&nbsp; There are also enhancements like <a href=http://hacks.mozilla.org/2010/01/javascript-speedups-in-firefox-3-6/ id=u.d9 title="improved JavaScript performance">improved JavaScript performance</a> and optimizations to speed up everyday Web tasks to make web applications snappier.&nbsp; However, what developers will be most interested in are all the new features around CSS3 and HTML5 that bring the future of the Web to Firefox 3.6 today.&nbsp;</p>
<p>This is an exciting release because we have<span style=COLOR:#ff0000> <span style=COLOR:#000000>built on</span> </span>the <a href=https://developer.mozilla.org/En/Firefox_3.5_for_developers id=s1ta title="progress we made with Firefox 3.5">progress we made with Firefox 3.5</a> just a few months ago and have implemented even more cutting-edge features that will make the Web experience more engaging and interactive than ever before.</p>
<p>Today&#8217;s Firefox 3.6 release and the upcoming mobile release of Firefox for Maemo are both based on Gecko 1.9.2 and bring a number of big features including:</p>
<ul>
<li>
      <b>New CSS3 features</b>: <a href="http://hacks.mozilla.org/category/css/as/brief/">Improve your style</a> with support for gradients, multiple backgrounds, pointer-events and more.
    </li>
<li>
      <b>Drag and Drop</b>: It has never been easier to <a href=http://hacks.mozilla.org/2009/12/file-drag-and-drop-in-firefox-3-6/ id=pe:y title="move files from your desktop to the Web">move files from your desktop to the Web</a>.&nbsp; You can now drag content into the browser window and leverage the FileAPI for instant access to that content.
    </li>
<li>
      <b>File API</b>: Support for the latest HTML5 specification <a href=http://hacks.mozilla.org/category/fileapi/as/brief/ id=tqjm title="allows Web applications to access local files">allows Web applications to access local files</a> and interact with them in new ways.
    </li>
<li>
      <b>Web Open Font Format (WOFF)</b>: Add a touch of typography to the Web with <a href=http://hacks.mozilla.org/2009/10/woff/ id=jous title="WOFF support, which brings new possibilities">WOFF support, which makes even more possible</a> than the existing support for OpenType and TrueType fonts.
    </li>
<li>
      <b>Device orientation</b>: Discover new ways to manipulate and interact with Web content with <a href=http://hacks.mozilla.org/2009/10/orientation-for-firefox/ id=fg0q title="Access to the orientation of supported laptops and devices">access to the orientation of supported laptops and devices</a>.
    </li>
<li>
      <b>XMLHttpRequest improvements and more&#8230; </b>
    </li>
</ul>
<p>
  For a more in-depth look at all the changes that developers will want to know about please check out <a href=https://developer.mozilla.org/en/Firefox_3.6_for_developers id=p38y title="Firefox 3.6 Notes for Developers">Firefox 3.6 for Developers</a> on MDC.  And if you haven&#8217;t already, go <a href="http://www.firefox.com">get Firefox 3.6 now</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2010/01/firefox-3-6-is-here/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Firebug 1.5: a closer look</title>
		<link>http://hacks.mozilla.org/2010/01/firebug-1-5-a-closer-look/</link>
		<comments>http://hacks.mozilla.org/2010/01/firebug-1-5-a-closer-look/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 00:49:37 +0000</pubDate>
		<dc:creator>Alix Franquet</dc:creator>
				<category><![CDATA[Firebug]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=3628</guid>
		<description><![CDATA[Firebug 1.5 was released yesterday on addons.mozilla.org, where you can now download it. It&#8217;s compatible with the upcoming Firefox 3.6.
If you&#8217;d like to take a more in-depth look at what&#8217;s new in Firebug 1.5, here&#8217;s a series of articles written by Firebug contributor Jan Odvarko (aka Honza):

Break On Next: breaking on the next executed script, [...]]]></description>
			<content:encoded><![CDATA[<p>Firebug 1.5 was <a href="http://hacks.mozilla.org/2010/01/firebug-1-5-released/">released yesterday</a> on addons.mozilla.org, where you can now <a href="http://bit.ly/8BbZFz">download it</a>. It&#8217;s compatible with the <a href="http://bit.ly/8a0Kux">upcoming Firefox 3.6</a>.</p>
<p>If you&#8217;d like to take a more in-depth look at what&#8217;s new in Firebug 1.5, here&#8217;s a series of articles written by Firebug contributor <a href="http://www.softwareishard.com/blog/category/firebug/">Jan Odvarko</a> (aka Honza):</p>
<ul>
<li><a href="http://www.softwareishard.com/blog/firebug/firebug-15-break-on-next/">Break On Next</a>: breaking on the next executed script, HTML mutation, XHR execution, or JavaScript error.</li>
<li><a href="http://www.softwareishard.com/blog/firebug/firebug-15-xhr-breakpoints/">XHR breakpoints</a>: creating conditional breakpoints in XMLHttpRequest execution.</li>
<li><a href="http://www.softwareishard.com/blog/firebug/new-firebug-15-debugging-cookies/">Debugging cookies</a>: using the Firecookie extension to break JS execution on cookie changes.</li>
<li><a href="http://www.softwareishard.com/blog/firebug/page-load-analysis-using-firebug/">Page load analysis</a>: 6 examples analyzing data provided by the Net panel.</li>
<li><a href="http://www.softwareishard.com/blog/firebug/firebug-http-time-monitor/">HTTP Time Monitor</a>: using Network Activity Observer for more accurate timing.</li>
<li><a href="http://www.softwareishard.com/blog/firebug/xml-explorer-for-firebug/">XML Response Explorer</a>: inspecting XML based responses received from the server.</li>
<li><a href="http://www.softwareishard.com/blog/firebug/firebug-net-panel-column-customization/">Network info header and column customization</a>: specifying what information to display within the Net panel.</li>
</ul>
<p>If you&#8217;ve had a chance to try the <a href="http://bit.ly/8BbZFz">new Firebug</a>, comment here or in the <a href="http://groups.google.com/group/firebug/t/4cdca29c3cf5d7cb">Firebug newsgroup</a> and tell the team what you think! </p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2010/01/firebug-1-5-a-closer-look/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Firebug 1.5 released!</title>
		<link>http://hacks.mozilla.org/2010/01/firebug-1-5-released/</link>
		<comments>http://hacks.mozilla.org/2010/01/firebug-1-5-released/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 20:32:23 +0000</pubDate>
		<dc:creator>Alix Franquet</dc:creator>
				<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[Firebug]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=3617</guid>
		<description><![CDATA[Editor’s note: today the Firebug team released Firebug 1.5. Check out Rob Campbell&#8217;s announcement reposted below.
I am very happy to be able to announce the release of Firebug 1.5.0 on addons.mozilla.org. This release represents a significant effort by the Firebug Working Group which saw the addition of some new faces over the last few months. [...]]]></description>
			<content:encoded><![CDATA[<p><em>Editor’s note: today the Firebug team released Firebug 1.5. Check out <a href="http://bit.ly/64VT4P">Rob Campbell&#8217;s announcement</a> reposted below.</em></p>
<p>I am very happy to be able to announce the release of <a href="http://bit.ly/8BbZFz">Firebug 1.5.0 on addons.mozilla.org</a>. This release represents a significant effort by the Firebug Working Group which saw the addition of some new faces over the last few months. </p>
<p>Here’s a quick run-down of some of the new features:</p>
<ul>
<li>Enhanced Inspector</li>
<li>More accurate Net panel timings</li>
<li>Break on HTML mutation</li>
<li>MathML and SVG namespace support</li>
<li>Break on XHR</li>
<li>Improved HTML editing</li>
<li>Persist buttons on Console and Net panel</li>
<li>Separate Computed CSS and Style subpanels</li>
<li>Many many bugfixes and stability improvements</li>
</ul>
<p>This is a huge release and these are just some of the highlights you’ll see in this new version. Please feel free to read the <a href="http://bit.ly/8v3dVo">Firebug 1.5 release notes</a> and <a href="http://bit.ly/6fPwbS">John Barton’s blog post</a> for more details.</p>
<p>As always, if you encounter any problems, don’t be shy about <a href="http://bit.ly/5Y5ny3">filing a bug</a>! We’ll be following up with quick point-releases if and when you find issues.</p>
<p>Special shout-outs and thanks to Mike Ratcliffe for the Inspector improvements, Steven Roussey for HTML editor improvements, SVG and MathML namespace patches, Honza for being awesome and John Barton for his tireless contributions.</p>
<p>If you’re new to Firebug, you might want to check out my <a href="http://bit.ly/91Sf0p">introduction to Firebug screencast</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2010/01/firebug-1-5-released/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>JavaScript speedups in Firefox 3.6</title>
		<link>http://hacks.mozilla.org/2010/01/javascript-speedups-in-firefox-3-6/</link>
		<comments>http://hacks.mozilla.org/2010/01/javascript-speedups-in-firefox-3-6/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 17:00:00 +0000</pubDate>
		<dc:creator>Alix Franquet</dc:creator>
				<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Firefox 3.6]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[TraceMonkey]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=3529</guid>
		<description><![CDATA[This post was written by David Mandelin who works on Mozilla’s JavaScript team.
Firefox 3.5 introduced TraceMonkey, our new JavaScript engine that traces loops and JIT compiles them to native (x86/ARM) code. Many JavaScript programs ran 3-4x faster in TraceMonkey compared to Firefox 3. (See our previous article for technical details.) 
For JavaScript performance in Firefox [...]]]></description>
			<content:encoded><![CDATA[<p><em>This post was written by <a href="http://blog.mozilla.com/dmandelin/">David Mandelin</a> who works on Mozilla’s JavaScript team.</em></p>
<p>Firefox 3.5 introduced TraceMonkey, our new JavaScript engine that traces loops and JIT compiles them to native (x86/ARM) code. Many JavaScript programs ran 3-4x faster in TraceMonkey compared to Firefox 3. (See our <a href="http://hacks.mozilla.org/2009/07/tracemonkey-overview/">previous article</a> for technical details.) </p>
<p>For JavaScript performance in Firefox 3.6, we focused on the areas that we thought needed further improvement the most:</p>
<ul>
<li>Some JavaScript code was not trace-compiled in Firefox 3.5. Tracing was disabled by default for Firefox UI JavaScript and add-on JavaScript, so those programs did not benefit from tracing. Also, many advanced JavaScript features were not trace-compiled. For Firefox 3.6, we wanted to trace more programs and more JS features.</li>
<li>Animations coded with JavaScript were often choppy because of garbage collection pauses. We wanted to improve GC performance to make pauses shorter and animations smoother.</li>
</ul>
<p>In this article, I&#8217;ll explain the most important JS performance improvements that come with Firefox 3.6. I&#8217;ll focus on listing what kinds of JS code get faster, including sample programs that show the improvements Fx3.6 makes over Fx3.5. </p>
<h3>JIT for Browser UI JavaScript</h3>
<p>Firefox runs JavaScript code in one of two contexts:<em>content</em> and <em>chrome</em> (no relation to Google Chrome). JavaScript that is part of web content runs in a <em>content</em> context. JavaScript that is part of the browser UI or browser add-ons runs in a <em>chrome</em> context and has extra privileges. For example, chrome JS can alter the main browser UI, but content JS is not allowed to.
</p>
<p>The TraceMonkey JIT can be enabled or disabled separately for content and chrome JS using <code>about:config</code>. Because bugs affecting chrome JS are a greater risk for security and reliability, in Firefox 3.5 we chose to disable the JIT for chrome JS by default. After extensive testing, we&#8217;ve decide to enable the JIT for chrome JS by default, something we did not have time to fully investigate for Fx3.5. Turning on the JIT for chrome should make the JS behind the Firefox UI and add-ons run faster. This difference is probably not very noticeable for general browser usage, because the UI was designed and coded to perform well with the older JS engines. The difference should be more noticeable for add-ons that do heavy JS computation.</p>
<style>
table.ac_table         { margin-left: auto; margin-right: auto }
table.ac_table caption { margin-bottom: 1em }
table.ac_table tr th   { border-bottom: solid 1px black }
table.ac_table th      { border-bottom: solid 1px black }
table.ac_table tr td   { padding-left: 1ex; padding-right: 1ex; text-align: center; font-family: monospace }
table.ac_table tr th   { padding-left: 1ex; padding-right: 1ex; text-align: center }
</style>
<table class='ac_table'>
<tr>
<th>Option
<th>Fx3.5 Default
<th>Fx3.6 Default</tr>
<tr>
<td>javascript.options.jit.chrome
<td>false
<td>true</tr>
<tr>
<td>javascript.options.jit.content
<td>true
<td>true</tr>
<caption><strong><code>about:config</code> options for the JIT</strong></caption>
</table>
<h3>Garbage Collector Performance</h3>
<p>JavaScript is a garbage-collected language, so periodically the JavaScript engine must reclaim unused memory. Our garbage collector (GC) pauses all JavaScript programs while it works. This is fine as long as the pauses are &#8220;short&#8221;. But if the pauses are even a little too long, they can make animations jerky. Animations need to run at 30-60 frames per second to look smooth, which means it should take no longer than 17-33 ms to render one frame. Thus, GC pauses longer than 40 ms cause jerkiness, while pauses under 10 ms should be almost unnoticeable. In Firefox 3.5, pause times were noticeably long, and JavaScript animations are increasingly common on the web, so reducing pause times was a major goal for JavaScript in Firefox 3.6.</p>
<div style='float:left'>
<iframe src='http://people.mozilla.com/~afranquet/clock.html' width='280' height='320' style='border: 3px solid gray; margin-right: 20px;'></iframe></p>
<p style='margin-left: auto; margin-right: auto; text-align: center'>
<strong>Demo: GC Pauses and Animation</strong>
  </p>
</div>
<p><strong>Demo.</strong><br />
The spinning dial animation shown here illustrates pause times. Besides animating the dial, this demo creates one million 100-character strings per second, so it requires frequent GC. The <em>frame delay</em> meter gives the average time between frames in milliseconds. The <em>estimated GC delay</em> meter gives the average estimated GC delay, based on the assumption that if a frame has a delay of 1.7 times the average delay or more, then exactly one GC ran during that frame. (This procedure may not be valid for other browsers, so it is not valid for comparing different browsers. Note also that the GC time also depends on other live JavaScript sessions, so for a direct comparison of two browsers, have the same tabs open in each.) On my machine, I get an estimated GC delay of about 80 ms in Fx3.5, but only 30 ms in Fx3.6.</p>
<p>But it&#8217;s a lot easier to see the difference by opening the demo in Fx3.5, watching it a bit, and then trying it in Fx3.6.<br />
In Fx3.5, I see frequent pauses and the animation looks noticeably jerky. In Fx3.6, it looks pretty smooth, and it&#8217;s hard for me even to tell exactly when the GC is running.
</p>
<p><strong>How Fx3.6 does it better.</strong> We&#8217;ve made many improvements to the garbage collector and memory allocator. I want to give a little more technical details on the big two changes that really cut our pause times.</p>
<p>First, we noticed that a large fraction of the pause time was spent calling <code>free</code> to reclaim the unused memory. We can&#8217;t do much to make freeing memory faster, but we realized we could do it on a separate thread. In Fx3.6, the main JS thread simply adds unused memory chunks to a queue, and another thread frees them during idle time or on a separate processor. This means machines with 2 or more cores will benefit more from this change. But even when one core, freeing might be delayed to an idle time when it will not affect scripts.</p>
<p>Second, we knew that in Fx3.5 running GC clears out all the native code compiled by the JIT as well as some other caches that speed up JS. The reason is that the tracing JIT and GC did not know about each other, so if the GC ran, it might reclaim objects being used by a compiled trace. The result was that immediately after a GC, JS ran a bit slower as the caches and compiled traces were built back up. This would be experienced as either an extended GC pause or a brief hiccup of slow animation right after the GC pause. In Fx3.6, we taught the GC and the JIT to work together, and now the GC does not clear caches or wipe out native code, so it resumes running normally right after GC.</p>
<h3>Tracing More JavaScript Constructs</h3>
<p>In my <a href="http://hacks.mozilla.org/2009/07/tracemonkey-overview/">article on TraceMonkey</a> for the Fx3.5 release, I noted that certain code constructs, such as the <code>arguments</code> object, were not traced and did not get performance improvements from the JIT. A major goal for JS in Fx3.6 was to trace more stuff, so more programs can run faster. We do trace more stuff now, in particular:</p>
<ul>
<li><strong>DOM Properties.</strong> DOM objects are special and harder for the trace compiler to work with. For Fx3.5, we implemented tracing of DOM methods, but not DOM properties. Now we trace DOM properties (and other &#8220;native&#8221; C++ getters and setters) as well. We still do not trace scripted getters and setters.</li>
<li><strong>Closures.</strong> Fx3.5 traced only a few operations involving closures (by which I mean functions that refer to variables defined in lexically enclosing functions). Fx3.6 can trace more programs that use closures. The main operation that is still not traced yet is creating an anonymous function that modifies closure variables. But calling such a function and actually writing to the closure variables are traced.</li>
<li><strong><code>arguments</code>.</strong> We now trace most common uses of the <code>arguments</code> keyword. &#8220;Exotic&#8221; uses, such as setting elements of <code>arguments</code>, are not traced.</li>
<li><strong><code>switch</code>.</strong> We have improved performance when tracing <code>switch</code> statements that use densely packed numeric case labels. These are particularly important for emulators and VMs.</li>
</ul>
<p>These improvements are particularly important for jQuery and Dromaeo, which heavily use <code>arguments</code>, closures, and the DOM. I suspect many other complex JavaScript applications will also benefit. For example, we recently heard from the author that <a href='http://stackulator.com/rtree/'>this R-tree library</a> performs much better in Fx3.6.</p>
<p>Here is a pair of demos of new things we trace. The first sets a DOM property in a loop. The second calls a <strong>sum</strong> function implemented with <code>arguments</code> I get a speedup of about 2x for both of them in Fx3.6 vs. Fx3.5.</p>
<p><script type="text/javascript">
function sum() {
  var ans = 0;
  for (var i = 0; i < arguments.length; ++i)
   ans += arguments[i];
  return ans;
}
function runTracingTests() {
  var t0 = new Date;
  var domObj = document.getElementById('dom_time');
  for (var i = 0; i < 600000; ++i)
    domObj.x = 10;
  var t1 = new Date;
  for (var i = 0; i < 30000; ++i)
    sum(1, 2, 3, 4, 5);
  var t2 = new Date;
  document.getElementById('dom_time').innerHTML = (t1-t0) + ' ms';
  document.getElementById('sum_time').innerHTML = (t2-t1) + ' ms';
}
function showCode(id) {
  var e = document.getElementById(id);
  if (e.style['visibility'] == 'hidden') {
    e.style['visibility'] = 'visible';
    e.style['height'] = 'auto';
  } else {
    e.style['visibility'] = 'hidden';
    e.style['height'] = '0px';
  }
}
</script></p>
<div style='border: 1px solid black; padding: 8px; margin: 12px'>
<p><strong>Demo: Fx3.6 Tracing DOM properties and <code>arguments</code></strong></p>
<p><button onclick='runTracingTests()'>Run</button><br />
  <button onclick='showCode("tracingTestsCode")'>Show/Hide Code</button></p>
<p>DOM Property Set: <strong><span id='dom_time' </span></strong></p>
<p>Sum using <code>arguments</code>: <strong><span id='sum_time'> </span></strong></p>
<pre style='border: 1px solid gray; visibility: hidden; height: 0px' id='tracingTestsCode'>
function sum() {
  var ans = 0;
  for (var i = 0; i < arguments.length; ++i)
    ans += arguments[i];
  return ans;
}

function runTracingTests() {
  var t0 = new Date;

  var domObj = document.getElementById('dom_time');
  for (var i = 0; i < 600000; ++i)
    domObj.x = 10;

  var t1 = new Date;

  for (var i = 0; i < 30000; ++i)
    sum(1, 2, 3, 4, 5);

  var t2 = new Date;

  document.getElementById('dom_time').innerHTML = (t1-t0) + ' ms';
  document.getElementById('sum_time').innerHTML = (t2-t1) + ' ms';
}
</pre>
</div>
<h3>String and RegExp Improvements</h3>
<p>Fx3.6 includes several improvements to string and regular expression performance. For example, the regexp JIT compiler now supports a larger class of regular expressions, including the ever-popular <code>\w+</code>. We also made some of our basic operations faster, like <code>indexOf</code>, <code>match</code>, and <code>search</code>. Finally, we made concatenating sequences of several strings inside a function (a common operation in building up HTML or other kinds of textual output) much faster.</p>
<p style='margin-left: 4ex; margin-right: 4ex'>
<em>Technical aside on how we made string concatenation faster:</em> The C++ function that concatenates two strings S1 and S2 does this: Allocate a buffer big enough to hold the result, then copy the characters of S1 and S2 into the buffer. To concatenate more than two strings, as in JS <code>s + "foo" + t</code>, Fx3.5 simply concatenates two at a time from left to right.</p>
<p style='margin-left: 4ex; margin-right: 4ex'>
Using the Fx3.5 algorithm, to concatenate N strings each of length K, we need to do N-1 memory allocations, and all but one of them are for temporary strings. Worse, the first two input strings are copied N-1 times, the next one is copied N-2 times, and so on. The total number of characters copied is K(N-1)(N+2)/2, which is O(N^2).
</p>
<p style='margin-left: 4ex; margin-right: 4ex'>
Clearly, we can do a lot better. The minimum work we can do is to copy each input string exactly once to the output string, for a total of KN characters copied. Fx3.6 achieves this by detecting sequences of concatenation in JS programs and combining the entire sequence into one operation that uses the optimal algorithm.</p>
<p>Here are a few string benchmarks you can try that are faster in Fx3.6:</p>
<p><script type="text/javascript">
function words() {
  var text_unit = 'word ';
  var text = '';
  for (var i = 0; i < 1000; ++i)
    text += text_unit;
  var t0 = new Date;
  for (var i = 0; i < 300; ++i)
    text.match(/\w+/g)
  return new Date - t0;
}
function indexOf_foo() {
  var text_unit = 'bar baz quux ';
  var text = '';
  for (var i = 0; i < 1000; ++i)
    text += text_unit;
  text += 'foo';
  var t0 = new Date;
  for (var i = 0; i < 3000; ++i)
    text.indexOf('foo')
  return new Date - t0;
}
function match_foo() {
  var text_unit = 'bar baz quux ';
  var text = '';
  //for (var i = 0; i < 1000; ++i)
  //  text += text_unit;
  //text += 'foo';
  text = 'bar baz qux quux foo';
  var t0 = new Date;
  for (var i = 0; i < 100000; ++i)
    text.search('foo')
  return new Date - t0;
}
function runStringTests() {
  var dt1 = words();
  var dt2 = indexOf_foo();
  var dt3 = match_foo();
  var dt4 = buildHTML();
  document.getElementById('words_time').innerHTML = dt1 + ' ms';
  document.getElementById('indexOf_foo_time').innerHTML = dt2 + ' ms';
  document.getElementById('match_foo_time').innerHTML = dt3 + ' ms';
  document.getElementById('buildHTML_time').innerHTML = dt4 + ' ms';
}
function buildHTML(url, text, style) {
  var t0 = new Date;
  for (var i = 0; i < 100000; ++i)
    var q = '<a href="' + url + ' style="' + style + '">' + text + '</a>';
  return new Date - t0;
}
</script></p>
<div style='border: 1px solid black; padding: 8px; margin: 12px'>
<p><strong>Demo: Fx3.6 String Operations</code></strong></p>
<p><button onclick='runStringTests()'>Run</button><br />
  <button onclick='showCode("stringTestsCode")'>Show/Hide Code</button></p>
<p>/\w+/: <strong><span id='words_time'> </span></strong></p>
<p>indexOf('foo'): <strong><span id='indexOf_foo_time'> </span></strong></p>
<p>match('foo'): <strong><span id='match_foo_time'> </span></strong></p>
<p>Build HTML: <strong><span id='buildHTML_time'> </span></strong></p>
<pre style='border: 1px solid gray; visibility: hidden; height: 0px' id='stringTestsCode'>
function words() {
  var text_unit = 'word ';
  var text = '';
  for (var i = 0; i < 1000; ++i)
    text += text_unit;

  var t0 = new Date;
  for (var i = 0; i < 300; ++i)
    text.match(/\w+/g)
  return new Date - t0;
}

function indexOf_foo() {
  var text_unit = 'bar baz quux ';
  var text = '';
  for (var i = 0; i < 1000; ++i)
    text += text_unit;
  text += 'foo';

  var t0 = new Date;
  for (var i = 0; i < 3000; ++i)
    text.indexOf('foo')
  return new Date - t0;
}

function match_foo() {
  var text_unit = 'bar baz quux ';
  var text = '';
  //for (var i = 0; i < 1000; ++i)
  //  text += text_unit;
  //text += 'foo';
  text = 'bar baz qux quux foo';

  var t0 = new Date;
  for (var i = 0; i < 100000; ++i)
    text.search('foo')
  return new Date - t0;
}

function runStringTests() {
  var dt1 = words();
  var dt2 = indexOf_foo();
  var dt3 = match_foo();
  var dt4 = buildHTML();

  document.getElementById('words_time').innerHTML = dt1 + ' ms';
  document.getElementById('indexOf_foo_time').innerHTML = dt2 + ' ms';
  document.getElementById('match_foo_time').innerHTML = dt3 + ' ms';
  document.getElementById('buildHTML_time').innerHTML = dt4 + ' ms';
}

function buildHTML(url, text, style) {
  var t0 = new Date;
  for (var i = 0; i < 100000; ++i)
    var q = '<a href="' + url + ' style="' + style + '">' + text + '</a>';
  return new Date - t0;
}
</pre>
</div>
<h3>Final Thoughts and Next Steps</h3>
<p>We also made a lot of little improvements that don't fit into the big categories above. Most importantly, Adobe, Mozilla, Intel, Sun, and other contributors continue to improve <em>nanojit</em>, the compiler back-end used by TraceMonkey. We have improved its use of memory, made trace recording and compiling faster, and also improved the speed of the generated native code. A better <em>nanojit</em> gives a boost to all JS that runs in the JIT.</p>
<p>There are two big items that didn't make the cut for Fx3.6, but will be in the next version of Firefox and are already available in nightly builds:</p>
<ul>
<li><strong>JITting recursion.</strong> Recursive code, like explicit looping code, is likely to be hot code, so it should be JITted. Nightly builds JIT directly recursive functions. Mutual recursion (g calls f calls g) is not traced yet.</li>
<li><strong>AMD x64 nanojit backend.</strong> Nanojit now has a backend that generates AMD x64 code, which gives the possibility of better performance on that plaform.</li>
</ul>
<p>And if you try a nightly build, you'll find that many of these demos are already even faster than in Fx3.6!</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2010/01/javascript-speedups-in-firefox-3-6/feed/</wfw:commentRss>
		<slash:comments>69</slash:comments>
		</item>
		<item>
		<title>offline web applications</title>
		<link>http://hacks.mozilla.org/2010/01/offline-web-applications/</link>
		<comments>http://hacks.mozilla.org/2010/01/offline-web-applications/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 19:39:26 +0000</pubDate>
		<dc:creator>Paul Rouget</dc:creator>
				<category><![CDATA[Demo]]></category>
		<category><![CDATA[Featured Demo]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Offline]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=3188</guid>
		<description><![CDATA[The network is a key component of any web application, whether it is used to download JavaScript, CSS, and HTML source files and accompanying resources (images, videos, &#8230;) or to reach web services (XMLHttpRequest and &#60;forms&#62;).
Yet having offline support for web applications can be very useful to users. Imagine, for example, a webmail application that [...]]]></description>
			<content:encoded><![CDATA[<p>The network is a key component of any web application, whether it is used to download JavaScript, CSS, and HTML source files and accompanying resources (images, videos, &#8230;) or to reach web services (XMLHttpRequest and <code>&lt;forms&gt;</code>).</p>
<p>Yet having offline support for web applications can be very useful to users. Imagine, for example, a webmail application that allows users to read emails already in their inbox and write new messages even when they are not connected.</p>
<p>The mechanism used to support offline web applications can also be used to improve an application&#8217;s performance by storing data in the cache or to make data persistent between user sessions and when reloading and restoring pages.</p>
<h3>Demo: a To Do List Manager</h3>
<p>To see an offline web application in action, watch Vivien Nicolas&#8217; demo, which shows a to do list manager working online and offline on an N900 running Firefox:</p>
<p><video width="500" controls><br />
<source src="http://videos.mozilla.org/serv/marketing/offlinewebapps/20100105_offlinewebapps.ogv" type="video/ogg"/><br />
<source src="http://videos.mozilla.org/serv/marketing/offlinewebapps/20100105_offlinewebapps.mp4" type="video/mp4"/><br />
<embed src="http://www.youtube.com/v/Kt8kayGR2Fw&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="500"></embed><br />
</video></p>
<p>You can also check out the <a href="http://demos.hacks.mozilla.org/openweb/todo/">live demo</a> of the application.</p>
<h3>Creating your Own Offline Application</h3>
<p>For a web application to work offline, you need to consider three things:</p>
<ul>
<li>Store user inputs through <a href="https://developer.mozilla.org/en/DOM/Storage">localStorage</a></li>
<li>Define which files should be cached via a <a href="https://developer.mozilla.org/en/Offline_resources_in_Firefox#Specifying_a_cache_manifest">manifest file</a></li>
<li>Manage connection changes with <a href="https://developer.mozilla.org/en/Online_and_offline_events">online and offline events</a></li>
</ul>
<p>Let&#8217;s see how to use each of these components.</p>
<h3>Storage: Persistent Data</h3>
<p><a href="https://developer.mozilla.org/en/DOM/Storage">DOM storage</a> lets you store data between browser sessions, share data between tabs and prevent data loss (for example from page reloads or browser restarts). The data are stored as strings (for example a JSONified JavaScript object) in a Storage object.</p>
<p>There are two kinds of storage global objects: <code>sessionStorage</code> and <code>localStorage</code>.</p>
<ul>
<li><code>sessionStorage</code> maintains a storage area that&#8217;s available for the duration of the page session. A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window causes a new session to be initiated.</li>
<li><code>localStorage</code> maintains a storage area that can be used to hold data over a long period of time (e.g. over multiple pages and browser sessions). It&#8217;s not destroyed when the user closes the browser or switches off the computer.</li>
</ul>
<p>Both localStorage and sessionStorage use the following API:</p>

<div class="wp_syntax"><div class="code"><pre class="idl" style="font-family:monospace;">window.localStorage and window.sessionStorage <span style="color: #808080;">&#123;</span>
  <span style="color: #0000ec;">long</span> length<span style="color: #66cc66;">;</span> <span style="color: #3f7f5f;">// Number of items stored</span>
  <span style="color: #0000ec;">string</span> key<span style="color: #808080;">&#40;</span><span style="color: #0000ec;">long</span> index<span style="color: #808080;">&#41;</span><span style="color: #66cc66;">;</span> <span style="color: #3f7f5f;">// Name of the key at index</span>
  <span style="color: #0000ec;">string</span> getItem<span style="color: #808080;">&#40;</span><span style="color: #0000ec;">string</span> key<span style="color: #808080;">&#41;</span><span style="color: #66cc66;">;</span> <span style="color: #3f7f5f;">// Get value of the key</span>
  <span style="color: #0000ec;">void</span> setItem<span style="color: #808080;">&#40;</span><span style="color: #0000ec;">string</span> key, <span style="color: #0000ec;">string</span> data<span style="color: #808080;">&#41;</span><span style="color: #66cc66;">;</span> <span style="color: #3f7f5f;">// Add a new key with value data</span>
  <span style="color: #0000ec;">void</span> removeItem<span style="color: #808080;">&#40;</span><span style="color: #0000ec;">string</span> key<span style="color: #808080;">&#41;</span><span style="color: #66cc66;">;</span> <span style="color: #3f7f5f;">// Remove the item key</span>
  <span style="color: #0000ec;">void</span> clear<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #66cc66;">;</span> <span style="color: #3f7f5f;">// Clear the storage</span>
<span style="color: #808080;">&#125;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p>Here is an example showing how to store and how to read a string:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// save the string</span>
<span style="color: #003366; font-weight: bold;">function</span> saveStatusLocally<span style="color: #009900;">&#40;</span>txt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  window.<span style="color: #660066;">localStorage</span>.<span style="color: #660066;">setItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;status&quot;</span><span style="color: #339933;">,</span> txt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// read the string</span>
<span style="color: #003366; font-weight: bold;">function</span> readStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">return</span> window.<span style="color: #660066;">localStorage</span>.<span style="color: #660066;">getItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;status&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that the storage properties are limited to an HTML5 origin (scheme + hostname + non-standard port). This means that window.localStorage from http://foo.com is a different instance of window.localStorage from http://bar.com. For example, http://google.com can&#8217;t access the storage of http://yahoo.com.</p>
<h3>Are We Offline?</h3>
<p>Before storing data, you may want to <a href="https://developer.mozilla.org/en/Online_and_offline_events">know if the user is online or not</a>. This can be useful, for example, to decide whether to store a value locally (client side) or to send it to the server.</p>
<p>Check if the user is online with the <code>navigator.onLine</code> property.<br />
In addition, you can be notified of any connectivity changes by listening to the <code>online</code> and <code>offline</code> events of the window element.</p>
<p>Here is a very simple piece of JavaScript code, which sends your status to a server (à la twitter).</p>
<ul>
<li>If you set your status and you&#8217;re online, it sends the status.</li>
<li>If you set your status and you&#8217;re offline, it stores your status.</li>
<li>If you go online and have a stored status, it sends the stored status.</li>
<li>If you load the page, are online, and have a stored status, it sends the stored status.</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> whatIsYourCurrentStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">status</span> <span style="color: #339933;">=</span> window.<span style="color: #000066;">prompt</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;What is your current status?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000066;">status</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>navigator.<span style="color: #660066;">onLine</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    sendToServer<span style="color: #009900;">&#40;</span><span style="color: #000066;">status</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    saveStatusLocally<span style="color: #009900;">&#40;</span><span style="color: #000066;">status</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> sendLocalStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">status</span> <span style="color: #339933;">=</span> readStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">status</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    sendToServer<span style="color: #009900;">&#40;</span><span style="color: #000066;">status</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    window.<span style="color: #660066;">localStorage</span>.<span style="color: #660066;">removeItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;status&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
window.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;load&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>navigator.<span style="color: #660066;">onLine</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     sendLocalStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
window.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;online&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  sendLocalStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
window.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;offline&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;You're now offline. If you update your status, it will be sent when you go back online&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Offline Resources: the Cache Manifest</h3>
<p>When offline, a user&#8217;s browser can&#8217;t reach the server to get any files that might be needed. You can&#8217;t always count on the browser&#8217;s cache to include the needed resources because the user may have cleared the cache, for example. This is why you need to define explicitly which files must be stored so that all needed files and resources are available when the user goes offline: HTML, CSS, JavaScript files, and other resources like images and video.</p>
<p>The <a href="https://developer.mozilla.org/en/Offline_resources_in_Firefox#Specifying_a_cache_manifest">manifest file</a> is specified in the HTML and contains the explicit list of files that should be cached for offline use by the application.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html</span> <span style="color: #000066;">manifest</span>=<span style="color: #ff0000;">&quot;offline.manifest&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></pre></div></div>

<p>Here is an example of the contents of a manifest file:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">CACHE MANIFEST
fonts/MarketingScript.ttf
css/main.css
css/fonts.css
img/face.gif
js/main.js
index.xhtml</pre></div></div>

<p>The MIME-Type type of the manifest file must be: <code>text/cache-manifest</code>.</p>
<p><a href="https://developer.mozilla.org/en/Offline_resources_in_Firefox#Specifying_a_cache_manifest">See the documentation</a> for more details on the manifest file format and cache behavior.</p>
<h3>Summary</h3>
<p>The key components you should remember to think about when making your application work offline are to <a href="https://developer.mozilla.org/en/DOM/Storage">store the user inputs</a> in localStorage, create a <a href="https://developer.mozilla.org/en/Offline_resources_in_Firefox#Specifying_a_cache_manifest">cache manifest</a> file, and <a href="https://developer.mozilla.org/en/Online_and_offline_events">monitor connection changes</a>. </p>
<p>Visit the <a href="https://developer.mozilla.org/">Mozilla Developer Center</a> for the complete documentation.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2010/01/offline-web-applications/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
<enclosure url="http://videos.mozilla.org/serv/marketing/offlinewebapps/20100105_offlinewebapps.ogv" length="7544233" type="video/ogg" />
		</item>
		<item>
		<title>Hacks v2.0</title>
		<link>http://hacks.mozilla.org/2010/01/hacks-v2-0/</link>
		<comments>http://hacks.mozilla.org/2010/01/hacks-v2-0/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 20:06:20 +0000</pubDate>
		<dc:creator>Jay Patel</dc:creator>
				<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[MDN]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=3337</guid>
		<description><![CDATA[As you may have noticed, Hacks looks a bit different today.  That’s because we’ve completely redesigned the site. We wanted to make it easier for everyone to not only enjoy the new content we publish, but also to find past articles and demos.  With Firefox 3.6 just around the corner, it&#8217;s the perfect time [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: left;">As you may have noticed, Hacks looks a bit different today.  That’s because we’ve completely redesigned the site. We wanted to make it easier for everyone to not only enjoy the new content we publish, but also to find past articles and demos.  With <a href="https://developer.mozilla.org/En/Firefox_3.6_for_developers">Firefox 3.6 just around the corner</a>, it&#8217;s the perfect time to rediscover Hacks!</p>
<p style="text-align: left;">Thanks go out to <a href="http://howsehold.org">Chris Howse</a> for his design expertise, <a href="http://focalcurve.com">Craig Cook</a> and <a href="http://themattharris.com">Matt Harris</a> for their development work, and everyone else that helped out.  What a difference a redesign like this can make:</p>
<p style="text-align: center;"><span style="color: #000000;"><strong><a rel="attachment wp-att-3360" href="http://hacks.mozilla.org/2010/01/hacks-v2-0/hacks_v1_cropped-2/"><img class="size-large wp-image-3360 aligncenter" title="hacks_v1_cropped" src="http://hacks.mozilla.org/wp-content/uploads/2010/01/hacks_v1_cropped1-500x295.png" alt="" width="500" height="295" /></a></strong></span><span style="color: #000000;"><strong>Hacks v1.0</strong></span></p>
<p>Chris Blizzard started the Hacks blog back in June 2009 as part of the<a href="http://hacks.mozilla.org/2009/06/35-days/"> &#8220;35 days of Firefox&#8221; </a>campaign to give developers a look at the latest features and web technology upgrades that were coming in Firefox 3.5.  Since then, we have continued to reach out to web developers and collaborate with them to put together amazing posts.</p>
<p>With a collection of <a href="http://hacks.mozilla.org/2009/11/building-beautiful-buttons-with-css-gradients/">helpful tutorials</a>, <a href="http://hacks.mozilla.org/category/standards/">updates on web standards</a>, and <a href="http://hacks.mozilla.org/demos/">technology demos</a>, Hacks has become a popular destination for anyone interested in keeping up with innovation on the web.  We cover a wide range of topics, including everything from <a href="http://hacks.mozilla.org/?s=html5">new possibilities with HTML5</a> to the <a href="http://hacks.mozilla.org/2009/11/whats-new-in-firebug-1-5/">latest version of Firebug</a>. Developers get to see what is possible on the web today, while everyone gets a glimpse of what the future holds for Firefox and the open web.</p>
<p>Hacks has ignited a renewed focus on web developer outreach and collaboration.  In addition to the blog, you can follow us on <a href="http://twitter.com/mozhacks">Twitter</a>, watch videos and demos on <a href="http://www.youtube.com/user/mozhacks">YouTube</a>, and even subscribe to <a href="http://hacks.mozilla.org/2009/12/the-new-abouthacks-newsletter/">the about:hacks newsletter</a>.  Our goal is to inform and inspire web developers to help us <a href="http://hacks.mozilla.org/2009/10/mozilla-developer-network/">establish a strong Mozilla Developer Network (MDN)</a>.</p>
<p>We have big plans for MDN in 2010, and Hacks v2.0 is just the beginning!  Enjoy the new website and please <a href="http://bit.ly/hacksfeedback">share your thoughts and feedback with us&#8230; </a></p>
<p>Happy New Year!</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2010/01/hacks-v2-0/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>the new about:hacks newsletter</title>
		<link>http://hacks.mozilla.org/2009/12/the-new-abouthacks-newsletter/</link>
		<comments>http://hacks.mozilla.org/2009/12/the-new-abouthacks-newsletter/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 19:04:49 +0000</pubDate>
		<dc:creator>Alix Franquet</dc:creator>
				<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[MDN]]></category>
		<category><![CDATA[about:hacks]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=3165</guid>
		<description><![CDATA[Yesterday, we published the  first issue of about:hacks, Mozilla&#8217;s newsletter for web developers. If you asked to receive news and updates from Mozilla in our November survey, it should be waiting for you in your inbox.

About:hacks will be published monthly, and will include demos, tutorials, Firefox release information, the latest on web standards and [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday, we published the  <a href="http://bit.ly/abouthacksissue1">first issue of about:hacks</a>, Mozilla&#8217;s newsletter for web developers. If you asked to receive news and updates from Mozilla in our <a href="http://bit.ly/mozmdn">November survey</a>, it should be waiting for you in your inbox.</p>
<p><img class="aligncenter size-full wp-image-3166" title="abouthacksheader" src="http://hacks.mozilla.org/wp-content/uploads/2009/12/abouthacksheader.png" alt="abouthacksheader" width="500" /></p>
<p>About:hacks will be published monthly, and will include demos, tutorials, Firefox release information, the latest on web standards and developer tools, and updates on the Mozilla Developer Network.</p>
<p>This <a href="http://bit.ly/abouthacksissue1">first issue</a> includes, among other things, a demo that combines Processing and multi-touch, an HTML5 video tutorial, a preview of Firebug 1.5 and Eventbug, and a sneak peek at what&#8217;s going on behind the scenes at Mozilla.</p>
<p>We know you may already be reading the <a href="http://hacks.mozilla.org/">hacks blog</a>, following <a href="http://twitter.com/mozhacks">@mozhacks on Twitter</a>, and maybe even subscribing to <a href="http://www.youtube.com/mozhacks">mozhacks on YouTube</a>, so we&#8217;ll make sure the newsletter is a good mix of original content and a recap of some of the most important things that happened in the previous month.</p>
<p>This is why your  <a href="http://www.surveygizmo.com/s/214365/abouthacks1">feedback</a> is very important: what did you like? what would you change? what would you like to see in the next issue? Let us know and we&#8217;ll make sure to write the newsletter you want to read!</p>
<p>If you haven&#8217;t seen about:hacks, <a href="http://bit.ly/abouthacksissue1">check it out</a>, and if you like it, <a href="http://bit.ly/abouthacks">subscribe</a> to get the next issue in January.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/12/the-new-abouthacks-newsletter/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>autobuffering video in Firefox</title>
		<link>http://hacks.mozilla.org/2009/12/autobuffering-video-in-firefox/</link>
		<comments>http://hacks.mozilla.org/2009/12/autobuffering-video-in-firefox/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 00:56:05 +0000</pubDate>
		<dc:creator>Christopher Blizzard</dc:creator>
				<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Standards]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=3103</guid>
		<description><![CDATA[John Gruber recently wrote up an article titled Why the HTML5 ‘Video’ Element Is Effectively Unusable, Even in the Browsers Which Support It
He&#8217;s mostly upset that browsers don&#8217;t respect the autobuffer attribute.  Or, really, that browsers autobuffer by default.  Safari and Chrome do apparently autobuffer by default, but he incorrectly says that Firefox [...]]]></description>
			<content:encoded><![CDATA[<p>John Gruber recently wrote up an article titled <em><a href="http://daringfireball.net/2009/12/html5_video_unusable">Why the HTML5 ‘Video’ Element Is Effectively Unusable, Even in the Browsers Which Support It</a></em></p>
<p>He&#8217;s mostly upset that browsers don&#8217;t respect the autobuffer attribute.  Or, really, that browsers autobuffer by default.  Safari and Chrome do apparently autobuffer by default, but he incorrectly says that Firefox does as well.  Just to be clear: <em>Firefox does not autobuffer by default, nor does it autoplay by default.</em>  I&#8217;m not sure how his testing led him to believe that it does, but I wrote up some examples to show that it does not.  Here are three examples:</p>
<ul>
<li><a href="http://people.mozilla.org/~blizzard/gruber-test/test_noautobuffer.html">Video test without the autobuffer attribute.</a>  (Load this first to avoid caching issues!)</li>
<li><a href="http://people.mozilla.org/~blizzard/gruber-test/test_autobuffer.html">Video test with the autobuffer attibute.</a></li>
<li><a href="http://people.mozilla.org/~blizzard/gruber-test/test_both.html">Video test with one of each.</a></li>
</ul>
<p>The best way to test this is to load your favorite browser and clear its cache.  Then load your favorite network monitor.  Then load the <a href="http://people.mozilla.org/~blizzard/gruber-test/test_noautobuffer.html">page without the autobuffer attribute set.</a>  You can see how much bandwidth the browser is actually using via your external tool.</p>
<p>Of the browsers I tried, only Firefox gives useful feedback on how much is actually buffered as part of its native control set so you have to go to external tools to be able to tell what&#8217;s going on with anything else.  If you load the <a href="http://people.mozilla.org/~blizzard/gruber-test/test_both.html">page with two videos &#8211; one with autobuffer set and one without -</a> you can mouse over them in Firefox and see how one has buffered and one hasn&#8217;t.</p>
<p>The video on that test page is pretty big &#8211; around 115mb &#8211; so you can really tell when it&#8217;s buffering even on a fast network.</p>
<p>Here&#8217;s what my testing shows:</p>
<ul>
<li>Safari for desktops downloads a lot of data.  (<a href="http://joehewitt.com/post/on-middle-men/">I don&#8217;t have an iPhone</a> so I can&#8217;t test that.)</li>
<li>Chrome downloads a lot of data.</li>
<li>Firefox loads a small amount of data and then stops.</li>
<li><b>Update:</b> According to <a href="http://twitter.com/foolip/status/6923376494">Philip Jägenstedt, Opera will also do what Firefox does</a> when they include open video support.
</ul>
<p>This is consistent with what I know about Safari.  I talked with one of the Apple WebKit engineers a few months ago and he said that Safari for desktops buffered by default and said that Safari for mobile does not.</p>
<p>There might be some confusion because Firefox does make a couple of network requests when you include a video tag.  This is done for two reasons:</p>
<ul>
<li>To get the first frame of the video and it&#8217;s size.  It will only download a small part of the beginning of the video to get the first frame and then stop downloading.</li>
<li>If your server supports byte range requests, it seeks to the end of the video to try and determine the duration of the video.  If your server doesn&#8217;t support byterange requests, we don&#8217;t get the duration.  (Ogg was originally designed as a format for streaming, not static files and as such doesn&#8217;t include duration information in the header of the file, although there is work underway to add this functionality.)</li>
</ul>
<p>If you want to get a sense of how Firefox uses bandwidth, try <a href="http://people.mozilla.org/~blizzard/vprogress/test.html">this bandwidth test</a> I wrote a few months ago.  It uses a Firefox-only feature to read progress information on a video as it&#8217;s downloaded and give you a rough sense of the amount of bandwidth that particular video element is using.  It shows a little traffic when you load the video and then shows how it starts using bandwidth once you press the play button.</p>
<p>But this shows that we understand John&#8217;s point of view and we made the decision not to auto buffer by default for the very reasons he points out.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/12/autobuffering-video-in-firefox/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>interactive file uploads with Drag and Drop, FileAPI and XMLHttpRequest</title>
		<link>http://hacks.mozilla.org/2009/12/uploading-files-with-xmlhttprequest/</link>
		<comments>http://hacks.mozilla.org/2009/12/uploading-files-with-xmlhttprequest/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 18:01:35 +0000</pubDate>
		<dc:creator>Paul Rouget</dc:creator>
				<category><![CDATA[Demo]]></category>
		<category><![CDATA[Drag and Drop]]></category>
		<category><![CDATA[Featured Demo]]></category>
		<category><![CDATA[FileAPI]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Firefox 3.6]]></category>
		<category><![CDATA[XMLHttpRequest]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=2931</guid>
		<description><![CDATA[In previous posts, we showed how to access a file through the input tag or through the Drag and Drop mechanism. In both cases, you can use XMLHttpRequest to upload the files and follow the upload progress.
Demo




If you&#8217;re running the latest beta of Firefox 3.6, check out our file upload demo.
Uploading
XMLHttpRequest will send a given [...]]]></description>
			<content:encoded><![CDATA[<p>In previous posts, we showed how to access a file through the <a href="http://hacks.mozilla.org/2009/12/multiple-file-input-in-firefox-3-6/">input tag</a> or through the <a href="http://hacks.mozilla.org/2009/12/file-drag-and-drop-in-firefox-3-6/">Drag and Drop mechanism</a>. In both cases, you can use <a href="https://developer.mozilla.org/En/Using_XMLHttpRequest"><code>XMLHttpRequest</code></a> to upload the files and follow the upload progress.</p>
<h3>Demo</h3>
<p><video controls><br />
<source src="http://videos.mozilla.org/serv/blizzard/image-upload/2009-12-11-ImageUpload.ogv" type="video/ogg"/><br />
<source src="http://videos.mozilla.org/serv/blizzard/image-upload/2009-12-11-ImageUpload.mov" type="video/mp4"/><br />
<embed src="http://www.youtube.com/v/YG894djaF9o&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></video></p>
<p>If you&#8217;re running the latest beta of <a href="http://www.mozilla.com/en-US/firefox/all-beta.html">Firefox 3.6</a>, check out our <a href="http://demos.hacks.mozilla.org/openweb/uploadingFiles/">file upload demo</a>.</p>
<h3>Uploading</h3>
<p><a href="https://developer.mozilla.org/En/Using_XMLHttpRequest"><code>XMLHttpRequest</code></a> will send a given file to the server as a binary array, so you first need to read the content of the file as a binary string, using the <a href="http://hacks.mozilla.org/2009/12/w3c-fileapi-in-firefox-3-6/">File API.</a> Because both <a href="http://hacks.mozilla.org/2009/12/file-drag-and-drop-in-firefox-3-6/">Drag and Drop</a> and the <code>input</code> tag allow you to handle multiple files at once, you&#8217;ll need to create as many requests as there are files.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> reader <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FileReader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
reader.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> bin <span style="color: #339933;">=</span> e.<span style="color: #660066;">target</span>.<span style="color: #660066;">result</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">// bin is the binaryString</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
reader.<span style="color: #660066;">readAsBinaryString</span><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Once the file is read, send it to the server with XMLHttpRequest:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> xhr <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
xhr.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;upload.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
xhr.<span style="color: #660066;">overrideMimeType</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text/plain; charset=x-user-defined-binary'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
xhr.<span style="color: #660066;">sendAsBinary</span><span style="color: #009900;">&#40;</span>bin<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can choose to be notified when specific events,  such as error, success, or abort, occur during the request (see the <a href="https://developer.mozilla.org/En/Using_XMLHttpRequest#In_Firefox_3.5_and_later">MDC documentation</a> for more details).</p>
<h3>Following the Upload Progress</h3>
<p>The <code>progress</code> event provides the size of the uploaded portion of the binary content. This allows you to easily compute how much of the file has been uploaded.</p>
<p>Here&#8217;s an example showing the percentage of the file that has been uploaded so far:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">xhr.<span style="color: #660066;">upload</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;progress&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">lengthComputable</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> percentage <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">loaded</span> <span style="color: #339933;">*</span> 100<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> e.<span style="color: #660066;">total</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// do something</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/12/uploading-files-with-xmlhttprequest/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
<enclosure url="http://videos.mozilla.org/serv/blizzard/image-upload/2009-12-11-ImageUpload.ogv" length="2273855" type="video/ogg" />
<enclosure url="http://videos.mozilla.org/serv/blizzard/image-upload/2009-12-11-ImageUpload.mov" length="2968459" type="video/quicktime" />
		</item>
		<item>
		<title>file drag and drop in Firefox 3.6</title>
		<link>http://hacks.mozilla.org/2009/12/file-drag-and-drop-in-firefox-3-6/</link>
		<comments>http://hacks.mozilla.org/2009/12/file-drag-and-drop-in-firefox-3-6/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 19:37:21 +0000</pubDate>
		<dc:creator>Paul Rouget</dc:creator>
				<category><![CDATA[Demo]]></category>
		<category><![CDATA[Drag and Drop]]></category>
		<category><![CDATA[FileAPI]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Firefox 3.6]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=2913</guid>
		<description><![CDATA[In a previous post, we showed you how to upload several files using the input element. In Firefox 3.6, you can let your users drag and drop files directly into your web page, without going through the file picker.
Demo
If you&#8217;re running the latest Firefox 3.6 beta, check out our interactive demo of drag and drop. [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://hacks.mozilla.org/2009/12/multiple-file-input-in-firefox-3-6/">previous post</a>, we showed you how to upload several files using the <code>input</code> element. In Firefox 3.6, you can let your users drag and drop files directly into your web page, without going through the file picker.</p>
<h3>Demo</h3>
<p>If you&#8217;re running the latest <a href="http://www.mozilla.com/en-US/firefox/all-beta.html">Firefox 3.6 beta</a>, check out our <a href="http://demos.hacks.mozilla.org/openweb/DnD/">interactive demo of drag and drop</a>.  It showcases two technologies: the <a href="https://developer.mozilla.org/En/DragDrop/DataTransfer">Drag and Drop API</a> and the <a href="https://developer.mozilla.org/en/DOM/FileReader">File API</a>.</p>
<p><video width="500" controls><br />
<source src="http://videos.mozilla.org/serv/blizzard/drag-and-drop/2009-12-11-dnd.ogv" type="video/ogg"/><br />
<source src="http://videos.mozilla.org/serv/blizzard/drag-and-drop/2009-12-11-dnd.mov" type="video/mp4"/><br />
<embed src="http://www.youtube.com/v/4Us6S0fDV7U&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed><br />
</video></p>
<h3>Drag and Drop Events</h3>
<p>To use drag and drop, you first need to tell the browser that a given element can handle dropped objects and will respond to a drop action, using the <code>dragover</code> and <code>drop</code> events.</p>
<p>You also need to prevent the browser&#8217;s default behavior, which is to simply load the dropped object in the browser window.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dropzone.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;dragover&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  event.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
dropzone.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;drop&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  event.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">// Ready to do something with the dropped object</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You may also want to use the <code>dragenter</code> and <code>dragleave</code> events to be notified when a drag session starts or stops.</p>
<p>Your element is now ready to receive files with the <code>drop</code> event.</p>
<h3>Manipulating the Files</h3>
<p>On the <code>drop</code> event, you can access the files with the <code>files</code> property of the <a href="https://developer.mozilla.org/en/DragDrop/DataTransfer">DataTransfer</a> object:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dropzone.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;drop&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  event.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">// Ready to do something with the dropped object</span>
  <span style="color: #003366; font-weight: bold;">var</span> allTheFiles <span style="color: #339933;">=</span> event.<span style="color: #660066;">dataTransfer</span>.<span style="color: #660066;">files</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;You've just dropped &quot;</span> <span style="color: #339933;">+</span> allTheFiles.<span style="color: #660066;">length</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; files&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Once you&#8217;ve accessed the files, the <a href="http://hacks.mozilla.org/2009/12/w3c-fileapi-in-firefox-3-6/">File API</a> lets you do much more, like parsing files as a binary array, or displaying a preview of an image by reading the file as a <a href="https://developer.mozilla.org/en/data_URIs">DataURL</a>.</p>
<p>Of course, you can still drag and drop data other than files (e.g. text, URLs, remote images &#8230;) using the <a href="https://developer.mozilla.org/En/DragDrop/DataTransfer">drag and drop API</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/12/file-drag-and-drop-in-firefox-3-6/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
<enclosure url="http://videos.mozilla.org/serv/blizzard/drag-and-drop/2009-12-11-dnd.ogv" length="3178328" type="video/ogg" />
<enclosure url="http://videos.mozilla.org/serv/blizzard/drag-and-drop/2009-12-11-dnd.mov" length="3427652" type="video/quicktime" />
		</item>
		<item>
		<title>WebGL Draft Released Today</title>
		<link>http://hacks.mozilla.org/2009/12/webgl-draft-released-today/</link>
		<comments>http://hacks.mozilla.org/2009/12/webgl-draft-released-today/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 22:06:05 +0000</pubDate>
		<dc:creator>Arun Ranganathan</dc:creator>
				<category><![CDATA[Standards]]></category>
		<category><![CDATA[WebGL]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=2940</guid>
		<description><![CDATA[Even without a draft specification of WebGL in circulation, we&#8217;ve seen some promising 3D content using WebGL appear on the web, put together mainly through developer ingenuity and the fact that Firefox, Chromium, and WebKit are open source projects with early support for the technology.  Today, the WebGL Working Group at Khronos released a [...]]]></description>
			<content:encoded><![CDATA[<p>Even without a draft specification of WebGL in circulation, we&#8217;ve seen some <a href="http://hacks.mozilla.org/2009/12/webgl-goes-mobile/">promising</a> <a href="http://hacks.mozilla.org/2009/09/three-more-webgl-demos/">3D</a> <a href="http://hacks.mozilla.org/2009/10/webgl-in-the-wild/">content</a> using WebGL appear on the web, put together mainly through developer ingenuity and the fact that Firefox, Chromium, and WebKit are open source projects with early support for the technology.  Today, the <a href="http://khronos.org/webgl">WebGL Working Group at Khronos</a> released a <a href="https://cvs.khronos.org/svn/repos/registry/trunk/public/webgl/doc/spec/WebGL-spec.html">provisional public draft of the WebGL specification</a>, and we are very excited for what this means for the web.</p>
<p>For one thing, it means more developers can get involved in the evolution of <a href="https://cvs.khronos.org/svn/repos/registry/trunk/public/webgl/doc/spec/WebGL-spec.html">WebGL</a>.  There&#8217;s a <a href="https://www.khronos.org/webgl/public-mailing-list/">public mailing</a> list set up, so that you can engage directly with members of the <a href="http://khronos.org/webgl">WebGL Working Group</a>, as well as a <a href="http://www.khronos.org/message_boards/viewforum.php?f=35">web forum</a>.  It&#8217;s important to note that the specification is not yet finalized.  Participation from the web community is essential towards finalizing the specification, which we hope to do in the first quarter of 2010.</p>
<p>It also means that there are implementations of the draft specification that you can begin to test.  You can obtain a <a href="http://nightly.mozilla.org/">Firefox nightly</a> that implements the <a href="https://cvs.khronos.org/svn/repos/registry/trunk/public/webgl/doc/spec/WebGL-spec.html">WebGL draft specification</a>, and can turn on WebGL support in that build by following these steps:</p>
<ul>
<li>Type &#8220;<code>about:config</code>&#8221; in your browser&#8217;s URL bar</li>
<li>Do a search for &#8220;webgl&#8221; in the <em>Filter</em> field</li>
<li>Double-click the &#8220;<code>enabled_for_all_sites</code>&#8221; preference to change it to &#8220;<code>true</code>&#8221; </li>
</ul>
<p>Other browsers that support the draft <a href="https://cvs.khronos.org/svn/repos/registry/trunk/public/webgl/doc/spec/WebGL-spec.html">WebGL specification</a> are listed on the <a href="http://khronos.org/webgl/wiki/">WebGL Wiki</a>.</p>
<p>Now of course, this is hardware-accelerated 3D graphics in an early beta, and for now requires an OpenGL 2.0-capable GPU and drivers. In particular, most flavors of Intel&#8217;s integrated GPUs will not work straight out of the box (such as the GMA900/GMA950 often found in laptops).  Developers who build nightly builds of the browser can induce <em>software rendering</em> using <a href="http://www.mesa3d.org/">Mesa</a>, and should check out Vlad&#8217;s <a href="http://blog.vlad1.com/2009/09/21/webgl-samples/">blog post</a> for doing this on Windows.  <strong>Caveat emptor:</strong> building software rendering using Mesa into a Firefox nightly is likely to yield slower performance, and is only for the intrepid. </p>
<p><a href="https://cvs.khronos.org/svn/repos/registry/trunk/public/webgl/doc/spec/WebGL-spec.html">WebGL</a> is a royalty-free, cross-platform API that brings <a href="http://www.khronos.org/opengles/">OpenGL ES 2.0</a> to the web as a 3D drawing context within <a href="https://developer.mozilla.org/en/HTML/Canvas">HTML5&#8217;s Canvas element</a>, exposed as low-level interfaces within the Document Object Model.  </p>
<p>Developers familiar with the <a href="https://developer.mozilla.org/en/Drawing_Graphics_with_Canvas">Canvas 2D context</a> will recognize that WebGL is another context for Canvas:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// get the canvas element in the DOM</span>
<span style="color: #003366; font-weight: bold;">var</span> canvas <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;canvas3D&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> gl <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;experimental-webgl&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Note</strong> that till the <a href="https://cvs.khronos.org/svn/repos/registry/trunk/public/webgl/doc/spec/WebGL-spec.html">specification</a> is finalized, the context is called <code>experimental-webgl</code>.</p>
<p>WebGL uses the OpenGL shading language, GLSL ES, and can be cleanly combined with other web content that is layered on top or underneath the 3D content.  It is an <em>emerging web standard</em>, and is designed to be used with other parts of the web platform.  The release of<a href="https://cvs.khronos.org/svn/repos/registry/trunk/public/webgl/doc/spec/WebGL-spec.html"> the draft specification</a> is one step in bringing about a <em>plugin free</em> 3D API to the web, usable straight out of the box.  People have already begun using it to create compelling libraries.  Check out <a href="http://www.x3dom.org/">X3DOM</a>, which is a JavaScript library using WebGL that allows users to author content in <a href="http://www.web3d.org/x3d/">X3D</a>.  We expect developer ingenuity to surprise and awe us, as it always has.</p>
<p><strong>References</strong></p>
<ul>
<li>Review the <a href="https://cvs.khronos.org/svn/repos/registry/trunk/public/webgl/doc/spec/WebGL-spec.html">WebGL draft specification</a></li>
<li>Get involved!  <a href="http://khronos.org/webgl">WebGL Homepage on Khronos</a></li>
<li>Read articles and contribute on the <a href="http://khronos.org/webgl/wiki">WebGL Wiki</a></li>
<li>A blog about <a href="http://learningwebgl.com/blog/">learning WebGL</a></li>
<li>Find 3D recipes in the <a href="http://learningwebgl.com/cookbook/index.php/Main_Page">learning WebGL Cookbook</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/12/webgl-draft-released-today/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>multiple file input in Firefox 3.6</title>
		<link>http://hacks.mozilla.org/2009/12/multiple-file-input-in-firefox-3-6/</link>
		<comments>http://hacks.mozilla.org/2009/12/multiple-file-input-in-firefox-3-6/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 21:20:46 +0000</pubDate>
		<dc:creator>Paul Rouget</dc:creator>
				<category><![CDATA[DOM]]></category>
		<category><![CDATA[Device APIs]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[FileAPI]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Firefox 3.6]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=2905</guid>
		<description><![CDATA[Firefox 3.6 supports multiple file input. This new capability allows you to get several files as input at once, using standard technologies. This is a big improvement, since you used to be constrained to one file at a time, or needed to use a third party (proprietary) application. This will be particularly useful, for example, [...]]]></description>
			<content:encoded><![CDATA[<p>Firefox 3.6 supports <a href="https://developer.mozilla.org/en/DOM/Input.multiple">multiple file input</a>. This new capability allows you to get several files as input at once, using standard technologies. This is a big improvement, since you used to be constrained to one file at a time, or needed to use a third party (proprietary) application. This will be particularly useful, for example, for photo uploads.</p>
<h3>The input tag</h3>
<p>To let your user select a local file, use the input tag on your Web page. This will show the file picker to the user:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span><span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>

<p>In Firefox 3.6, the input tag has been expanded to support multiple files:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #000066;">multiple</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>

<p>The user will still see the same file picker, but will be able to select more than one file. </p>
<h3>The form tag</h3>
<p>You  can still use the classic <code>form</code> mechanism:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;upload.php&quot;</span> <span style="color: #000066;">enctype</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;multipart/form-data&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'uploads[]'</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #000066;">multiple</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Send&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

<p>If the server side code is in PHP, don&#8217;t forget to make sure that the value of the <code>name</code> attribute has brackets. The brackets are not from the HTML specification, but are required to manipulate the result of the request as an array (see <a href="http://www.php.net/manual/en/features.file-upload.multiple.php">PHP documentation</a>). </p>
<p>Here&#8217;s an example, which goes through the file list and prints each file name:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uploads'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$filename</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Using File API</h3>
<p>Firefox 3.6 also supports <a href="http://hacks.mozilla.org/2009/12/w3c-fileapi-in-firefox-3-6/">FileAPI</a>. This allows you to do extra processing on the client slide before sending the files to the server. You can access the selected files with the <code>files</code> property of the input DOM element and then manipulate the files using the FileAPI. </p>
<p>For example, here&#8217;s how to get the name of each file selected by the user. This is done on the client side, unlike the previous PHP example.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">var</span> input <span style="color: #339933;">=</span> document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input[type='file']&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">// You've selected input.files.length files</span>
  <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> input.<span style="color: #660066;">files</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// input.files[i] is a file object</span>
    <span style="color: #003366; font-weight: bold;">var</span> li <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;li&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    li.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> input.<span style="color: #660066;">files</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #000066;">name</span><span style="color: #339933;">;</span>
    ul.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>li<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Demo</h3>
<p>See this mechanism in action in our <a href="http://demos.hacks.mozilla.org/openweb/multipleFiles/">multiple file input demo</a>. You&#8217;ll need <a href="http://www.mozilla.com/en-US/firefox/all-beta.html">Firefox 3.6</a> (beta).</p>
<h3>Documentation</h3>
<p>To learn more about multiple file input, check out the <a href="https://developer.mozilla.org/en/DOM/Input.multiple">documentation on MDC</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/12/multiple-file-input-in-firefox-3-6/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Firefox 3.6 FileAPI demo: reading EXIF data from a local JPEG file</title>
		<link>http://hacks.mozilla.org/2009/12/firefox-36-fileapi-demo-reading-exif-data-from-a-local-jpeg-file/</link>
		<comments>http://hacks.mozilla.org/2009/12/firefox-36-fileapi-demo-reading-exif-data-from-a-local-jpeg-file/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 22:58:21 +0000</pubDate>
		<dc:creator>Christopher Blizzard</dc:creator>
				<category><![CDATA[Demo]]></category>
		<category><![CDATA[FileAPI]]></category>
		<category><![CDATA[Firefox 3.6]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=2880</guid>
		<description><![CDATA[Paul Rouget has put together a great demo of the new FileAPI we&#8217;re including in Firefox 3.6.  It lets you drag a JPG from the desktop into the browser that includes EXIF data and it can extract the GPS coordinates in the image and then load the location of where the photo was taken, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.mozbox.org/">Paul Rouget</a> has put together a great demo of the new <a href="http://hacks.mozilla.org/2009/12/w3c-fileapi-in-firefox-3-6/">FileAPI</a> we&#8217;re including in <a href="http://www.mozilla.com/en-US/firefox/all-beta.html">Firefox 3.6</a>.  It lets you drag a JPG from the desktop into the browser that includes EXIF data and it can extract the GPS coordinates in the image and then load the location of where the photo was taken, entirely from JavaScript.</p>
<p>If you have the <a href="http://www.mozilla.com/en-US/firefox/all-beta.html">Firefox 3.6 beta</a>, <a href="http://demos.hacks.mozilla.org/openweb/FileAPI/">you can view the demo</a> or you can just watch the video below.</p>
<p><video width="500" controls><br />
<source src="http://videos.mozilla.org/serv/blizzard/FileAPI/2009-12-08-FileAPI.ogv" type="video/ogg"/><br />
<source src="http://videos.mozilla.org/serv/blizzard/FileAPI/2009-12-08-FileAPI.mp4" type="video/mp4"/><br />
<embed src="http://www.youtube.com/v/YhaswKECOfA&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed><br />
</video></p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/12/firefox-36-fileapi-demo-reading-exif-data-from-a-local-jpeg-file/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
<enclosure url="http://videos.mozilla.org/serv/blizzard/FileAPI/2009-12-08-FileAPI.ogv" length="3311252" type="video/ogg" />
		</item>
		<item>
		<title>W3C FileAPI in Firefox 3.6</title>
		<link>http://hacks.mozilla.org/2009/12/w3c-fileapi-in-firefox-3-6/</link>
		<comments>http://hacks.mozilla.org/2009/12/w3c-fileapi-in-firefox-3-6/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 22:52:55 +0000</pubDate>
		<dc:creator>Arun Ranganathan</dc:creator>
				<category><![CDATA[Drag and Drop]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[FileAPI]]></category>
		<category><![CDATA[Firefox 3.6]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=2781</guid>
		<description><![CDATA[Often, web applications will prompt the user to select a file, typically to upload to a server.  Unless the web application makes use of a plugin, file selection occurs through an HTML input element, of the sort &#60;input type="file"/&#62;.  Firefox 3.6 now supports much of the W3C File API, which specifies the ability [...]]]></description>
			<content:encoded><![CDATA[<p>Often, web applications will prompt the user to select a file, typically to upload to a server.  Unless the web application makes use of a plugin, file selection occurs through an HTML input element, of the sort <code>&lt;input type="file"/&gt;</code>.  Firefox 3.6 now supports much of the <a href="http://www.w3.org/TR/FileAPI/">W3C File API</a>, which specifies the ability to <em>asynchronously read</em> the selected file into memory, and perform operations on the file data within the web application (for example, to display a thumbnail preview of an image, before it is uploaded, or to look for ID3 tags within an MP3 file, or to look for EXIF data in JPEG files, all on the client side).  This is a new API, and replaces the file API that was introduced in Firefox 3.</p>
<p>It is important to note that even before the advent of the <a href="http://www.w3.org/TR/FileAPI/">W3C File API</a> draft (which only became a Working Draft in November 2009), Firefox 3 and later provide the ability to <a href="https://developer.mozilla.org/en/nsIDOMFile">read files into memory <em>synchronously</em></a> but that capability should be considered <em>deprecated</em> in favor of the new implementation in Firefox 3.6 of <a href="https://developer.mozilla.org/en/Using_files_from_web_applications">the asynchronous File API</a>.  The deprecated API allowed you synchronously access a file:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// After obtaining a handle to a file</span>
<span style="color: #006600; font-style: italic;">// access the file data</span>
<span style="color: #003366; font-weight: bold;">var</span> dataURL <span style="color: #339933;">=</span> file.<span style="color: #660066;">getAsDataURL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
img.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> dataURL<span style="color: #339933;">;</span></pre></div></div>

<p>While Firefox 3.6 will continue to support code usage of the sort above, it should be considered <em>deprecated</em> since it reads files <em>synchronously</em> on the main thread.  For large files, this could result in blocking on the result of the read, which isn&#8217;t desirable.  Moreover, the file object itself provides a method to read from it, rather than having a separate reader object.  These considerations informed the technical direction of the <a href="https://developer.mozilla.org/en/Using_files_from_web_applications">new File API in Firefox 3.6</a> (and the direction of the <a href="http://www.w3.org/TR/FileAPI/">specification</a>).  The rest of this article is about the newly introduced File API.</p>
<p><strong>Accessing file selections</strong></p>
<p>Firefox 3.6 supports <em>multiple file selections</em> on an input element, and returns all the files selected using the <a href="https://developer.mozilla.org/en/DOM/FileList">FileList</a> interface.  Previous versions of Firefox only supported one selection of a file using the input element.  Additionally, the <a href="https://developer.mozilla.org/en/DOM/FileList">FileList</a> interface is also exposed to the <a href="https://developer.mozilla.org/En/DragDrop/Drag_and_Drop">HTML5 Drag and Drop API</a> as a property of the <a href="https://developer.mozilla.org/En/DragDrop/DataTransfer">DataTransfer</a> interface.  Users can drag and drop multiple files to a drop target within a web page as well.</p>
<p>The following HTML spawns the standard file picker, with which you can select multiple files:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;inputFiles&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #000066;">multiple</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>

<p>Note that if you don&#8217;t use the <code>multiple</code> attribute, you only enable single file selection.</p>
<p>You can work with all the selected files obtained either through the file picker (using the <code>input</code> element) or through the <a href="https://developer.mozilla.org/En/DragDrop/DataTransfer">DataTransfer</a> object by iterating through the FileList:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> files <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;inputFiles&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">files</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// or, for a drag event e:</span>
<span style="color: #006600; font-style: italic;">// var dt = e.dataTransfer; var files = dt.files</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> files.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> file <span style="color: #339933;">=</span> files<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  handleFile<span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Properties of files</strong></p>
<p>Once you obtain a reference to an individually selected file from a <a href="https://developer.mozilla.org/en/DOM/FileList">FileList</a>, you get a <a href="https://developer.mozilla.org/en/DOM/File">File</a> object, which has <code>name</code>, <code>type</code>, and <code>size</code> properties.  Continuing with the code snippet above:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> handleFile<span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// RegExp for JPEG mime type</span>
    <span style="color: #003366; font-weight: bold;">var</span> imageType <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/image\/jpeg/</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Check if match</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>file.<span style="color: #660066;">type</span>.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span>imageType<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
   <span style="color: #006600; font-style: italic;">// Check if the picture exceeds set limit</span>
   <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>file.<span style="color: #660066;">size</span> <span style="color: #339933;">&gt;</span> maxSize<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Choose a smaller photo!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
  <span style="color: #006600; font-style: italic;">// Add file name to page</span>
  <span style="color: #003366; font-weight: bold;">var</span> picData <span style="color: #339933;">=</span> document.<span style="color: #660066;">createTextNode</span><span style="color: #009900;">&#40;</span>file.<span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  dataGrid.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>picData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The <code>size</code> attribute is the file&#8217;s size, in bytes.  The <code>name</code> attribute is the file&#8217;s name, without path information.  The <code>type</code> attribute is an ASCII-encoded string in lower case representing the media type of the file, expressed as an <a href="http://www.ietf.org/rfc/rfc2046.txt">RFC2046 MIME type</a>.  The <code>type</code> attribute in particular is useful in sniffing file type, as in the example above, where the script determines if the file in question is a JPEG file.  If Firefox 3.6 cannot determine the file&#8217;s <code>type</code>, it will return the empty string.</p>
<p><strong>Reading Files</strong></p>
<p>Firefox 3.6 and beyond support the <a href="https://developer.mozilla.org/en/DOM/FileReader">FileReader</a> object to read file data asynchronously into memory, using event callbacks to mark progress.  The object is instantiated in the standard way:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> binaryReader <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FileReader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Event handler attributes are used to work with the <code>result</code> of the file read operation.  For very large files, it is possible to watch for <em>progress events</em> as the file is being read into memory (using the <code>onprogress</code> event handler attribute to set the event handler function).  This is useful in scenarios where the drives in question may not be local to the hardware, or if the file in question is particularly big.</p>
<p>The <a href="https://developer.mozilla.org/en/DOM/FileReader">FileReader</a> object supports three methods to read files into memory.  Each allows programmatic access to the files data in a different format, though in practice only one read method should be called on a given <a href="https://developer.mozilla.org/en/DOM/FileReader">FileReader</a> object:</p>
<ul>
<li><code>filereader.readAsBinaryString(file);</code> will asynchronously return a binary string with each byte represented by an integer in the range [0..255].  This is useful for binary manipulations of a file&#8217;s data, for example to look for <a href="http://en.wikipedia.org/wiki/ID3">ID3 tags in an MP3 file</a>, or to look for <a href="http://en.wikipedia.org/wiki/Exchangeable_image_file_format">EXIF data</a> in a JPEG image.</li>
<li><code>filereader.readAsText(file, encoding);</code> will asynchronously return a string in the format solicited by the encoding parameter (for example <code>encoding = "UTF-8"</code>).  This is useful for working with a text file, for example to parse an XML file.</li>
<li><code>filereader.readAsDataURL(file);</code> will asynchronously return a <a href="https://developer.mozilla.org/en/data_URIs">Data URL</a>.  Firefox 3.6 allows large URLs, and so this feature is particularly useful when a URL could help display media content in a web page, for example for image data, video data, or audio data.</li>
</ul>
<p>An example helps tie this all together:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>files.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>handleFile<span style="color: #009900;">&#40;</span>files<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        invalid.<span style="color: #660066;">style</span>.<span style="color: #660066;">visibility</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;visible&quot;</span><span style="color: #339933;">;</span>
        invalid.<span style="color: #660066;">msg</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Select a JPEG Image&quot;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> binaryReader <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FileReader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
binaryReader.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #003366; font-weight: bold;">var</span> exif <span style="color: #339933;">=</span> findEXIFInJPG<span style="color: #009900;">&#40;</span>binaryReader.<span style="color: #660066;">result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>exif<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// ...set up conditions for lack of data</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// ...write out exif data</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
binaryReader.<span style="color: #660066;">onprogress</span> <span style="color: #339933;">=</span> updateProgress<span style="color: #339933;">;</span>
binaryReader.<span style="color: #000066;">onerror</span> <span style="color: #339933;">=</span> errorHandler<span style="color: #339933;">;</span>
&nbsp;
binaryReader.<span style="color: #660066;">readAsBinaryString</span><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> updateProgress<span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #006600; font-style: italic;">// use lengthComputable, loaded, and total on ProgressEvent</span>
   <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>evt.<span style="color: #660066;">lengthComputable</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #003366; font-weight: bold;">var</span> loaded <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>evt.<span style="color: #660066;">loaded</span> <span style="color: #339933;">/</span> evt.<span style="color: #660066;">total</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>loaded <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// update progress meter</span>
            progMeter.<span style="color: #660066;">style</span>.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>loaded <span style="color: #339933;">*</span> <span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> errorHandler<span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>evt.<span style="color: #660066;">target</span>.<span style="color: #660066;">error</span>.<span style="color: #660066;">code</span> <span style="color: #339933;">==</span> evt.<span style="color: #660066;">target</span>.<span style="color: #660066;">error</span>.<span style="color: #660066;">NOT_FOUND_ERR</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;File Not Found!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In order to work with binary data, the use of the <a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/String/CharCodeAt">charCodeAt</a> function exposed on strings will be particularly useful.  For instance, an utility of the sort:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getByteAt<span style="color: #009900;">&#40;</span>file<span style="color: #339933;">,</span> idx<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> file.<span style="color: #660066;">charCodeAt</span><span style="color: #009900;">&#40;</span>idx<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>allows extraction of the Unicode value of the character at the given index.</p>
<p>An example of similar code in action in Firefox 3.6, including use of the <code>readAsDataURL</code> method to render an image, as well as binary analysis of a JPEG for EXIF detection (using the <code>readAsBinaryString</code> method), can be found in <a href="http://hacks.mozilla.org/2009/12/firefox-36-fileapi-demo-reading-exif-data-from-a-local-jpeg-file/">Paul Rouget&#8217;s great demo of the File API.</a>.</p>
<p><strong>A word on the specification</strong></p>
<p>The existence of a W3C public working draft of <a href="http://www.w3.org/TR/FileAPI/">the File API</a> holds the promise of other browsers implementing it shortly.  Firefox 3.6&#8217;s implementation is fairly complete, but is missing some of the technology mentioned in the specification.  Notably, the <code>urn</code> feature on the <a href="https://developer.mozilla.org/en/DOM/File">File</a> object isn&#8217;t yet implemented, and neither is the ability to extract byte-ranges of files using the <code>slice</code> method.  A synchronous way to read files isn&#8217;t yet implemented as part of <a href="https://developer.mozilla.org/En/DOM/Worker">Worker Threads</a>.  These features will come in future versions of Firefox.</p>
<p><strong>References</strong></p>
<ul>
<li><a href="http://www.w3.org/TR/FileAPI/">The W3C File API Specification</a></li>
<li><a href="https://developer.mozilla.org/en/Using_files_from_web_applications">MDC: Using files within web applications</a></li>
<li><a href="https://developer.mozilla.org/en/DOM/FileList">MDC: FileList</a></li>
<li><a href="https://developer.mozilla.org/en/DOM/File">MDC: File</a></li>
<li><a href="https://developer.mozilla.org/en/DOM/FileReader">MDC: FileReader</a></li>
<li><a href="https://developer.mozilla.org/En/DragDrop/Drag_and_Drop">MDC: Drag and Drop</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/12/w3c-fileapi-in-firefox-3-6/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>css backgrounds in Firefox 3.6</title>
		<link>http://hacks.mozilla.org/2009/12/css-backgrounds-firefox-36/</link>
		<comments>http://hacks.mozilla.org/2009/12/css-backgrounds-firefox-36/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 18:49:01 +0000</pubDate>
		<dc:creator>Alix Franquet</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Firefox 3.6]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=2429</guid>
		<description><![CDATA[Firefox 3.6 allows you to do more with CSS backgrounds: you can use gradients, set a background size, and specify multiple backgrounds.
Custom Background Size
In Firefox 3.6, you can specify the size of a background image to scale it as a percentage of the element&#8217;s size, or to a specific length, using -moz-background-size.

-moz-background-size:  &#60;bg-size&#62;&#91;, &#60;bg-size&#62;&#93;*

Percentages. [...]]]></description>
			<content:encoded><![CDATA[<p>Firefox 3.6 allows you to do more with CSS backgrounds: you can use <a href="http://hacks.mozilla.org/2009/11/css-gradients-firefox-36/">gradients</a>, set a background size, and specify multiple backgrounds.</p>
<p><strong>Custom Background Size</strong></p>
<p>In Firefox 3.6, you can specify the size of a background image to scale it as a <a href="https://developer.mozilla.org/en/CSS/percentage">percentage</a> of the element&#8217;s size, or to a specific <a href="https://developer.mozilla.org/en/CSS/length">length</a>, using <a href="https://developer.mozilla.org/en/CSS/-moz-background-size">-moz-background-size</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">-moz-background-<span style="color: #000000; font-weight: bold;">size</span><span style="color: #00AA00;">:</span>  &lt;bg-size<span style="color: #00AA00;">&gt;</span><span style="color: #00AA00;">&#91;</span><span style="color: #00AA00;">,</span> &lt;bg-size<span style="color: #00AA00;">&gt;</span><span style="color: #00AA00;">&#93;</span><span style="color: #00AA00;">*</span></pre></div></div>

<p><strong>Percentages.</strong> In this example, you can see the effect of setting a size using percentages. On the left, size is set to auto, which maintains the original size of the background image. In the center, size is set to 100%, which scales the background image to 100% of the area (horizontally), even if the original image was smaller than the background positioning area. On the right, size is set to 10%, which scales the image to 10% of the area. The background image is repeated by default.</p>
<p><img class="aligncenter size-full wp-image-2682" title="css_bgsize_autoto10_fxlogo" src="http://hacks.mozilla.org/wp-content/uploads/2009/11/css_bgsize_autoto10_fxlogo.png" alt="css_bgsize_autoto10_fxlogo" width="350" height="121" /></p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"> <span style="color: #6666ff;">.bg_example_left</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>http<span style="color: #00AA00;">:</span>//demos<span style="color: #6666ff;">.hacks</span><span style="color: #6666ff;">.mozilla</span>.org/openweb/resources/images/logos/firefox-48.png<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  -moz-background-<span style="color: #000000; font-weight: bold;">size</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
 <span style="color: #6666ff;">.bg_example_center</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>http<span style="color: #00AA00;">:</span>//demos<span style="color: #6666ff;">.hacks</span><span style="color: #6666ff;">.mozilla</span>.org/openweb/resources/images/logos/firefox-48.png<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  -moz-background-<span style="color: #000000; font-weight: bold;">size</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
 <span style="color: #6666ff;">.bg_example_right</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>http<span style="color: #00AA00;">:</span>//demos<span style="color: #6666ff;">.hacks</span><span style="color: #6666ff;">.mozilla</span>.org/openweb/resources/images/logos/firefox-48.png<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  -moz-background-<span style="color: #000000; font-weight: bold;">size</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">10</span>%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Here&#8217;s the same example (auto &#8211; 100% &#8211; 10%) using a different background image. In this case, the original is larger than the background area. As a result, specifying &#8220;auto&#8221; shows only a portion of the original, and you need to set a size of 100% to make the full image visible.</p>
<p><img class="aligncenter size-full wp-image-2697" title="css_bgsize_autoto10_flowers" src="http://hacks.mozilla.org/wp-content/uploads/2009/11/css_bgsize_autoto10_flowers.png" alt="css_bgsize_autoto10_flowers" width="350" height="113" /></p>
<p><strong>Horizontal and Vertical Scaling.</strong> It it possible to define a size for both horizontal and vertical scaling. Specifying only one size sets horizontal scaling (as in the examples above) and vertical defaults to &#8220;auto&#8221;. If a second size is specified, it is used for vertical scaling, as in the example below. On the left you can see an image with horizontal scaling of 100% and vertical defaulting to &#8220;auto&#8221;. On the right, horizontal is set to 100% and vertical is 30%, changing the original appearance of the image because of the change in proportions.</p>
<p><img class="aligncenter size-full wp-image-2705" title="css_bg_size_vert" src="http://hacks.mozilla.org/wp-content/uploads/2009/11/css_bg_size_vert.png" alt="css_bg_size_vert" width="229" height="113" /></p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">  -moz-background-<span style="color: #000000; font-weight: bold;">size</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span><span style="color: #00AA00;">;</span>
  -moz-background-<span style="color: #000000; font-weight: bold;">size</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span> <span style="color: #933;"><span style="color: #cc66cc;">30</span>%</span><span style="color: #00AA00;">;</span></pre></div></div>

<p><strong>Custom Size Demo.</strong> Try our <a href="http://demos.hacks.mozilla.org/openweb/cssBackground/size/">interactive demo: select the size of a background on the fly.</a> You&#8217;ll need the <a href="http://www.mozilla.com/firefox/all-rc.html">latest beta of Firefox 3.6</a>.</p>
<p><strong>Multiple Backgrounds</strong></p>
<p>Firefox 3.6 also enables you to stack multiple backgrounds. This allows you to create cool effects by stacking a <a href="http://hacks.mozilla.org/2009/11/css-gradients-firefox-36/">gradient</a> on top of an image for example.</p>
<p><strong>Defining.</strong> To define multiple backgrounds, simply list them as follows, using the <a href="https://developer.mozilla.org/en/CSS/background">background</a> CSS property:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.foo</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> background1<span style="color: #00AA00;">,</span> background2<span style="color: #00AA00;">,</span> ...<span style="color: #00AA00;">,</span> backgroundN<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>The order in which you list the backgrounds matters: the first in the list will appear as the top layer, the last one as the bottom layer.</p>
<p><strong>Setting Properties.</strong> For multiple backgrounds, you can set the same properties you would for a single background, such as background-position or background-repeat. Define each background&#8217;s behavior by specifying a value for each property. The values need to be listed in the order in which you initially listed the backgrounds.</p>
<p>So if you defined:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> background1<span style="color: #00AA00;">,</span> background2<span style="color: #00AA00;">,</span> background3<span style="color: #00AA00;">;</span></pre></div></div>

<p>List the values in the same order for each property:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> background1_position<span style="color: #00AA00;">,</span> background2_position<span style="color: #00AA00;">,</span> background3_position<span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">background-repeat</span><span style="color: #00AA00;">:</span> background1_repeat<span style="color: #00AA00;">,</span> background2_repeat<span style="color: #00AA00;">,</span> background3_repeat<span style="color: #00AA00;">;</span></pre></div></div>

<p><strong>Example.</strong> Here&#8217;s how to stack three different backgrounds: the Firefox logo, a <a href="http://hacks.mozilla.org/2009/11/css-gradients-firefox-36/">linear gradient</a>, and an image with flowers. If you&#8217;re running <a href="http://www.mozilla.com/firefox/all-rc.html">Firefox 3.6 beta</a>, you can turn on and off any or all of the three backgrounds in our <a href="http://demos.hacks.mozilla.org/openweb/cssBackground/multiple">interactive demo</a>.</p>
<p><img class="aligncenter size-full wp-image-2726" title="css_multibg" src="http://hacks.mozilla.org/wp-content/uploads/2009/11/css_multibg.png" alt="css_multibg" width="216" height="117" /></p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"> <span style="color: #6666ff;">.multi_bg_example</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>http<span style="color: #00AA00;">:</span>//demos<span style="color: #6666ff;">.hacks</span><span style="color: #6666ff;">.mozilla</span>.org/openweb/resources/images/logos/firefox-48.png<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span> -moz-linear-gradient<span style="color: #00AA00;">&#40;</span><span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">,</span> rgba<span style="color: #00AA00;">&#40;</span>255<span style="color: #00AA00;">,</span> 255<span style="color: #00AA00;">,</span> 255<span style="color: #00AA00;">,</span> 0<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span>  rgba<span style="color: #00AA00;">&#40;</span>255<span style="color: #00AA00;">,</span> 255<span style="color: #00AA00;">,</span> 255<span style="color: #00AA00;">,</span> 1<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">http://demos.hacks.mozilla.org/openweb/resources/images/patterns/flowers-pattern.jpg</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">background-repeat</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">,</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">,</span> <span style="color: #993333;">repeat</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">bottom</span> <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">,</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">,</span> <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2009/12/css-backgrounds-firefox-36/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
