<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6442308477719289110</id><updated>2024-08-28T13:14:32.671-07:00</updated><category term="javascript"/><category term="best practice"/><category term="dom"/><category term="dom theory"/><category term="jquery"/><category term="array"/><category term="api"/><category term="asshole"/><category term="bigdecimal"/><category term="constructor"/><category term="css"/><category term="css1"/><category term="css2.1"/><category term="css3"/><category term="double"/><category term="float"/><category term="global"/><category term="html"/><category term="html5"/><category term="htmlcollection"/><category term="isarray"/><category term="java"/><category term="json"/><category term="keyword"/><category term="management"/><category term="no"/><category term="nodelist"/><category term="ofuscation"/><category term="rule"/><category term="security"/><category term="selectors"/><category term="theory"/><category term="this"/><category term="trim"/><category term="type"/><category term="variables"/><title type='text'>foo()</title><subtitle type='html'>Essays on JavaScript, Software Development and stuff</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://alebelcor.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://alebelcor.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Alejandro Beltran</name><uri>http://www.blogger.com/profile/13533114582122558035</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://2.bp.blogspot.com/_RTIp6dEXzUs/TU3aPr0kdtI/AAAAAAAAAAU/AFDiZQxwOwg/s220/1.PNG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>15</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6442308477719289110.post-1614867327213140338</id><published>2011-10-08T17:21:00.000-07:00</published><updated>2011-10-09T23:00:06.004-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="api"/><category scheme="http://www.blogger.com/atom/ns#" term="html"/><category scheme="http://www.blogger.com/atom/ns#" term="html5"/><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><title type='text'>HTML5 APIs</title><content type='html'>HTML5 introduces lots of new and exciting things for web authors,&amp;nbsp;among&amp;nbsp;these are new APIs.&amp;nbsp;Here are some of them.&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;&lt;a href=&quot;http://dev.w3.org/html5/spec-author-view/spec.html#application-cache-api&quot;&gt;Application Cache API&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
An API to interact with the application cache, which is a set of cached resources.&lt;br /&gt;
&lt;br /&gt;
You can do &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;window.applicationCache&lt;/span&gt; to get the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;ApplicationCache&lt;/span&gt; object that applies to the active document of that window.&amp;nbsp;More on application caches &lt;a href=&quot;http://dev.w3.org/html5/spec/offline.html#appcache&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;a href=&quot;http://dev.w3.org/html5/spec-author-view/spec.html#datatransfer&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;DataTransfer API&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
The &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;DataTransfer&lt;/span&gt; objects are used to expose the drag data store that underlies a drag-and-drop operation.&amp;nbsp;More on drag and drop &lt;a href=&quot;http://dev.w3.org/html5/spec-author-view/spec.html#dnd&quot;&gt;here&lt;/a&gt;.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;&lt;a href=&quot;http://dev.w3.org/html5/spec-author-view/spec.html#command-api&quot;&gt;Command API&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
A command is the abstraction behind menu items, buttons, and links. They are defined to have facets (label, hint, icon, etc.) and are exposed on elements using this API.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;&lt;a href=&quot;http://dev.w3.org/html5/spec-author-view/spec.html#the-constraint-validation-api&quot;&gt;Constraint Validation API&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
As of this writing, there is no proper definition to this. But we can assume it&#39;ll extend our client-side validation tool arsenal.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;&lt;a href=&quot;http://dev.w3.org/html5/spec-author-view/spec.html#history-0&quot;&gt;History API&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
The H&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;istory&lt;/span&gt; objects provide a representation of the pages in the session history of browsing contexts.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
For example, you can go back (&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;window.history.back()&lt;/span&gt;) and forward (&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;window.history.forward()&lt;/span&gt;).&amp;nbsp;More on session history &lt;a href=&quot;http://dev.w3.org/html5/spec-author-view/spec.html#history&quot;&gt;here&lt;/a&gt;.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;&lt;a href=&quot;http://dev.w3.org/html5/spec-author-view/spec.html#media-controllers&quot;&gt;MediaController API&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
An API to interact with the&amp;nbsp;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;audio&amp;gt;&lt;/span&gt;&amp;nbsp;and&amp;nbsp;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;video&amp;gt;&lt;/span&gt;&amp;nbsp;elements.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
You can do things like play (&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;controller.play()&lt;/span&gt;) and pause (&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;controller.pause()&lt;/span&gt;).&amp;nbsp;More on media elements &lt;a href=&quot;http://dev.w3.org/html5/spec-author-view/spec.html#media-element&quot;&gt;here&lt;/a&gt;.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;&lt;a href=&quot;http://dev.w3.org/html5/spec-author-view/spec.html#text-track-api&quot;&gt;Text Track API&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
An API to interact with the&amp;nbsp;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;audio&amp;gt;&lt;/span&gt;&amp;nbsp;and&amp;nbsp;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;lt;video&amp;gt;&lt;/span&gt;&amp;nbsp;associated text tracks, like subtitles or captions.&lt;br /&gt;
&lt;br /&gt;
You can get a&amp;nbsp;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;media&lt;/span&gt;&amp;nbsp;element and get a track from it like&amp;nbsp;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;media.textTracks[0]&lt;/span&gt;&amp;nbsp;(for the first one).&lt;br /&gt;
A&amp;nbsp;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;textTrack&lt;/span&gt;&amp;nbsp;element has it&#39;s own API which can return things like the language, readyState, mode, etc.&amp;nbsp;More on media elements&amp;nbsp;&lt;a href=&quot;http://dev.w3.org/html5/spec-author-view/spec.html#media-element&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;&lt;a href=&quot;http://dev.w3.org/html5/spec-author-view/spec.html#textFieldSelection&quot;&gt;APIs for the text field selections&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
The &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;input&lt;/span&gt; and &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;textarea&lt;/span&gt;&amp;nbsp;text elements define an API for handling their selection.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
You can select everything in a text field with the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;select()&lt;/span&gt; function, or use &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;selectionStart&lt;/span&gt; and &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;selectionEnd&lt;/span&gt; to get the currently selected text.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
By the way, this is not it. There are, of course, other interfaces and current elements that are being extended with new properties and functions.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: x-large;&quot;&gt;Related specifications&lt;/span&gt;&lt;br /&gt;
Now I present to you:&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;Specs that were originally in the HTML5 spec, but were later moved out into their own.&lt;/li&gt;
&lt;li&gt;Specs worked on by the Web Apps Working Group (in charge of HTML5).&lt;/li&gt;
&lt;li&gt;Other related/interesting specs.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;&lt;a href=&quot;http://dev.w3.org/html5/2dcontext&quot;&gt;Canvas 2D Context&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
Defines the 2D Context for the HTML &amp;lt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;canvas&amp;gt;&lt;/span&gt;&amp;nbsp;element.&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;&lt;a href=&quot;http://dev.w3.org/2006/webapi/clipops/clipops.html&quot;&gt;Clipboard API and events&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
Describes APIs for clipboard operations such as copy, cut and paste in web applications.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;&lt;a href=&quot;http://dvcs.w3.org/hg/editing/raw-file/tip/editing.html&quot;&gt;Editing APIs&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
Defines commands to edit HTML documents programmatically.&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;a href=&quot;http://dev.w3.org/2006/webapi/FileAPI&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;File API&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
This&amp;nbsp;specification provides an API for representing file objects in web applications, as well as programmatically selecting them and accessing their data.&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;&lt;a href=&quot;http://dev.w3.org/2009/dap/file-system/file-dir-sys.html&quot;&gt;File API: Directories and System&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
This specification defines an API to navigate file system hierarchies.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://dev.w3.org/2009/dap/file-system/file-writer.html&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;File API: Writer&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
This specification defines an API for writing to files from web applications.&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;a href=&quot;http://dev.w3.org/html5/postmsg&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;HTML5 Web Messaging&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
Defines two mechanisms for communicating between browsing contexts in HTML documents.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;a href=&quot;http://dvcs.w3.org/hg/IndexedDB/raw-file/tip/Overview.html&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;Indexed Database API&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
Formerly WebSimpleDB API, this document defines APIs for a database of records holding simple values and hierarchical objects.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://dev.w3.org/html5/eventsource&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;Server-Sent Events&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
This specification defines an API for opening an HTTP connection for receiving push notifications from a server in the form of DOM events.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;a href=&quot;http://dev.w3.org/html5/websockets&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;The Web Sockets API&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
This specification defines an API that enables Web pages to use the WebSocket protocol for two-way communication with a remote host.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://dev.w3.org/html5/webstorage&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;Web Storage&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
This specification defines an API for persistent data storage of key-value pair data in Web clients.&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;&lt;a href=&quot;http://dev.w3.org/html5/workers&quot;&gt;Web Workers&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
This specification defines an API that allows Web application authors to spawn background workers running scripts in parallel to their main page. This allows for thread-like operation with message-passing as the coordination mechanism.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;a href=&quot;http://dev.w3.org/2006/webapi/XMLHttpRequest-2&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;XMLHttpRequest Level 2&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
Enhances the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;XMLHttpRequest&lt;/span&gt; object with new features, such as cross-origin requests, progress events, and the handling of byte streams for both sending and receiving.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;
And, again, these are not it. There are other working groups in the W3C besides the Web Apps one, each one with specifications they&#39;re working on. It&#39;s crazy, I know, but hopefully this will give&amp;nbsp;you an idea of what&#39;s coming.&amp;nbsp;I strongly recommend you to research more about it and experiment.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Thanks for reading and let me know your comments.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;div&gt;
Sources:&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/lxtGi&quot;&gt;HTML5: Edition for Web Authors (editor&#39;s draft)&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/VGjgi&quot;&gt;WebApps Working Group Publication Status of specifications&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://alebelcor.blogspot.com/feeds/1614867327213140338/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://alebelcor.blogspot.com/2011/10/html5-apis.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/1614867327213140338'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/1614867327213140338'/><link rel='alternate' type='text/html' href='http://alebelcor.blogspot.com/2011/10/html5-apis.html' title='HTML5 APIs'/><author><name>Alejandro Beltran</name><uri>http://www.blogger.com/profile/13533114582122558035</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://2.bp.blogspot.com/_RTIp6dEXzUs/TU3aPr0kdtI/AAAAAAAAAAU/AFDiZQxwOwg/s220/1.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6442308477719289110.post-638201443687022207</id><published>2011-09-11T01:31:00.001-07:00</published><updated>2011-09-11T01:31:47.114-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="css"/><category scheme="http://www.blogger.com/atom/ns#" term="css1"/><category scheme="http://www.blogger.com/atom/ns#" term="css2.1"/><category scheme="http://www.blogger.com/atom/ns#" term="css3"/><category scheme="http://www.blogger.com/atom/ns#" term="selectors"/><title type='text'>CSS Selectors</title><content type='html'>Have you ever wondered:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;How many selectors does CSS have?&lt;/li&gt;
&lt;li&gt;Which selectors come from which version of CSS?&lt;/li&gt;
&lt;li&gt;How does each selector work?&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
Me too! so here&#39;s what I found out with an example for each one.&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;CSS 1&lt;/span&gt;&lt;br /&gt;
E → an element of type E&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;div

/* selects any div element in the document */&lt;/pre&gt;E F → an F element descendant of an E element&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;div p

/* selects any p element that is a descendant of a div element */&lt;/pre&gt;E.warning → an E element whose class is &quot;warning&quot;&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;.banner

/* selects any element whose class is &quot;banner&quot; */&lt;/pre&gt;E#myid → an E element with ID equal to &quot;myid&quot;.&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;#footer

/* selects an element with ID equal to &quot;footer&quot; */&lt;/pre&gt;E:link → an E element being the source anchor of a hyperlink of which the target is not yet visited&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;a:link

/* selects any unvisited a element */&lt;/pre&gt;E:visited → an E element being the source anchor of a hyperlink of which the target is already visited&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;a:visited

/* selects any visited a element */&lt;/pre&gt;E:active → an E element currently being selected&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;a:active

/* selects any a element currently being selected */&lt;/pre&gt;E:first-line → the first formatted line of an E element&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;p:first-line

/* selects the first formatted line of any p element */&lt;/pre&gt;E:first-letter → the first formatted letter of an E element&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;p:first-letter

/* selects the first formatted letter for any p element */&lt;/pre&gt;E, F → an element of type E and an element of type F&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;label, span

/* not really a selector, it&#39;s a way of grouping them */&lt;/pre&gt;According to the CSS 1 spec: &quot;Anchor pseudo-classes have no effect on elements other than &#39;A&#39;&quot;.&lt;br /&gt;&lt;br /&gt;
Also, CSS 1 and CSS 2.1 use a single colon to refer to the first-line pseudo-class (:first-line) but CSS 3 uses two colons (::first-line).&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;CSS 2.1&lt;/span&gt;&lt;br /&gt;
Includes all previous ones and adds these in:&lt;br /&gt;
&lt;br /&gt;
* → any element&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;*

/* selects any element */&lt;/pre&gt;E &gt; F → an F element child of an E element&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;div &amp;gt; p

/* selects any p elements that are direct children of a div element */&lt;/pre&gt;E:first-child → an E element, first child of its parent&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;p:first-child

/* selects a p element, the first child of its parent */&lt;/pre&gt;E:hover → an E element when it&#39;s hovered over&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;a:hover

/* selects any a element when it&#39;s hovered over */&lt;/pre&gt;E:focus → an E element while it has the focus&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;input:focus

/* selects any input element while it has the focus */&lt;/pre&gt;E:lang(fr) → an element of type E in language &quot;fr&quot;&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;*:lang(fr)

/* selects any element which language matches french*/&lt;/pre&gt;E[foo] → an E element with a &quot;foo&quot; attribute&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;img[title]

/* selects any img element with a title attribute */&lt;/pre&gt;E + F → an F element immediately preceded by an E element&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;div + span

/* selects any span element immediately preceded by a sibling div element */
&lt;/pre&gt;E[foo=&quot;bar&quot;] → an E element whose &quot;foo&quot; attribute value is exactly equal to &quot;bar&quot;&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;p[class=&quot;example&quot;]

/* selects any p element whose class attribute is exactly equal to &quot;example&quot; */
&lt;/pre&gt;E[foo~=&quot;bar&quot;] → an E element whose &quot;foo&quot; attribute value is a list of whitespace-separated values, one of which is exactly equal to &quot;bar&quot;&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;a[title~=&quot;copyright&quot;]

/* selects any a element whose title attribute value is a list of
whitespace-separated values, one of which is exactly equal to &quot;copyright&quot; */
&lt;/pre&gt;E[foo|=&quot;en&quot;] → an E element whose &quot;foo&quot; attribute has a hyphen-separated list of values beginning (from the left) with &quot;en&quot;&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;*[lang|=&quot;en&quot;]

/* selects any element whose lang attribute has a hyphen-separated list of
values beginning (from the left) with &quot;en&quot; */
&lt;/pre&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;CSS 3&lt;/span&gt;&lt;br /&gt;
Includes all previous ones and adds these in:&lt;br /&gt;
&lt;br /&gt;
E[foo^=&quot;bar&quot;] → an E element whose &quot;foo&quot; attribute value begins exactly with the string &quot;bar&quot;&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;object[type^=&quot;image/&quot;]

/* selects any object element whose type attribute value begins with &quot;image/&quot; */&lt;/pre&gt;E[foo$=&quot;bar&quot;] → an E element whose &quot;foo&quot; attribute value ends exactly with the string &quot;bar&quot;&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;a[href$=&quot;.html&quot;]

/* selects any a element whose href attribute value ends with &quot;.html&quot; */&lt;/pre&gt;E[foo*=&quot;bar&quot;] → an E element whose &quot;foo&quot; attribute value contains the substring &quot;bar&quot;&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;p[title*=&quot;hello&quot;]

/* selects any p element whose title attribute value contains the
substring &quot;hello&quot; */&lt;/pre&gt;E:root → an E element, root of the document	&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;:root

/* selects the root of the document, i.e. the HTML element */&lt;/pre&gt;E:nth-child(n) → an E element, the n-th child of its parent&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;tr:nth-child(0n+3)

/* selects any tr element that is the third child of its parent */&lt;/pre&gt;E:nth-last-child(n) → an E element, the n-th child of its parent, counting from the last one&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;tr:nth-last-child(-n+2)

/* selects any tr element that is one of the two last rows of an HTML table */&lt;/pre&gt;E:nth-of-type(n) → an E element, the n-th sibling of its type&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;p:nth-of-type(odd)

/* selects any p element with odd position */&lt;/pre&gt;E:nth-last-of-type(n) → an E element, the n-th sibling of its type, counting from the last one&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;p:nth-of-type(even)

/* selects any p element with even position, starting from the last */&lt;/pre&gt;E:last-child → an E element, last child of its parent&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;ol &amp;gt; li:last-child

/* selects any li element which is a last child of an ol element */&lt;/pre&gt;E:first-of-type → an E element, first sibling of its type&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;dt:first-of-type

/* selects any dt element that is the first sibling of its type */&lt;/pre&gt;E:last-of-type → an E element, last sibling of its type&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;td:last-of-type

/* selects any td element that is the last sibling of its type */&lt;/pre&gt;E:only-child → an E element, only child of its parent&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;tr:only-child

/* selects any tr element that is an only child */&lt;/pre&gt;E:only-of-type → an E element, only sibling of its type&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;p:only-of-type

/* selects any p element which is the only of its type in a parent */&lt;/pre&gt;E:empty → an E element that has no children (including text nodes)&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;div:empty

/* selects any empty div element */&lt;/pre&gt;E:target → an E element being the target of the referring URI&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;p:target

/* selects any p element that is the target element of the referring URI */&lt;/pre&gt;E:enabled → a user interface element E which is enabled&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;input:enabled

/* selects any input element that is in a enabled state */&lt;/pre&gt;E:disabled	→ a user interface element E which is disabled&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;input:disabled

/* selects any input element that is in a disabled state */&lt;/pre&gt;E:checked	→ a user interface element E which is checked&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;input:checked

/* selects any input element that is selected or is toggled &quot;on&quot; */&lt;/pre&gt;E:not(s) → an E element that does not match simple selector s&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;button:not([disabled])

/* selects all button elements that are not disabled*/&lt;/pre&gt;E ~ F → an F element preceded by an E element&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;div ~ p

/* selects any p element that is a sibling to any div element */&lt;/pre&gt;&lt;br /&gt;
Sources:&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/ENpHk&quot;&gt;Cascading Style Sheets, level 1&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/vNZaZ&quot;&gt;Cascading Style Sheets Level 2 Revision 1 (CSS 2.1)&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/iPoQ7&quot;&gt;Selectors Level 3&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://alebelcor.blogspot.com/feeds/638201443687022207/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://alebelcor.blogspot.com/2011/09/css-selectors.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/638201443687022207'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/638201443687022207'/><link rel='alternate' type='text/html' href='http://alebelcor.blogspot.com/2011/09/css-selectors.html' title='CSS Selectors'/><author><name>Alejandro Beltran</name><uri>http://www.blogger.com/profile/13533114582122558035</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://2.bp.blogspot.com/_RTIp6dEXzUs/TU3aPr0kdtI/AAAAAAAAAAU/AFDiZQxwOwg/s220/1.PNG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6442308477719289110.post-433596186772043243</id><published>2011-08-12T21:01:00.000-07:00</published><updated>2011-08-12T21:01:14.789-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><category scheme="http://www.blogger.com/atom/ns#" term="jquery"/><category scheme="http://www.blogger.com/atom/ns#" term="type"/><title type='text'>jQuery.type</title><content type='html'>In my last post I wrote about how to detect a JavaScript array, so I thought it would be a good idea to see how jQuery does it.&lt;br /&gt;
&lt;br /&gt;
Here&#39;s the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;jQuery.isArray()&lt;/span&gt; method (in 1.6.2):&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;isArray: Array.isArray || function( obj ) {
    return jQuery.type(obj) === &quot;array&quot;;
}
&lt;/pre&gt;It uses the native &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Array.isArray()&lt;/span&gt; method if it exists, if not it will find out the type of whatever is passed in using the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;jQuery.type()&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;&lt;/span&gt;method and see if that equals the string &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&quot;array&quot;&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
But come on, that&#39;s not much of a learning experience not to mention it makes for a pretty short blog post.&lt;br /&gt;
&lt;br /&gt;
So instead, I looked into the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;jQuery.type()&lt;/span&gt; method to see how that works.&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;jQuery.type()&lt;/span&gt;&lt;br /&gt;
It looks something like this:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;...
// Save a reference to some core methods
toString = Object.prototype.toString
...
// [[Class]] -&amp;gt; type pairs
class2type = {};
...
type: function( obj ) {
    return obj == null ?
        String( obj ) :
        class2type[ toString.call(obj) ] || &quot;object&quot;;
}
...
// Populate the class2type map
jQuery.each(
    &quot;Boolean Number String Function Array Date RegExp Object&quot;.split(&quot; &quot;),
    function(i, name) {
	class2type[ &quot;[object &quot; + name + &quot;]&quot; ] = name.toLowerCase();
})
&lt;/pre&gt;(Distant pieces of code are separated with three dots.)&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;Breaking it down&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Line 3 - A reference to the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;toString()&lt;/span&gt; method of &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Object&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Line 6 - An internal object (map) that will contain all types as strings.&lt;/li&gt;
&lt;li&gt;Line 15-19 - For each member in the array we&#39;re passing in, we&#39;ll create a property in the object and set its value to the name of the array member in lowercase.&lt;/li&gt;
&lt;li&gt;Line 8-12 - The method will check if the argument passed in is &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;null&lt;/span&gt; or &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;undefined&lt;/span&gt;, if so, it will return the string &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&quot;null&quot;&lt;/span&gt; or &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&quot;undefined&quot;&lt;/span&gt; (by calling the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;String&lt;/span&gt; constructor without the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;new&lt;/span&gt; keyword, which is allowed for some constructors). If it&#39;s something else, we&#39;ll call the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;toString()&lt;/span&gt; method on the argument and look to see if the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;class2type&lt;/span&gt; object has a value for it, if not, we&#39;ll just return the string &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&quot;object&quot;&lt;/span&gt;.&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
To give you a better overall picture, here&#39;s how the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;class2type&lt;/span&gt; object would look after page load:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;var class2type = {
    &quot;[object Array]&quot;: &quot;array&quot;,
    &quot;[object Boolean]&quot;: &quot;boolean&quot;,
    &quot;[object Date]&quot;: &quot;date&quot;,
    &quot;[object Function]&quot;: &quot;function&quot;,
    &quot;[object Number]&quot;: &quot;number&quot;,
    &quot;[object Object]&quot;: &quot;object&quot;,
    &quot;[object RegExp]&quot;: &quot;regexp&quot;,
    &quot;[object String]&quot;: &quot;string&quot;
};
&lt;/pre&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;Epiphany&lt;/span&gt;&lt;br /&gt;
That&#39;s why we call the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;toString()&lt;/span&gt; method of &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Object&lt;/span&gt; when we&#39;re looking for the property name. Remember how this method returns this sort of&amp;nbsp;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&quot;[object Constructor]&quot;&lt;/span&gt; string pattern?&lt;br /&gt;
&lt;br /&gt;
So, if we did &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;jQuery.type(&quot;hello&quot;)&lt;/span&gt; we&#39;d actually be doing something like &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;class2type[&quot;[object String]&quot;]&lt;/span&gt;, which would return the corresponding value &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&quot;string&quot;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
And that&#39;s how the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;jQuery.type()&lt;/span&gt; method works.&lt;br /&gt;
&lt;br /&gt;
It&#39;s not that complicated when you have an &quot;Epiphany&quot; section, huh?&lt;br /&gt;
&lt;br /&gt;
Sources:&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/1T9yK&quot;&gt;jQuery.type() - jQuery API Documentation&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/MW8WI&quot;&gt;jQuery 1.6.2 Source Code&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://alebelcor.blogspot.com/feeds/433596186772043243/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://alebelcor.blogspot.com/2011/08/jquerytype.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/433596186772043243'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/433596186772043243'/><link rel='alternate' type='text/html' href='http://alebelcor.blogspot.com/2011/08/jquerytype.html' title='jQuery.type'/><author><name>Alejandro Beltran</name><uri>http://www.blogger.com/profile/13533114582122558035</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://2.bp.blogspot.com/_RTIp6dEXzUs/TU3aPr0kdtI/AAAAAAAAAAU/AFDiZQxwOwg/s220/1.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6442308477719289110.post-4565998302411570612</id><published>2011-07-21T10:30:00.005-07:00</published><updated>2011-07-21T13:22:10.100-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="array"/><category scheme="http://www.blogger.com/atom/ns#" term="constructor"/><category scheme="http://www.blogger.com/atom/ns#" term="isarray"/><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><title type='text'>How to detect a JavaScript array</title><content type='html'>&quot;Piece of cafe&quot; I bet you&#39;re thinking, oh I wish it were that easy.&lt;br /&gt;
&lt;br /&gt;
But fear not, JavaScript does have the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;typeof&lt;/span&gt; operator (returns a string) which is very useful for type-checking:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;var str = &#39;hello&#39;,
    num = 64,
    bool = false,
    func = function () {},
    undef;

typeof str; // &quot;string&quot;
typeof num; // &quot;number&quot;
typeof bool; // &quot;boolean&quot;
typeof func; // &quot;function&quot;
typeof undef; // &quot;undefined&quot;
&lt;/pre&gt;Like shown in the previous code, it works fine for most things. However, it&#39;s a little flawed:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;var n = null,
    str = new String(&#39;hello&#39;), // any wrapper function or custom constructor
    arr = [1, 2, 3];

typeof n; // &quot;object&quot;, which is wrong
typeof str; // &quot;object&quot;, which is correct but not very useful.
typeof arr; // &quot;object&quot;, which is correct but not very useful.
&lt;/pre&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;constructor property&lt;/span&gt;&lt;br /&gt;
Every created object has a &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;constructor&lt;/span&gt; property that points to the function that created the object&#39;s prototype. You can leverage that reference to do some accurate detections:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;var str = new String(&#39;hello&#39;),
    arr = [1, 2, 3],
    obj,
    Person = function (name) {
        this.name = name;
    };

obj = new Person(&#39;John&#39;);

obj.constructor === Person; // true
str.constructor === String; // true
arr.constructor === Array; // true
&lt;/pre&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;isArray method&lt;/span&gt;&lt;br /&gt;
ECMAScript 5 defines a new method for the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Array&lt;/span&gt; function called &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;isArray()&lt;/span&gt;. If it&#39;s not implemented by the browser you can define it like this:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;if (!Array.hasOwnProperty(&#39;isArray&#39;)) {
    Array.isArray = function (value) {
        return Object.prototype.toString.call(value) === &#39;[object Array]&#39;;
    };
}
&lt;/pre&gt;Invoking the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;toString()&lt;/span&gt; method of &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Object&lt;/span&gt;, gets us a string representation of the object, for arrays it&#39;s &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;[object Array]&lt;/span&gt;:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;var arr1 = [1, 2, 3],
    arr2 = new Array(1, 2, 3);

Array.isArray(arr1); // true
Array.isArray(arr2); // true
Array.isArray(new Number(&#39;12&#39;)); // false
Array.isArray({}); // false
&lt;/pre&gt;&lt;br /&gt;
Sources:&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/qGNiS&quot;&gt;typeof operator (MDC)&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/oOmxS&quot;&gt;constructor property (MDC)&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://alebelcor.blogspot.com/feeds/4565998302411570612/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://alebelcor.blogspot.com/2011/07/how-to-detect-javascript-array.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/4565998302411570612'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/4565998302411570612'/><link rel='alternate' type='text/html' href='http://alebelcor.blogspot.com/2011/07/how-to-detect-javascript-array.html' title='How to detect a JavaScript array'/><author><name>Alejandro Beltran</name><uri>http://www.blogger.com/profile/13533114582122558035</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://2.bp.blogspot.com/_RTIp6dEXzUs/TU3aPr0kdtI/AAAAAAAAAAU/AFDiZQxwOwg/s220/1.PNG'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6442308477719289110.post-7788896003175597318</id><published>2011-07-09T15:57:00.000-07:00</published><updated>2011-07-09T15:57:05.166-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><category scheme="http://www.blogger.com/atom/ns#" term="keyword"/><category scheme="http://www.blogger.com/atom/ns#" term="this"/><title type='text'>The this keyword in JavaScript</title><content type='html'>&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;What does &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace; font-size: large;&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt; mean?&lt;/span&gt;&lt;br /&gt;
Short answer, it depends.&lt;br /&gt;
&lt;br /&gt;
In general terms, you could say that the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;this&lt;/span&gt; keyword, in JavaScript, refers to the function&#39;s context, how it&#39;s invoked. There are four ways in which a function can be invoked: method form, function form, constructor form and apply form.&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;Method form&lt;/span&gt;&lt;br /&gt;
If a function is invoked in the method form, &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;this&lt;/span&gt; will be bound to the object that owns the function:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;var myObj = {
    name: &#39;Chuck&#39;,
    sayName: function () {
        return &#39;Hello &#39; + this.name;
    }
};

myObj.sayName(); // this refers to myObj, returns &#39;Hello Chuck&#39;
&lt;/pre&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;Function form&lt;/span&gt;&lt;br /&gt;
This means taking a function value and calling it. In ECMAScript 3, &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;this&lt;/span&gt; would be bound to the global object:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;var myFunc = function () {
    return this.location.href;
};

myFunc(); // this refers to window (in browsers), returns the current URL
&lt;/pre&gt;In ECMAScript 5 Strict Mode (&lt;a href=&quot;http://goo.gl/0V8Ef&quot;&gt;almost fully supported&lt;/a&gt;), &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;this&lt;/span&gt; was changed so that it would refer to &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;undefined&lt;/span&gt; instead:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;var myFunc = function () {
    &#39;use strict&#39;;
    return this.location.href;
};

myFunc(); // error, this is undefined
&lt;/pre&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;Constructor form&lt;/span&gt;&lt;br /&gt;
A constructor is meant to be called using the new keyword, in this form, &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;this&lt;/span&gt; will refer to the object being produced:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;var MyConstructor = function (name) {
    this.name = name;
    this.sayName = function () {
        return &#39;Hello &#39; + this.name;
    };
};

var myObj = new MyConstructor(&#39;Chuck&#39;);
myObj.name; // &#39;Chuck&#39;
myObj.sayName(); // &#39;Hello Chuck&#39;
&lt;/pre&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;Apply form&lt;/span&gt;&lt;br /&gt;
Functions are objects and, as most objects, they have methods. This form refers to using the function&#39;s methods &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;apply()&lt;/span&gt; or &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;call()&lt;/span&gt; to call a function. Both methods take whatever first argument you pass and bound &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;this&lt;/span&gt; to it. They differ in the way they provide the arguments to the function being invoked.&lt;br /&gt;
The &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;apply()&lt;/span&gt; method takes an array of arguments:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;var myObj1 = {
    name: &#39;Chuck&#39;
};
var myObj2 = {
    name: &#39;Charles&#39;
};
var sayWelcome = function (location) {
    return &#39;Hello &#39; + this.name + &#39;. Welcome to &#39; + location;
};

sayWelcome.apply(myObj1, [&#39;New York&#39;]); // &#39;Hello Chuck. Welcome to New York&#39;
sayWelcome.apply(myObj2, [&#39;Toronto&#39;]); // &#39;Hello Charles. Welcome to Toronto&#39;
&lt;/pre&gt;The &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;call()&lt;/span&gt; method takes a list of arguments separated by commas:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;sayWelcome.call(myObj1, &#39;New York&#39;); // &#39;Hello Chuck. Welcome to New York&#39;
sayWelcome.call(myObj2, &#39;Toronto&#39;); // &#39;Hello Charles. Welcome to Toronto&#39;
&lt;/pre&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;Summary&lt;/span&gt;&lt;br /&gt;
The value of &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;this&lt;/span&gt; depends on the calling form:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Method form: it&#39;s bound to the object containing the method.&lt;/li&gt;
&lt;li&gt;Function form: it&#39;s bound to either the global object (ECMAScript 3), or to &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;undefined&lt;/span&gt; in (ECMAScript 5 Strict Mode).&lt;/li&gt;
&lt;li&gt;Constructor form: it&#39;s bound to the new object being constructed.&lt;/li&gt;
&lt;li&gt;Apply form: it&#39;s bound to the first argument passed to &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;apply()&lt;/span&gt; or &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;call()&lt;/span&gt;.&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
Source:&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/s9vkC&quot;&gt;Crockford on JavaScript - Act III: Function the Ultimate&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://alebelcor.blogspot.com/feeds/7788896003175597318/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://alebelcor.blogspot.com/2011/07/this-keyword-in-javascript.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/7788896003175597318'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/7788896003175597318'/><link rel='alternate' type='text/html' href='http://alebelcor.blogspot.com/2011/07/this-keyword-in-javascript.html' title='The this keyword in JavaScript'/><author><name>Alejandro Beltran</name><uri>http://www.blogger.com/profile/13533114582122558035</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://2.bp.blogspot.com/_RTIp6dEXzUs/TU3aPr0kdtI/AAAAAAAAAAU/AFDiZQxwOwg/s220/1.PNG'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6442308477719289110.post-9029829897379769908</id><published>2011-06-16T10:00:00.000-07:00</published><updated>2011-06-16T10:00:01.437-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="best practice"/><category scheme="http://www.blogger.com/atom/ns#" term="bigdecimal"/><category scheme="http://www.blogger.com/atom/ns#" term="double"/><category scheme="http://www.blogger.com/atom/ns#" term="float"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><title type='text'>Why your Java math might be wrong</title><content type='html'>We all know that mathematical operations with decimals in JavaScript are no good. For instance:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;alert(1.1 + 2.2); // should be 3.3, but the result is 3.3000000000000003&lt;/pre&gt;&lt;br /&gt;
It turns out this is a cross to bare for pretty much any language that uses the IEEE-754 standard to implement a&amp;nbsp;floating&amp;nbsp;point number and that includes... *drums*...&amp;nbsp;&lt;b&gt;Java&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;// Adding doubles
System.out.println(1.1 + 2.2); // 3.3000000000000003

// Adding floats
System.out.println(1.1f + 2.2f); // 3.3000002
&lt;/pre&gt;&lt;br /&gt;
So if you&#39;re doing these kinds of operations with &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;float&lt;/span&gt;&amp;nbsp;or &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;double&lt;/span&gt;&amp;nbsp;in Java you might be doing them wrong.&lt;br /&gt;
&lt;br /&gt;
This is huge because an application that deals with money is almost certain to have to deal with decimals. You have to get it right or you&#39;ll be in a lot of trouble.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&quot;People have a reasonable expectation that when you’re adding up their money you’re going to come up with the right result, and you don’t&quot;&lt;/i&gt;&amp;nbsp;- Douglas Crockford&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;float&lt;/span&gt; and &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;double&lt;/span&gt; should never be used for currency or high precision operations.&lt;br /&gt;
&lt;br /&gt;
The proper way to do this is using the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;BigDecimal&lt;/span&gt; class. Check out the &lt;a href=&quot;http://goo.gl/r78E1&quot;&gt;documentation&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Let&#39;s try it again, this time using &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;BigDecimal&lt;/span&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;BigDecimal num1 = new BigDecimal(1.1);
BigDecimal num2 = new BigDecimal(2.2);
System.out.println(num1.add(num2)); // 3.30000000000000026645352591003756...etc.
&lt;/pre&gt;&lt;br /&gt;
Oh noes! what happened? I thought &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;BigDecimal&lt;/span&gt; was supposed to solve all my problems.&lt;br /&gt;
&lt;br /&gt;
Hold on, take another look at the code above. You&#39;ll notice we&#39;re constructing our &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;BigDecimal&lt;/span&gt; objects using a... *drums*... &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;double&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
So? Well, that&#39;s the deal with &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;float&lt;/span&gt; and &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;double&lt;/span&gt;, it&#39;s why they should not be used in these operations. They cannot accurately represent numbers with decimals.&lt;br /&gt;
&lt;br /&gt;
OK, how do we fix this? There&#39;s a constructor that takes a &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;String&lt;/span&gt;&amp;nbsp;argument&amp;nbsp;(didn&#39;t you look at the documentation?).&amp;nbsp;That&#39;s how we should always do it. Pass a &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;String&lt;/span&gt; so you won&#39;t lose precision:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: java&quot;&gt;BigDecimal num3 = new BigDecimal(&quot;1.1&quot;);
BigDecimal num4 = new BigDecimal(&quot;2.2&quot;);
System.out.println(num3.add(num4)); // 3.3, finally
&lt;/pre&gt;&lt;br /&gt;
Perfect! now lawsuits won&#39;t be falling from the sky.&lt;br /&gt;
&lt;br /&gt;
Thanks for reading and let me know your comments.&lt;br /&gt;
&lt;br /&gt;
Sources:&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/73HT9&quot;&gt;Representing money&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/DE0Aw&quot;&gt;Beware of floating point numbers&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://alebelcor.blogspot.com/feeds/9029829897379769908/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://alebelcor.blogspot.com/2011/06/why-your-java-math-might-be-wrong.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/9029829897379769908'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/9029829897379769908'/><link rel='alternate' type='text/html' href='http://alebelcor.blogspot.com/2011/06/why-your-java-math-might-be-wrong.html' title='Why your Java math might be wrong'/><author><name>Alejandro Beltran</name><uri>http://www.blogger.com/profile/13533114582122558035</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://2.bp.blogspot.com/_RTIp6dEXzUs/TU3aPr0kdtI/AAAAAAAAAAU/AFDiZQxwOwg/s220/1.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6442308477719289110.post-986112766189575010</id><published>2011-05-26T11:00:00.006-07:00</published><updated>2011-05-26T11:00:04.801-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><category scheme="http://www.blogger.com/atom/ns#" term="ofuscation"/><category scheme="http://www.blogger.com/atom/ns#" term="security"/><title type='text'>How to hide your JavaScript code</title><content type='html'>Billy Hoffman, author of &quot;Ajax Security&quot;, has a very interesting talk about JavaScript named &quot;The Evil Parts&quot; where he talks about the &quot;evil&quot; things a person can accomplish using JavaScript.&lt;br /&gt;
&lt;br /&gt;
One of the things he shows us are two functions, a dehydrate and a hydrate function. He uses them to hide/display malicious code, to &quot;dehydrate&quot; JavaScript, so the code is transformed into whitespace and tabs, therefore becoming invisible.&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;// to &quot;dehydrate&quot; a string of code
function dehydrate(s) {
    var r = new Array();
    for (var i = 0; i &amp;lt; s.length; i++) {
        for (var j = 6; j &amp;gt;= 0; j--) {
            if (s.charCodeAt(i) &amp;amp; (Math.pow(2, j))) {
                r.push(&#39; &#39;);
            } else {
                r.push(&#39;\t&#39;);
            }
        }
    }
    r.push(&#39;\n&#39;);
    return r.join(&#39;&#39;);
}
// to &quot;hydrate&quot; a string of code
function hydrate(s) {
    var r = []; var curr = 0;
    while (s.charAt(curr) != &#39;\n&#39;) {
        var tmp = 0;
        for (var i = 6; i &amp;gt;= 0; i--) {
            if (s.charAt(curr) == &#39; &#39;) {
                tmp = tmp | (Math.pow(2, i));
            }
            curr++;
        }
        r.push(String.fromCharCode(tmp));
    }
    return r.join(&#39;&#39;);
}
&lt;/pre&gt;&lt;br /&gt;
So you dehydrate the code to hide it. And you hydrate it back and then eval it to execute it.&lt;br /&gt;
The code does become larger (x7 according to him) because it goes character by character transforming them, but hey, it&#39;s invisible code!&lt;br /&gt;
&lt;br /&gt;
Give it a try, you know you want to.&lt;br /&gt;
&lt;br /&gt;
Source:&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/3wfYi&quot;&gt;Billy Hoffman - JavaScript: The Evil Parts&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://alebelcor.blogspot.com/feeds/986112766189575010/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://alebelcor.blogspot.com/2011/05/how-to-hide-your-javascript-code.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/986112766189575010'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/986112766189575010'/><link rel='alternate' type='text/html' href='http://alebelcor.blogspot.com/2011/05/how-to-hide-your-javascript-code.html' title='How to hide your JavaScript code'/><author><name>Alejandro Beltran</name><uri>http://www.blogger.com/profile/13533114582122558035</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://2.bp.blogspot.com/_RTIp6dEXzUs/TU3aPr0kdtI/AAAAAAAAAAU/AFDiZQxwOwg/s220/1.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6442308477719289110.post-2257540764424833439</id><published>2011-05-19T11:00:00.017-07:00</published><updated>2011-08-20T14:06:29.376-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="global"/><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><category scheme="http://www.blogger.com/atom/ns#" term="variables"/><title type='text'>How many global variables are there?</title><content type='html'>We all know global variables in JavaScript are bad.&lt;br /&gt;
&lt;br /&gt;
If you declare a bunch of variables/functions in the global namespace they become properties of the global object, in the browser that would be the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;window&lt;/span&gt; object.&lt;br /&gt;
&lt;br /&gt;
So the other day I was thinking of a way to find out how many of them were in a page. So I came up with this script that you can run to find out:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;(function (window) {
    var document = window.document,
        hasOwnProperty = Object.prototype.hasOwnProperty,
        globals = [],
        prop,
        div;

    for (prop in window) {
        globals.push(&#39;&amp;lt;li&amp;gt;&#39;);
        !hasOwnProperty.call(window, prop) &amp;amp;&amp;amp; globals.push(&#39;@&#39;);
        globals.push(prop.toString());
        globals.push(&#39;&amp;lt;/li&amp;gt;&#39;);
    }

    div = document.getElementById(&#39;abc-js-globals&#39;) ||
        document.createElement(&#39;div&#39;);
    div.id = div.id || &#39;abc-js-globals&#39;;
    div.innerHTML = &#39;&amp;lt;h2&amp;gt;Global Variables:&amp;lt;/h2&amp;gt;&amp;lt;ul&amp;gt;&#39;
        + globals.join(&#39;&#39;) + &#39;&amp;lt;/ul&amp;gt;&#39;;
    document.body.appendChild(div);

}(window));
&lt;/pre&gt;&lt;br /&gt;
You can paste that in the Firebug/Inspector console and a list of the globals will show up at the end of the page.&lt;br /&gt;
The names that start with &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;@&lt;/span&gt; mean they are properties inherited from the prototype chain.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;UPDATE:&lt;/b&gt;&amp;nbsp;I changed the code to be slightly faster (I think). I guess that makes it version 1.1.</content><link rel='replies' type='application/atom+xml' href='http://alebelcor.blogspot.com/feeds/2257540764424833439/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://alebelcor.blogspot.com/2011/05/how-many-global-variables-are-there.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/2257540764424833439'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/2257540764424833439'/><link rel='alternate' type='text/html' href='http://alebelcor.blogspot.com/2011/05/how-many-global-variables-are-there.html' title='How many global variables are there?'/><author><name>Alejandro Beltran</name><uri>http://www.blogger.com/profile/13533114582122558035</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://2.bp.blogspot.com/_RTIp6dEXzUs/TU3aPr0kdtI/AAAAAAAAAAU/AFDiZQxwOwg/s220/1.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6442308477719289110.post-9092498782351466914</id><published>2011-04-28T11:00:00.178-07:00</published><updated>2011-05-06T19:38:37.285-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><category scheme="http://www.blogger.com/atom/ns#" term="jquery"/><category scheme="http://www.blogger.com/atom/ns#" term="trim"/><title type='text'>jQuery.trim</title><content type='html'>Do you want to know how the trim method works in jQuery? Today is lucky day.&lt;br /&gt;
&lt;br /&gt;
Here&#39;s the code from the jQuery 1.5.2 source, I separated distant lines of code with three dots:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;...
// Check if a string has a non-whitespace character in it
rnotwhite = /\S/,

// Used for trimming whitespace
trimLeft = /^\s+/,
trimRight = /\s+$/,
...
// Save a reference to some core methods
...
trim = String.prototype.trim,
...
// Use native String.trim function wherever possible
trim: trim ?
    function( text ) {
        return text == null ?
            &quot;&quot; :
            trim.call( text );
    } :

    // Otherwise use our own trimming functionality
    function( text ) {
        return text == null ?
            &quot;&quot; :
            text.toString().replace( trimLeft, &quot;&quot; ).replace( trimRight, &quot;&quot; );
    },
...
// IE doesn&#39;t match non-breaking spaces with \s
if ( rnotwhite.test( &quot;\xA0&quot; ) ) {
    trimLeft = /^[\s\xA0]+/;
    trimRight = /[\s\xA0]+$/;
}
...
&lt;/pre&gt;&lt;br /&gt;
Line 14 is where the ternary action to determine how &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;$.trim&lt;/span&gt; will behave begins, all the way through line 26. Another way to look at it is this:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;trim: trim ?
    function( text ) {
        // native trimming
    } :
    function( text ) {
        // jQuery trimming
    },
&lt;/pre&gt;&lt;br /&gt;
You might find &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;trim: trim&lt;/span&gt; hard to understand, but not when you look at the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;trim&lt;/span&gt; variable in line 11. Notice how jQuery stores an internal reference to the native &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;trim&lt;/span&gt; method of strings. So, if we have native support for trimming, i.e. checking for truthy value in the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;trim&lt;/span&gt; variable, we go ahead and do it that way but not before checking for &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;null&lt;/span&gt; or &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;undefined&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
If the browser doesn&#39;t have support, we&#39;ll check if the passed string is not &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;null&lt;/span&gt; or &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;undefined&lt;/span&gt;, if it is, we&#39;ll return a blank string (just like in the first case), otherwise we&#39;ll trim it. Let&#39;s see how.&lt;br /&gt;
&lt;br /&gt;
We&#39;re calling the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;toString&lt;/span&gt; method on the passed string to, and this is a total guess, make sure we have a string to work with before attempting any operation.&lt;br /&gt;
&lt;br /&gt;
Then we&#39;ll trim the left side of the string and after that the right side. The regular expressions for those (in lines 6 &amp;amp; 7) match, pretty much, every type of space you wouldn&#39;t want in your string. So, we make sure that the passed string doesn&#39;t have those, and if it does, we get rid of them by replacing them with a blank string.&lt;br /&gt;
&lt;br /&gt;
We&#39;re just missing lines 2-3 and 28-32. &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;rnotwhite&lt;/span&gt; matches a single character other than white space and lines 28-32 check if we should be checking for non-breaking spaces (&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;A0&lt;/span&gt; in hexadecimal) in our trimming, mostly because of IE like the comment says. If so, we need to redefine our &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;trimLeft&lt;/span&gt; and &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;trimRight&lt;/span&gt; to include matching the non-breaking space character.&lt;br /&gt;
&lt;br /&gt;
Hopefully I&#39;m right about some of this but if not, let me know in the comments.&lt;br /&gt;
&lt;br /&gt;
Thanks for reading.&lt;br /&gt;
&lt;br /&gt;
Sources:&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/ycx2j&quot;&gt;jQuery 1.5.2 source code&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/kD5JA&quot;&gt;jQuery API - jQuery.trim()&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://alebelcor.blogspot.com/feeds/9092498782351466914/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://alebelcor.blogspot.com/2011/04/trim-in-jquery.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/9092498782351466914'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/9092498782351466914'/><link rel='alternate' type='text/html' href='http://alebelcor.blogspot.com/2011/04/trim-in-jquery.html' title='jQuery.trim'/><author><name>Alejandro Beltran</name><uri>http://www.blogger.com/profile/13533114582122558035</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://2.bp.blogspot.com/_RTIp6dEXzUs/TU3aPr0kdtI/AAAAAAAAAAU/AFDiZQxwOwg/s220/1.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6442308477719289110.post-7350722659734300568</id><published>2011-04-21T14:30:00.007-07:00</published><updated>2011-05-06T19:38:11.979-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><category scheme="http://www.blogger.com/atom/ns#" term="jquery"/><category scheme="http://www.blogger.com/atom/ns#" term="json"/><title type='text'>jQuery.parseJSON</title><content type='html'>The other day I was re-watching (haha, learning through repetition) &quot;11 More Things I Learned from the jQuery Source&quot; and this time I was quite amazed when Paul Irish talked about parseJSON. And the reason was the way jQuery makes the JSON object, it&#39;s clever, it&#39;s very clever.&lt;br /&gt;
&lt;br /&gt;
Let me show you. Here&#39;s jQuery&#39;s 1.5.2 parseJSON method:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;parseJSON: function( data ) {
    if ( typeof data !== &quot;string&quot; || !data ) {
        return null;
    }

    // Make sure leading/trailing whitespace is removed (IE can&#39;t handle it)
    data = jQuery.trim( data );

    // Make sure the incoming data is actual JSON
    // Logic borrowed from http://json.org/json2.js
    if ( rvalidchars.test(data.replace(rvalidescape, &quot;@&quot;)
        .replace(rvalidtokens, &quot;]&quot;)
        .replace(rvalidbraces, &quot;&quot;)) ) {

        // Try to use the native JSON parser first
        return window.JSON &amp;amp;&amp;amp; window.JSON.parse ?
            window.JSON.parse( data ) :
            (new Function(&quot;return &quot; + data))();

    } else {
        jQuery.error( &quot;Invalid JSON: &quot; + data );
    }
},
&lt;/pre&gt;&lt;br /&gt;
Let&#39;s dissect that:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Lines 2-4 are just checking you pass a string and its not empty.&lt;/li&gt;
&lt;li&gt;Line 7 explains itself.&lt;/li&gt;
&lt;li&gt;Lines 11-13 is a regular expression evaluation to make sure we have valid JSON. &lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
The interesting part, in this case, is the return statement:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;return window.JSON &amp;amp;&amp;amp; window.JSON.parse ?
    window.JSON.parse( data ) :
    (new Function(&quot;return &quot; + data))();
&lt;/pre&gt;&lt;br /&gt;
If the browser has native JSON support, use that.&amp;nbsp;If not, and this is the interesting part, we&#39;ll do &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;(new Function(&quot;return &quot; + data))()&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
But what does that mean? I&#39;m glad you asked.&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;We&#39;re calling the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Function&lt;/span&gt; constructor, which will return a &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Function&lt;/span&gt; object (function reference) and&amp;nbsp;immediately&amp;nbsp;invoke it. &lt;/li&gt;
&lt;li&gt;The &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Function&lt;/span&gt; constructor takes an &quot;n&quot; number of arguments that will become the function&#39;s parameters, being the last one the body of the function.&amp;nbsp;We&#39;re passing only one parameter, meaning &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&quot;return &quot; + data&lt;/span&gt; will become the function&#39;s body.&lt;/li&gt;
&lt;li&gt;What does the constructed function do then? Considering &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;data&lt;/span&gt; is a JSON string it becomes rather obvious now. It returns a JavaScript object.&lt;/li&gt;
&lt;/ul&gt;See? I told you it was clever, no need for eval() this way.&lt;br /&gt;
&lt;br /&gt;
Thanks for reading and let me know your comments.&lt;br /&gt;
&lt;br /&gt;
Source:&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/NiNm6&quot;&gt;11 More Things I Learned From The jQuery Source&lt;/a&gt;&amp;nbsp;by Paul Irish&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/PfwKh&quot;&gt;Function - MDN&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://alebelcor.blogspot.com/feeds/7350722659734300568/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://alebelcor.blogspot.com/2011/04/parsejson-in-jquery.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/7350722659734300568'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/7350722659734300568'/><link rel='alternate' type='text/html' href='http://alebelcor.blogspot.com/2011/04/parsejson-in-jquery.html' title='jQuery.parseJSON'/><author><name>Alejandro Beltran</name><uri>http://www.blogger.com/profile/13533114582122558035</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://2.bp.blogspot.com/_RTIp6dEXzUs/TU3aPr0kdtI/AAAAAAAAAAU/AFDiZQxwOwg/s220/1.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6442308477719289110.post-2533691991665788546</id><published>2011-04-19T11:00:00.014-07:00</published><updated>2011-04-19T11:00:01.652-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="asshole"/><category scheme="http://www.blogger.com/atom/ns#" term="management"/><category scheme="http://www.blogger.com/atom/ns#" term="no"/><category scheme="http://www.blogger.com/atom/ns#" term="rule"/><title type='text'>Beware of Assholes</title><content type='html'>&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia,&#39;Times New Roman&#39;,serif; font-size: large;&quot;&gt;What is an asshole?&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Someone who either intentionally or unintentionally infuriates/demeans/damages their peers/superiors/underlings.&lt;/li&gt;
&lt;li&gt;There are two flavors: Temporary and Certified assholes. A temporary asshole is someone who is probably having a bad day or a bad moment. Certified assholes are persistently nasty and destructive jerks.&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia,&#39;Times New Roman&#39;,serif; font-size: large;&quot;&gt;Why are they dangerous?&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Remember how they can damage people? well, at times that includes clients and customers.&lt;/li&gt;
&lt;li&gt;They undermine organizational performance.&lt;/li&gt;
&lt;li&gt;Asshole poisoning is a contagious disease that anyone can catch. Once you unleash disdain, anger and contempt or someone else unleashes it on you, it spreads like wildfire.&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia,&#39;Times New Roman&#39;,serif; font-size: large;&quot;&gt;What should you care?&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;The total costs of assholes are HIGH. From loss of motivation, higher stress and turnover to legal and HR management costs like inside and outside counsel.&lt;/li&gt;
&lt;li&gt;If you sap the energy out of people, you may be sucking the life out of your career.&lt;/li&gt;
&lt;li&gt;When people feel mistreated and dissatisfied&amp;nbsp;with&amp;nbsp;their jobs, they are unwilling to do extra work to help their organization.&lt;/li&gt;
&lt;li&gt;Writing, displaying and repeating words about treating&amp;nbsp;people&amp;nbsp;with respect, but allowing or encouraging the opposite behavior, is worse than useless. The organization and its leaders are seen as hypocrites, which fuels cynicism and scorn.&lt;/li&gt;
&lt;li&gt;Organizations that drive in compassion and drive out fear attract superior talent, share ideas more freely, have less dysfunctional internal competition, and trump the external competition.&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia,&#39;Times New Roman&#39;,serif; font-size: large;&quot;&gt;What should you do?&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Adopt The No Asshole Rule.&lt;/li&gt;
&lt;li&gt;Have yourself tested. &lt;/li&gt;
&lt;li&gt;Get rid of assholes fast.&lt;/li&gt;
&lt;li&gt;Treat certified assholes as incompetent employees. &lt;/li&gt;
&lt;li&gt;Model and teach constructive confrontation. &lt;/li&gt;
&lt;li&gt;Improve your hiring process. Recruiters tend to hire candidates who look and act like his or her favorite person on the planet- him or herself. Assholes will hire assholes.&lt;/li&gt;
&lt;li&gt;Be like Google. Like Google, not &quot;be&quot; Google. They have a &quot;Don&#39;t be evil&quot; motto. The company works to screen them out in  hiring, and nasty people suffer during performance evaluations and  aren&#39;t promoted to management positions. &lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
&quot;The No Asshole Rule&quot; by Robert I. Sutton is a must-read for anyone looking to make a difference in their workplace. It is a very entertaining book and quite a learning experience.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;This post contains information found in:&lt;/i&gt;&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/TZP6J&quot;&gt;The No Asshole Rule: Building a Civilized Workplace and Surviving One That Isn&#39;t&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://alebelcor.blogspot.com/feeds/2533691991665788546/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://alebelcor.blogspot.com/2011/04/beware-of-assholes.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/2533691991665788546'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/2533691991665788546'/><link rel='alternate' type='text/html' href='http://alebelcor.blogspot.com/2011/04/beware-of-assholes.html' title='Beware of Assholes'/><author><name>Alejandro Beltran</name><uri>http://www.blogger.com/profile/13533114582122558035</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://2.bp.blogspot.com/_RTIp6dEXzUs/TU3aPr0kdtI/AAAAAAAAAAU/AFDiZQxwOwg/s220/1.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6442308477719289110.post-4118667288635896448</id><published>2011-03-28T10:30:00.003-07:00</published><updated>2011-04-05T11:32:26.270-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="array"/><category scheme="http://www.blogger.com/atom/ns#" term="dom"/><category scheme="http://www.blogger.com/atom/ns#" term="htmlcollection"/><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><category scheme="http://www.blogger.com/atom/ns#" term="nodelist"/><category scheme="http://www.blogger.com/atom/ns#" term="theory"/><title type='text'>HTMLCollections &amp; NodeLists</title><content type='html'>Most of us believed, at least for some time, that in our DOM Scripting, we always dealt with arrays in our JavaScript:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;var my_links = document.getElementsByTagName(&#39;a&#39;); // we have three links
alert(my_links.length) // outputs &quot;3&quot;
&lt;/pre&gt;&lt;br /&gt;
We later found out that the things we thought were arrays, were instead array-like objects. But how exactly are they like arrays? Those &quot;array-like&quot; objects/elements/things, most of the time, are either &lt;b&gt;HTMLCollections &lt;/b&gt;or &lt;b&gt;NodeLists&lt;/b&gt;, not native JavaScript array objects. Take a look at what the specification says of them, the keyword is live:&lt;br /&gt;
&lt;div style=&quot;font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;&quot;&gt;&lt;i&gt;&lt;br /&gt;
&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;&quot;&gt;&lt;i&gt;&quot;An HTMLCollection is a list of nodes. Collections in the HTML DOM are assumed to be live meaning that they are automatically updated when the underlying document is changed.&quot;&lt;/i&gt;&lt;/div&gt;- DOM Level 1&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;&quot;&gt;&lt;i&gt;&quot;The NodeList interface provides the abstraction of an ordered collection of nodes, without defining or constraining how this collection is implemented. NodeList objects in the DOM are live.&quot;&lt;/i&gt;&lt;/div&gt;- DOM Level 3&lt;br /&gt;
&lt;br /&gt;
But what does &lt;b&gt;that &lt;/b&gt;mean? It means that live collections, if modified, are updated as the program runs. For example, this is an infinte loop:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;var i, j,
    my_links = document.getElementsByTagName(&#39;a&#39;); // we have three links

for (i = 0, j = my_links.length; i &amp;lt; j; i += 1 ) {
    document.body.appendChild(document.createElement(&#39;a&#39;));
}
&lt;/pre&gt;&lt;br /&gt;
We&#39;re getting our collection (of three links) and then for each link that we have, we&#39;re going to append another link to the body. So why is this infinite? Because the collection is live, which means that not only will &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;i&lt;/span&gt; increment, &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;j&lt;/span&gt; also will, so naturally the loop will keep going.&lt;br /&gt;
&lt;br /&gt;
So why are they called array-like objects? If it looks like an array and acts like an array, then it must be an array, right? Wrong, DOM collections look like arrays because:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;They have an associated index to each value in the container. But that&#39;s something an object can have too:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;var my_obj = {
    0: &#39;zero&#39;,
    1: &#39;one&#39;,
    2: &#39;two&#39;,
    3: &#39;three&#39;
};

alert( my_obj[0] ); // &#39;zero&#39;
alert( my_obj[3] ); // &#39;three&#39;
&lt;/pre&gt;The &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;alert&lt;/span&gt; statements might look like they want the elements with index &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;0&lt;/span&gt; and &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;3&lt;/span&gt; but you&#39;re really getting the value from the property named &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;0&lt;/span&gt; and &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;3&lt;/span&gt;.&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;They have a &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;length&lt;/span&gt; property. This is deceptive because arrays have this same property, but so do HTMLCollections and NodeLists. But, because these are not true arrays they do not have &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;push&lt;/span&gt;, &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;concat&lt;/span&gt;, &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;splice&lt;span style=&quot;font-family: inherit;&quot;&gt; &lt;/span&gt;&lt;/span&gt;or any of the other array methods.&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
Be prepared, these are some of the DOM methods (that I know of) that return an HTMLCollection or NodeList:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;// DOM Level 1/HTML 4.0
// ---------------------------
// Return an HTMLCollection
document.anchors
document.applets
document.forms 
document.images 
document.links

document.getElementsByName

formElement.elements
selectElement.options
tableElement.rows
tableElement.tBodies
tableRowElement.cells

// Not part of any standard
document.embeds
document.plugins

// DOM Level 2
// ------------------
// Return a NodeList
Node.childNodes

document.getElementsByName
document.getElementsByTagName
document.getElementsByTagNameNS
document.getElementsByName

element.getElementsByTagName
element.getElementsByTagNameNS

// WHATWG Web Applications 1.0
// ---------------------------
// Return a NodeList
document.getElementsByClassName
element.getElementsByClassName
&lt;/pre&gt;&lt;br /&gt;
Throughout this post I&#39;ve been talking about &lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;NodeLists&lt;span style=&quot;font-family: inherit;&quot;&gt; &lt;/span&gt;&lt;/span&gt;as such and not as &lt;i&gt;live &lt;/i&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;NodeLists &lt;/span&gt;because they are inherintely live. There&#39;s an exception to this, there are &lt;i&gt;static &lt;/i&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;NodeLists &lt;/span&gt;that act as snapshots and do not update when the document is modified:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;// Selectors API Level 1
// ---------------------
// Return a static NodeList
document.querySelectorAll
element.querySelectorAll
&lt;/pre&gt;&lt;br /&gt;
In conclusion, I think it&#39;s important to know the differences between a live DOM collection a true JavaScript array, it&#39;s also an important thing to be aware of because you&#39;ll eventually interact with these.&lt;br /&gt;
&lt;br /&gt;
I know so far I&#39;ve been talking mostly about the DOM (sorry, this wasn&#39;t the exception) but you cannot say that this was a boring topic, or was it?&lt;br /&gt;
&lt;br /&gt;
Thanks for reading and let me know your comments.&lt;br /&gt;
&lt;br /&gt;
Sources:&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/Rsjsz&quot;&gt;Why is getElementsByTagName() faster than querySelectAll()?&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/1j5SA&quot;&gt;Speed Up Your JavaScript (video)&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/EsmN4&quot;&gt;HTMLCollection - MDN Doc Center, NodeList - MDN Doc Center&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/PN66P&quot;&gt;DOM Level 1 Specification&lt;/a&gt;, &lt;a href=&quot;http://goo.gl/jDqpo&quot;&gt;DOM Level 2 Specification&lt;/a&gt;, &lt;a href=&quot;http://goo.gl/K6p1j&quot;&gt;DOM Level 3 Specification&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://alebelcor.blogspot.com/feeds/4118667288635896448/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://alebelcor.blogspot.com/2011/03/htmlcollections-nodelists.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/4118667288635896448'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/4118667288635896448'/><link rel='alternate' type='text/html' href='http://alebelcor.blogspot.com/2011/03/htmlcollections-nodelists.html' title='HTMLCollections &amp; NodeLists'/><author><name>Alejandro Beltran</name><uri>http://www.blogger.com/profile/13533114582122558035</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://2.bp.blogspot.com/_RTIp6dEXzUs/TU3aPr0kdtI/AAAAAAAAAAU/AFDiZQxwOwg/s220/1.PNG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6442308477719289110.post-7863852856145708308</id><published>2011-03-03T11:30:00.002-07:00</published><updated>2011-03-03T11:30:00.629-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="best practice"/><category scheme="http://www.blogger.com/atom/ns#" term="dom"/><category scheme="http://www.blogger.com/atom/ns#" term="dom theory"/><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><title type='text'>Using innerHTML</title><content type='html'>&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;innerHTML&lt;/span&gt; is a read/write property of a DOM element that gets/sets the HTML contained in the element.&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;It&#39;s fast&lt;/span&gt;&lt;br /&gt;
This might vary between browsers but, it&#39;s almost a fact that creating and inserting elements using &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;innerHTML&amp;nbsp;&lt;/span&gt;instead of DOM methods is faster, not only at execution time, it&#39;ll also make the script size lighter because less code is needed.&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;It&#39;s clean &amp;amp; readable&lt;/span&gt;&lt;br /&gt;
Although&amp;nbsp;the name, &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;innerHTML&lt;/span&gt;, might seem confusing at first, it comes as the better choice in terms of code readability because DOM methods are very verbose and can consume a lot of lines of code.&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;It&#39;s supported&lt;/span&gt;&lt;br /&gt;
It was first introduced by Microsoft as proprietary to IE and there&#39;s no spec that defines the&amp;nbsp;behavior&amp;nbsp;of &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;innerHTML&lt;/span&gt;&amp;nbsp;but it has been adopted by all major browsers because of its usefulness and it pretty much works the same in all of them.&lt;br /&gt;
&lt;br /&gt;
Creating and inserting using DOM methods:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;var newDiv = document.createElement(&#39;div&#39;);
newDiv.setAttribute(&#39;id&#39;, &#39;new-div&#39;);
newDiv.setAttribute(&#39;class&#39;, &#39;big-div&#39;);
var text = document.createTextNode(&#39;Some text here&#39;);
newDiv.appendChild(text);

document.body.appendChild(newDiv); // div is inserted in the tree
&lt;/pre&gt;&lt;br /&gt;
Creating and inserting using&amp;nbsp;innerHTML:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;var newDiv = &#39;&amp;lt;div id=\&#39;new-div\&#39; class=\&#39;big-div\&#39;&amp;gt;Some text here&amp;lt;/div&amp;gt;&#39;;
document.body.innerHTML = newDiv; // div is inserted in the tree&lt;/pre&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;Reasons not to use it&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Not standard.&amp;nbsp;Although&amp;nbsp;it&#39;s fast and it works, the bottom line is that it is not part of any W3C or DOM standard. However, there are plans of adding it to the HTML5 specification.&lt;/li&gt;
&lt;li&gt;XSS unsafe. You have to know when to use it, otherwise you are exposing your application to XSS attacks, choose DOM methods until you&#39;re familiar with the subject.&lt;/li&gt;
&lt;li&gt;Not implemented everywhere. There are some table related elements, in IE, that can&#39;t be modified with it. The implementation and behavior might vary from browser to browser.&lt;/li&gt;
&lt;li&gt;Destroys the children. Setting a value to &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;innerHTML&lt;/span&gt; will destroy every descendant to that element, if any of those descendants had event handlers, that could&amp;nbsp;potentially&amp;nbsp;create a memory leak in some browsers.&lt;/li&gt;
&lt;/ul&gt;&lt;div&gt;Thanks for reading and let me know your comments.&lt;br /&gt;
&lt;br /&gt;
Sources:&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/PKHtg&quot;&gt;innerHTML (Mozilla Developer Network)&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://goo.gl/UUyIw&quot;&gt;innerHTML (Microsoft Developer Network)&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://alebelcor.blogspot.com/feeds/7863852856145708308/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://alebelcor.blogspot.com/2011/03/using-innerhtml.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/7863852856145708308'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/7863852856145708308'/><link rel='alternate' type='text/html' href='http://alebelcor.blogspot.com/2011/03/using-innerhtml.html' title='Using innerHTML'/><author><name>Alejandro Beltran</name><uri>http://www.blogger.com/profile/13533114582122558035</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://2.bp.blogspot.com/_RTIp6dEXzUs/TU3aPr0kdtI/AAAAAAAAAAU/AFDiZQxwOwg/s220/1.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6442308477719289110.post-5144182243561536859</id><published>2011-02-28T11:30:00.004-07:00</published><updated>2011-02-28T11:30:02.686-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="best practice"/><category scheme="http://www.blogger.com/atom/ns#" term="dom"/><category scheme="http://www.blogger.com/atom/ns#" term="dom theory"/><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><title type='text'>Manipulating DOM elements and styles</title><content type='html'>&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;Manipulating elements&lt;/span&gt;&lt;br /&gt;
Once you have access to an element, you can manipulate it using assignment of object properties or DOM methods. Let&#39;s take a look at the examples:&lt;br /&gt;
&lt;br /&gt;
HTML&lt;br /&gt;
&lt;pre class=&quot;brush: html&quot;&gt;&amp;lt;img src=&quot;small-dog.png&quot; id=&quot;img-dog&quot;&amp;gt;
&lt;/pre&gt;JavaScript&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;var myImage = document.getElementById(&#39;img-dog&#39;);

// Object property
myImage.src = &#39;big-dog.png&#39;;

// DOM method
myImage.setAttribute(&#39;src&#39;, &#39;big-dog.png&#39;);
&lt;/pre&gt;&lt;br /&gt;
In both examples we&#39;re changing the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;src&lt;/span&gt; of our image element. It&#39;s recommended to use the first example, it&#39;s easier, shorter, faster and works across browsers. If, for some reason, you can&#39;t use it, use the second one. Here&#39;s a link to &lt;a href=&quot;http://goo.gl/HQo7a&quot;&gt;quirksmode.org&lt;/a&gt; with more information.&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;Style&lt;/span&gt;&lt;br /&gt;
You can also change an element&#39;s style dynamically . You can do so by changing the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;className&lt;/span&gt; property or applying a style directly into the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;style&lt;/span&gt; object.&lt;br /&gt;
&lt;br /&gt;
HTML&lt;br /&gt;
&lt;pre class=&quot;brush: html&quot;&gt;&amp;lt;span id=&quot;my-span&quot;&amp;gt;Some text here&amp;lt;/span&amp;gt;
&lt;/pre&gt;JavaScript&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;var mySpan = document.getElementById(&#39;my-span&#39;);

mySpan.className = &#39;big&#39;; // the CSS class &#39;big&#39; makes the text big
mySpan.style.textAlign = &#39;center&#39;; // centers the text
&lt;/pre&gt;&lt;br /&gt;
Both, &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;className&lt;/span&gt; and &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;style&lt;/span&gt;, are read/write. But, the style object will only reflect styles that are directly applied to the element (i.e. in the markup), not the ones from the cascade. To get those you need go a little bit further:&lt;br /&gt;
&lt;br /&gt;
HTML&lt;br /&gt;
&lt;pre class=&quot;brush: html&quot;&gt;&amp;lt;span id=&quot;my-span&quot;&amp;gt;Some text here&amp;lt;/span&amp;gt;
&lt;/pre&gt;CSS&lt;br /&gt;
&lt;pre class=&quot;brush: css&quot;&gt;#my-span { font-weight: bold; }
&lt;/pre&gt;JavaScript&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;var mySpan = document.getElementById(&#39;my-span&#39;);

mySpan.style.fontWeight; // returns an empty string
mySpan.currentStyle[&#39;fontWeight&#39;]; // returns &#39;bold&#39;, works only in IE
document.defaultView.
        getComputedStyle(mySpan, null).
        getPropertyValue(&#39;font-weight&#39;); // returns &#39;bold&#39;, doesn&#39;t work in IE
&lt;/pre&gt;As you can see:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;The CSS property on the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;style&lt;/span&gt; object didn&#39;t have anything and returned an empty string.&lt;/li&gt;
&lt;li&gt;IE&#39;s proprietary method is very nice and readable (you could also use the dot notation, e.g. &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;mySpan.currentStyle.fontWeight&lt;/span&gt;).&lt;/li&gt;
&lt;li&gt;The standard way is very verbose and complicated but works on all standards compliant&amp;nbsp;browsers.&lt;/li&gt;
&lt;/ul&gt;If you need to get styles individually I suggest creating a utility function to deal with the cross browser issues (an example from &lt;a href=&quot;http://goo.gl/vlIir&quot;&gt;quirksmode.org&lt;/a&gt;).&lt;br /&gt;
But in general, I recommend using CSS classes for dynamic styling. It is way easier and faster to add/modify/delete CSS classes. It&#39;s a style agnostic practice, in case you don&#39;t have control over the styles, and also encourages layer separation.&lt;br /&gt;
&lt;br /&gt;
Thanks for reading and let me know your comments.&lt;br /&gt;
&lt;br /&gt;
Source: &lt;a href=&quot;http://goo.gl/XoDym&quot;&gt;Douglas Crockford: &quot;Theory of the Dom&quot; (2 of 3)&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://alebelcor.blogspot.com/feeds/5144182243561536859/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://alebelcor.blogspot.com/2011/02/manipulating-dom-elements-and-styles.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/5144182243561536859'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/5144182243561536859'/><link rel='alternate' type='text/html' href='http://alebelcor.blogspot.com/2011/02/manipulating-dom-elements-and-styles.html' title='Manipulating DOM elements and styles'/><author><name>Alejandro Beltran</name><uri>http://www.blogger.com/profile/13533114582122558035</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://2.bp.blogspot.com/_RTIp6dEXzUs/TU3aPr0kdtI/AAAAAAAAAAU/AFDiZQxwOwg/s220/1.PNG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6442308477719289110.post-6018272455991949003</id><published>2011-02-07T12:00:00.002-07:00</published><updated>2011-02-27T19:51:14.327-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="best practice"/><category scheme="http://www.blogger.com/atom/ns#" term="dom"/><category scheme="http://www.blogger.com/atom/ns#" term="dom theory"/><category scheme="http://www.blogger.com/atom/ns#" term="javascript"/><title type='text'>The script tag and the DOM</title><content type='html'>Today I&#39;m going to talk about the DOM. Things you might not know, tricks, nuances and some best practices (I think). So, let&#39;s get to it.&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;The &amp;lt;script&amp;gt; tag&lt;/span&gt;&lt;br /&gt;
It first appeared in&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt; &lt;/span&gt;Netscape Navigator 2 as a way to put programs in HTML documents and it then became part of the HTML standard in version 4.&lt;br /&gt;
&lt;pre class=&quot;brush: html&quot;&gt;&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;...&amp;lt;/script&amp;gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;hr /&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;&amp;lt;!-- // --&amp;gt;&lt;/span&gt;&lt;br /&gt;
The &amp;lt;!-- // --&amp;gt; hack was a workaround to prevent HTML documents from displaying code on the page by older browsers like Mosaic. You can still see this pattern in pages but it&#39;s no longer necessary as today&#39;s browsers understand both the script tag and JavaScript.&lt;br /&gt;
&lt;pre class=&quot;brush: html&quot;&gt;&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;
&amp;lt;!--
  code here
// --&amp;gt;
&amp;lt;/script&amp;gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;hr /&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;The language attribute&lt;/span&gt;&lt;br /&gt;
Microsoft came up with it as a way to select what kind of language (e.g. VBScript) you wanted to use for your program. The W3C&amp;nbsp;deprecated&amp;nbsp;this in favor of the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;type&lt;/span&gt; attribute. Avoid the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;language&lt;/span&gt; attribute.&lt;br /&gt;
&lt;pre class=&quot;brush: html&quot;&gt;&amp;lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot;&amp;gt;...&amp;lt;/script&amp;gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;hr /&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;The src attribute&lt;/span&gt;&lt;br /&gt;
It allows you to load a script from an external file. Great, because you don&#39;t want to put JavaScript in your HTML, for a large number of reasons: it makes the HTML file heavier, it becomes less maintainable, it becomes less scalable, it&#39;s not cachable, it becomes hard to minify or inspect, etc. It is highly recommended that you do not write JavaScript in your HTML and instead use the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;src&lt;/span&gt; attribute.&lt;br /&gt;
&lt;pre class=&quot;brush: html&quot;&gt;&amp;lt;script type=&quot;text/javascript&quot; scr=&quot;js/script.js&quot;&amp;gt;&amp;lt;/script&amp;gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;hr /&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;The type attribute&lt;/span&gt;&lt;br /&gt;
The W3C added the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;type&lt;/span&gt; attribute to replace the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;language&lt;/span&gt; one. It takes a MIME type instead of the name of the language. The official MIME type for JavaScript is &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;application/javascript&lt;/span&gt; or &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;application/ecmascript&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;and the most up to date browsers understand this, but not IE. So, if you want cross-browser support (and you should) you have to use &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;type=&quot;text/javascript&quot;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;, at least for now. This is a required attribute for HTML validation, but in practice you can just leave this out, the default language on all major browsers is JavaScript and besides if you&#39;re using &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;,&amp;nbsp;it ignores it.&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush: html&quot;&gt;&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;...&amp;lt;/script&amp;gt;
&amp;lt;script&amp;gt;...&amp;lt;/script&amp;gt;
&amp;lt;script type=&quot;text/javascript&quot; src=&quot;js/script.js&quot;&amp;gt;...&amp;lt;/script&amp;gt;
&amp;lt;script src=&quot;js/script.js&quot;&amp;gt;...&amp;lt;/script&amp;gt;&lt;/pre&gt;&lt;br /&gt;
&lt;hr /&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;Loading time&lt;/span&gt;&lt;br /&gt;
Because of the way the browser works, loading of a page&#39;s assets is incremental and single-threaded, it is recommended that the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;script&lt;/span&gt; tags be placed as low in the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;body&lt;/span&gt; as possible. It is also recommended to place your CSS i.e. &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;link&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;, as high in the &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt; as possible, this will greatly improve the user experience.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;You should also minify, gzip and join your script files into one. These will reduce file size and the number of HTTP requests and thus, the time it takes for the browser to load the page.&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush: html&quot;&gt;&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;css/styles.css&quot;&amp;gt;
    &amp;lt;!-- head contents here --&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;!-- body contents here --&amp;gt;
    &amp;lt;script type=&quot;text/javascript&quot; src=&quot;js/script.js&quot;&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;hr /&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;document.write&lt;/span&gt;&lt;br /&gt;
If you call this before the &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;onload&lt;/span&gt; event triggers it will insert stuff into your document, but if you do it after&amp;nbsp;it, it destroys your document and replace it with new stuff. You should avoid using &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;document.write&lt;/span&gt;, there are better alternatives now.&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;Collections&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;document.anchors&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;document.applets&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;document.embeds&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;document.forms&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;document.frames&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;document.images&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;document.plugins&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;document.scripts&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;document.stylesheets&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;Netscape provided these as a way to get and modify elements easier. These are still available but obsolete and you should avoid using them as there are better alternatives now.&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;The name and id attributes&lt;/span&gt;&lt;br /&gt;
If an element didn&#39;t have an &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;id&lt;/span&gt; but it had a &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;name&lt;/span&gt; with the same value, you could still get access to it via DOM methods.&amp;nbsp;They used to be interchangeable but they are not anymore.&lt;br /&gt;
&lt;br /&gt;
The HTML:&lt;br /&gt;
&lt;pre class=&quot;brush: html&quot;&gt;&amp;lt;input type=&quot;text&quot; name=&quot;firstname&quot; value=&quot;my value&quot;&amp;gt;
&lt;/pre&gt;The JavaScript:&lt;br /&gt;
&lt;pre class=&quot;brush: js&quot;&gt;var input = document.getElementById(&#39;firstname&#39;);
input.value = &#39;another value&#39;; // sets a value to &quot;firstname&quot; field
&lt;/pre&gt;&lt;br /&gt;
&lt;hr /&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: Georgia, &#39;Times New Roman&#39;, serif; font-size: large;&quot;&gt;document.all&lt;/span&gt;&lt;br /&gt;
Microsoft came up with this as a type of super collection that contained any tag that had a &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;name&lt;/span&gt; or &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;id&lt;/span&gt;. Since this is&amp;nbsp;propriety&amp;nbsp;thing and not cross-browser, it is best to not use it.&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;&lt;br /&gt;
Some of the topics that I did not cover in this post were browser history, how a browser works and document tree structure. If you&#39;re interested, click the link below to watch the video.&lt;br /&gt;
&lt;br /&gt;
Thanks for reading and let me know your comments.&lt;br /&gt;
&lt;br /&gt;
Source: &lt;a href=&quot;http://goo.gl/KVh23&quot;&gt;Douglas Crockford: &quot;Theory of the DOM&quot; (1 of 3)&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://alebelcor.blogspot.com/feeds/6018272455991949003/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://alebelcor.blogspot.com/2011/02/dom-theory-part-1.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/6018272455991949003'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6442308477719289110/posts/default/6018272455991949003'/><link rel='alternate' type='text/html' href='http://alebelcor.blogspot.com/2011/02/dom-theory-part-1.html' title='The script tag and the DOM'/><author><name>Alejandro Beltran</name><uri>http://www.blogger.com/profile/13533114582122558035</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='30' src='http://2.bp.blogspot.com/_RTIp6dEXzUs/TU3aPr0kdtI/AAAAAAAAAAU/AFDiZQxwOwg/s220/1.PNG'/></author><thr:total>0</thr:total></entry></feed>