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

<channel>
	<title>DaveOnCode &#187; javascript</title>
	<atom:link href="http://www.daveoncode.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveoncode.com</link>
	<description>coder and technology lover</description>
	<lastBuildDate>Sun, 16 May 2010 16:55:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Extending Eclipse using JavaScript and Monkey Script engine</title>
		<link>http://www.daveoncode.com/2009/12/16/extending-eclipse-using-javascript-monkey-script-engine/</link>
		<comments>http://www.daveoncode.com/2009/12/16/extending-eclipse-using-javascript-monkey-script-engine/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 16:20:50 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[extensions]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=555</guid>
		<description><![CDATA[I was wondering how to wrap a string with quotes in Eclipse by using a shortcut, then I realized that there is not such command, so I started thinking for a solution and initially I created an Aptana&#8217;s snippet, but I was not satisfied, because I want to have an handy shortcut to invoke my]]></description>
			<content:encoded><![CDATA[<p>I was wondering how to wrap a string with quotes in Eclipse by using a shortcut, then I realized that there is not such command, so I started thinking for a solution and initially I created an Aptana&#8217;s snippet, but I was not satisfied, because I want to have an handy shortcut to invoke my snippet. By googling, I discovered Eclipse Monkey Script engine, that is an extremely powerful tool for every JavaScript developer who wants to extend Eclipse features by writing few lines of JavaScript code. In order to use the js engine, you must have the package org.eclipse.eclipsemonkey (which is installed by default by Aptana) installed. Unfortunately I can&#8217;t find a complete and exhaustive reference for this project, which seems forgotten by authors, so I learnt what I know reading different posts.<br />
Basically, Monkey Script allow developers to access editor&#8217;s instance, get selected test, get document content, edit it and update it. Moreover is possible to print text to consol, read and create files on the filesystem&#8230; and finally the scripts created will be accessible from Eclipse menu (under &#8220;Scripts&#8221;) and invokable through user defined shortcut&#8230; really nice!<br />
So, backing to my experiment, I created this monkey script:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/* <br />
* Key: M1+M2+C<br />
* Menu: Custom Scripts &gt; Wrap with double quotes<br />
* DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript<br />
* Kudos: Davide Zanotti<br />
*/</span><br />
<span style="color: #003366; font-weight: bold;">function</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// no editor... exit</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> editors.<span style="color: #660066;">activeEditor</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;No active editor&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// get a reference to the editor in use</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> editor <span style="color: #339933;">=</span> editors.<span style="color: #660066;">activeEditor</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// beginning of text selection</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> startOffset <span style="color: #339933;">=</span> editor.<span style="color: #660066;">selectionRange</span>.<span style="color: #660066;">startingOffset</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// end of text selection</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> endOffset <span style="color: #339933;">=</span> editor.<span style="color: #660066;">selectionRange</span>.<span style="color: #660066;">endingOffset</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// selected text</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> selection <span style="color: #339933;">=</span> editor.<span style="color: #660066;">source</span>.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>startOffset<span style="color: #339933;">,</span> endOffset<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// surround selection with quotes</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;editor.<span style="color: #660066;">applyEdit</span><span style="color: #009900;">&#40;</span>startOffset<span style="color: #339933;">,</span> endOffset <span style="color: #339933;">-</span> startOffset<span style="color: #339933;">,</span> <span style="color: #3366CC;">'&quot;'</span> <span style="color: #339933;">+</span> selection <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Error &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #660066;">code</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #660066;">message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>It&#8217;s important to notice some points, first you MUST write an &#8220;header&#8221; using the comment syntax &#8220;/* */&#8221; into which you MUST declare at least 2 parameter: the &#8220;<strong>Menu</strong>&#8221; path (you can nest several menu items by writing several &#8220;&gt;&#8221;) and the &#8220;<strong>DOM</strong>&#8221; package (which allows you to access specific objects like &#8220;<strong>editors</strong>&#8220;). You can write several js functions, but you must provide a <strong>main()</strong> function, which is called automatically by Eclipse once you launch your script, otherwise you can call the main function as you like but you must then provide the &#8220;OnLoad&#8221; param in the header, specifying which function will be called.<br />
To define a shortcut you can use the &#8220;<strong>Key</strong>&#8221; parameter and define your own keys combination by choosing among the four modifiers: M1, M2, M3, M4, that are a platform-independent way of representing keys (these stand for ALT, COMMAND, CTRL, and SHIFT). The strange (at least to me) parameter &#8220;<strong>Kudos</strong>&#8221; is used to declare the author of the monkey script.<br />
To test my little script or create your own, you have simply to create a new project into Eclipse by calling it as you like (ie: &#8220;custom-extensions&#8221;), then create a &#8220;scripts&#8221; folder into which you will save your .js files&#8230; that&#8217;s all, try yourself and enjoy :)</p>
<p>This is how it looks:</p>
<div id="attachment_558" class="wp-caption alignnone" style="width: 519px"><img src="http://www.daveoncode.com/wp-content/uploads/2009/12/Screen-shot-2009-12-16-at-17.08.56.jpg" alt="eclipse monkey script menu" title="eclipse-monkey-script-menu" width="509" height="254" class="size-full wp-image-558" /><p class="wp-caption-text">eclipse monkey script menu</p></div>
<p>ps: I&#8217;m going to explore the Monkey Script API in order to write more complex and useful extensions :P</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/12/16/extending-eclipse-using-javascript-monkey-script-engine/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>.cjs (Compiled JavaScript): an idea for a new file extension</title>
		<link>http://www.daveoncode.com/2009/12/10/cjs-compiled-javascript-file-extension-idea/</link>
		<comments>http://www.daveoncode.com/2009/12/10/cjs-compiled-javascript-file-extension-idea/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 16:54:59 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=547</guid>
		<description><![CDATA[Due to the growing diffusion of tools to compress and optimize JavaScript files (such Google Closure), I feel the need of an introduction of a standard way to identify and differentiate them (compiled files) from other uncompressed files. Basically, I would adopt a different file extension like CJS (which stands for compiled/compressed JavaScript). In this]]></description>
			<content:encoded><![CDATA[<p>Due to the growing diffusion of tools to compress and optimize JavaScript files (such Google Closure), I feel the need of an introduction of a standard way to identify and differentiate them (compiled files) from other uncompressed files. Basically, I would adopt a different file extension like <strong>CJS</strong> (which stands for compiled/compressed JavaScript). In this way it would be easy to browse a directory of hundreds of js files and identify those which are ready to be used in our project because already compressed and optimized.<br />
What do you think about?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/12/10/cjs-compiled-javascript-file-extension-idea/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Find outermost top level XML/HTML tags with regular expressions</title>
		<link>http://www.daveoncode.com/2009/09/10/top-level-html-xml-tags-regex-regular-expressions/</link>
		<comments>http://www.daveoncode.com/2009/09/10/top-level-html-xml-tags-regex-regular-expressions/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 14:58:31 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=484</guid>
		<description><![CDATA[I&#8217;m working on a personal big project (which I&#8217;m going to release soon) and in this project I need to parse strings containing XHTML tags with the goal of extract the top level of a given tag name, ie. from: 123456789&#60;onetag id=&#34;t1&#34;&#62; &#160; &#160; &#60;onetag id=&#34;t1-1&#34;&#62;&#60;/onetag&#62; &#160; &#160; &#60;onetag id=&#34;t1-2&#34;&#62;&#60;/onetag&#62; &#60;/onetag&#62; &#60;onetag id=&#34;t2&#34;&#62;&#60;/onetag&#62; &#60;onetag id=&#34;t3&#34;&#62;&#60;/onetag&#62;]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a personal big project (which I&#8217;m going to release soon) and in this project I need to parse strings containing XHTML tags with the goal of extract the top level of a given tag name, ie. from:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t1&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t1-1&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t1-2&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
<span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t2&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
<span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t3&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
<span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t4&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t4-1&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span></div></td></tr></tbody></table></div>
<p>I have to get 4 tags (t1, t2, t3, t4 with t1 and t4 containing their child nodes).<br />
My regex knowledge is unfortunately very basic, so I googled for a ready to use regex, but none satisfied my need&#8230; all the examples I found didn&#8217;t handle properly nested tags&#8230; so, after some hours of testing I realized my own regex (my first real one), the result is the following:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> pattern <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/&lt;(onetag)[^&lt;&gt;]*&gt;(&lt;\1[^&lt;&gt;]*&gt;&lt;\/\1&gt;)*&lt;\/\1&gt;/gi</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>In my case I&#8217;m using that pattern in Javascript, but I think it can be used with any language, because it doesn&#8217;t make use of advanced features like &#8220;atomic grouping&#8221; and these kind of &#8220;black magics&#8221;. To match the desired tag you can use it by replacing &#8220;onetag&#8221; with the tag you are looking for (even a tag with a namespace like &#8220;&lt;foo:mytag&gt;&#8221;).</p>
<p>EDIT:</p>
<p>The pattern above will work only if applied to a single line string (ie: var myString = &#8220;&lt;onetag id=&#8217;t1&#8242;&gt;&#8230;&#8221;), if you use that pattern on a &#8220;complex string&#8221; (a string containing spaces and new lines) it won&#8217;t works properly. Fortunately you can remove &#8220;bad characters&#8221; before by using a simple replace:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> parsedString <span style="color: #339933;">=</span> originalString.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\s(?!\w)/gi</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span>pattern<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p><strong>\s(?!\w)</strong> will match any space and new line not followed by an alphanumeric characters (in this way spaces between tag attributes won&#8217;t be removed)</p>
<p>EDIT 2:</p>
<p>The pattern <strong>/<(onetag)[^<>]*>(<\1[^<>]*><\/\1>)*<\/\1>/gi</strong> won&#8217;t works properly in presence of several type of nested tags, ie:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t1&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;anothertag&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t1-1&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t1-2&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>anothertag&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
<span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t2&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
<span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t3&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
<span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t4&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;anothertag&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t4-1&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>anothertag&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span></div></td></tr></tbody></table></div>
<p>The updated pattern is the following:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> newP <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/&lt;(onetag)[^&lt;&gt;]*&gt;.*?(&lt;\1[^&lt;&gt;]*&gt;.*?&lt;\/\1&gt;)*.*?&lt;\/\1&gt;/gi</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>I hope this will works without further modifications :P</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/09/10/top-level-html-xml-tags-regex-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adapting iframe height to its content with 2 lines of javascript</title>
		<link>http://www.daveoncode.com/2009/06/12/adapting-iframe-height-to-its-content-with-2-lines-of-javascript/</link>
		<comments>http://www.daveoncode.com/2009/06/12/adapting-iframe-height-to-its-content-with-2-lines-of-javascript/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 09:38:06 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[html]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=435</guid>
		<description><![CDATA[In a project, I have to use an iframe and I need that its height is automatically calculated based on its content. However is not possible to accomplish this by simply setting the attribute height to 100%, instead we must smartly use javascript to dynamically assign that value after a computation. The solution I&#8217;m going]]></description>
			<content:encoded><![CDATA[<p>In a project, I have to use an iframe and I need that its height is automatically calculated based on its content. However is not possible to accomplish this by simply setting the attribute height to 100%, instead we must smartly use javascript to dynamically assign that value after a computation. The solution I&#8217;m going to show you is not 100% my work but instead an optimization and a better implementation of clever intuitions I found on the web.<br />
The &#8220;trick&#8221; works in this way:</p>
<p>1. The iframe should has an ID assigned:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">iframe</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;slideshow_frame&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;frame.html&quot;</span> <span style="color: #000066;">frameborder</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">marginheight</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">marginwidth</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">scrolling</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;no&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">iframe</span>&gt;</span></div></td></tr></tbody></table></div>
<p>2. the page loaded into the iframe should contains all the html into an &#8220;easy retrievable&#8221; container (a div with an ID is perfect!):</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;content&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background: red; width: 400px; height: 120px&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background: green; width: 400px; height: 300px&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background: yellow; width: 400px; height: 60px&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></div></td></tr></tbody></table></div>
<p>3. on the iframe&#8217;s page &#8220;onload&#8221; event, a function will be executed to adjust its height:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">function resizeIframe(iframeID) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; var iframe = window.parent.document.getElementById(iframeID);<br />
&nbsp; &nbsp; var container = document.getElementById(&quot;content&quot;);<br />
<br />
&nbsp; &nbsp; iframe.style.height = container.offsetHeight + &quot;px&quot;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
}</div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/06/12/adapting-iframe-height-to-its-content-with-2-lines-of-javascript/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Jetpack: building Firefox plugin with Javascript (and jQuery)</title>
		<link>http://www.daveoncode.com/2009/05/29/jetpack-firefox-plugin-with-javascript-jquery/</link>
		<comments>http://www.daveoncode.com/2009/05/29/jetpack-firefox-plugin-with-javascript-jquery/#comments</comments>
		<pubDate>Fri, 29 May 2009 15:08:00 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[browsers]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=427</guid>
		<description><![CDATA[I discovered a couple of days ago a new open source project from Mozilla labs: Jetpack. It&#8217;s an &#8220;API set&#8221; which allows to build Firefox&#8217;s plugins by using Javascript, HTML (including new HTML 5 tags: &#8220;video&#8221; and &#8220;audio&#8220;) and CSS (oh yes, in a single word Ajax). Jetpack is still in an early development phase]]></description>
			<content:encoded><![CDATA[<p>I discovered a couple of days ago a new open source project from Mozilla labs: <a href="https://jetpack.mozillalabs.com">Jetpack</a>.<br />
It&#8217;s an &#8220;API set&#8221; which allows to build Firefox&#8217;s plugins by using Javascript, HTML (including new HTML 5 tags: &#8220;<strong>video</strong>&#8221; and &#8220;<strong>audio</strong>&#8220;) and CSS (oh yes, in a single word Ajax).<br />
Jetpack is still in an early development phase and released as 0.1 version.<br />
<a href="https://jetpack.mozillalabs.com/api.html">Here</a> you can find the (really brief) api documentation and <a href="https://wiki.mozilla.org/Labs/Jetpack/In_The_Wild">here</a> some examples.<br />
I would like to try it in the future</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/05/29/jetpack-firefox-plugin-with-javascript-jquery/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>jQuery is not a magic&#8230; if you think so, you shouldn&#8217;t use it!</title>
		<link>http://www.daveoncode.com/2009/04/28/jquery-is-not-a-magic-if-you-think-so-you-shouldnt-use-it/</link>
		<comments>http://www.daveoncode.com/2009/04/28/jquery-is-not-a-magic-if-you-think-so-you-shouldnt-use-it/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 15:15:24 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=387</guid>
		<description><![CDATA[I decided to write this post, because I revised a Javascript code realized by a colleague and it was simply terrifying! It was bad formatted, it had HTML style comments (&#60;!- &#8211; - -&#62;) with insignificant or unclear content, missing var declarations and so on. What I would like to say is that, in order]]></description>
			<content:encoded><![CDATA[<p>I decided to write this post, because I revised a Javascript code realized by a colleague and it was simply terrifying!<br />
It was bad formatted, it had HTML style comments (&lt;!- &#8211; - -&gt;) with insignificant or unclear content, missing var declarations and so on.<br />
What I would like to say is that, in order to use jQuery and similar js libraries a mere HTML knowledge is not enough, Javascript is not hard to understand and use as Java, C++ or other object oriented programming languages are, but several things must be clear before to write some code. If you want to use in a clever and productive way such libraries you should at least:</p>
<ul>
<li>Understand the difference and behavior of String, Number, Array, Date and so on</li>
<li>Have a deep understanding of what DOM is and how it works (you should at least know these methods: getElementbyId(), getElementsByTagName(), createElement(), appendChild(), removeChild() and innerHTML property)</li>
<li>Know what objects and constructors are and how they behave</li>
<li>Know what a function is and how to create one</li>
<li>Know events, event listeners and event handlers</li>
<li>Understand loops</li>
</ul>
<p>Too much stuff to learn? It&#8217;s the minimum to know, there are then prototyping, JSON, closures, XMLHttpRequest and so on&#8230;<br />
Javascript is a programming language (not a tool for designers), don&#8217;t believe that a call to $.doMagic() will resolve your problems, you have to understand what is behind that doMagic() (at least the foundations) in order to realize concrete stuff&#8230; otherwise you will only write &#8220;magic spaghetti code&#8221;!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/04/28/jquery-is-not-a-magic-if-you-think-so-you-shouldnt-use-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using jQuery to load an XML configuration file</title>
		<link>http://www.daveoncode.com/2009/03/11/using-jquery-to-load-an-xml-configuration-file/</link>
		<comments>http://www.daveoncode.com/2009/03/11/using-jquery-to-load-an-xml-configuration-file/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 16:36:40 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[config]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=346</guid>
		<description><![CDATA[At work I&#8217;m integrating Viamichelin&#8217;s map api on a site and I realized a Javascript class which works as a proxy to Viamichelin&#8217;s services. The api provide several methods to customize appearance of map&#8217;s controls and ui components (colors, dimensions, opacity and so on) but following the api&#8217;s documentation and examples this means to have]]></description>
			<content:encoded><![CDATA[<p>At work I&#8217;m integrating Viamichelin&#8217;s map api on a site and I realized a Javascript class which works as a proxy to Viamichelin&#8217;s services.<br />
The api provide several methods to customize appearance of map&#8217;s controls and ui components (colors, dimensions, opacity and so on) but following the api&#8217;s documentation and examples this means to have several &#8220;hard coded&#8221; settings inside my code, which is an issue I want to avoid, so I thought to use an external XML configuration file and then call the methods using parametric values (which are retrieved by reading the file). Is pretty simple with ajax to load a text file, the only problem is that by default (or better, by nature) ajax is asynchronous and this can be an obstacle but fortunately with jQuery (but even with other libraries and personal XMLHttpRequest implementation) we can make synchronous calls by specifying it. </p>
<p><span id="more-346"></span><br />
In jQuery my ajax call looks is the following:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;viamichelin_proxy_config.xml&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;xml&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; cache<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; async<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>xml<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// process xml returned</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; error<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// handle error</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>The code above executes a synchronous (async: false) ajax call and returns an XML document object, inside &#8220;success&#8221; function&#8217;s body I pass all my desired parameters to my class constructor, in this way:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ViaMichelinMapProxy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; apiKey<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;api-key&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; language<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;language&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; genericErrorMsg<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;messages.generic-error-message&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; noResultMsg<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;messages.no-result-message&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; noDepartureMsg<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;messages.no-departure-result-message&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; noDestinationMsg<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;messages.no-destination-result-message&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; defaultZoom<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;default-zoom-level&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; highlightColor<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;route-highlight-color&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; highlightSize<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;route-highlight-size&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; highlightOpacity<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;route-highlight-opacity&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; canvasId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.map-canvas&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; routeSheetId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.route-sheet&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; departurePointId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.departure-point&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; destinationPointId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.destination-point&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; totalCostId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.total-cost&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; tollCostId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.toll-cost&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; petrolCostId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.petrol-cost&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; totalTimeId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.total-time&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; motorwayTimeId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.motorway-time&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; totalDistanceId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.total-distance&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; motorwayDistanceId<span style="color: #339933;">:</span> getConfig<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;containers.motorway-distance&quot;</span><span style="color: #339933;">,</span> xml<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>getConfig() is an utility function which, by using standard DOM functions (like getElementsByTagName()), reads and returns the required node value. Once the parameters are well defined, inside my class the parametric methods will work fine, and my colleagues won&#8217;t have to dig into my code in future to change a desired value, they only will need to open the xml file and quickly change an xml node value. The methods inside the class are so defined in this way:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// clean and flexible code, no hardcoded settings ^_^</span><br />
getItiAsVMComplexLayer<span style="color: #009900;">&#40;</span>_highlightColor<span style="color: #339933;">,</span> _highlightSize<span style="color: #339933;">,</span> _highlightOpacity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>My only doubt was &#8220;how much time it will takes to read and process an xml file and its several nodes?&#8221;&#8230; and the good answer was: only 0.05 seconds!! I&#8217;m very satisfied of my code implementation :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/03/11/using-jquery-to-load-an-xml-configuration-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Object oriented programming in Javascript</title>
		<link>http://www.daveoncode.com/2009/03/10/object-oriented-programming-in-javascript/</link>
		<comments>http://www.daveoncode.com/2009/03/10/object-oriented-programming-in-javascript/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 16:53:36 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[constructor]]></category>
		<category><![CDATA[object oriented]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=326</guid>
		<description><![CDATA[After several approaches, I finally found an elegant and functional solution to use Javascript in an OOP way, without use third party libraries or use particular tricks. The objectives I had in mind was: Realize a Java/Actionscript-like class Provide a constructor for the class Implementing public method Implementing private method Ensure encapsulation (class properties can&#8217;t]]></description>
			<content:encoded><![CDATA[<p>After several approaches, I finally found an elegant and functional solution to use Javascript in an OOP way, without use third party libraries or use particular tricks.<br />
The objectives I had in mind was:</p>
<ol>
<li>Realize a Java/Actionscript-like class</li>
<li>Provide a constructor for the class</li>
<li>Implementing public method</li>
<li>Implementing private method</li>
<li>Ensure encapsulation (class properties can&#8217;t be changed by assigning a value to the relative instance&#8217;s property)</li>
</ol>
<p><span id="more-326"></span><br />
Honestly, I don&#8217;t know if this approach has some weakness point nor if is been already adopted by someone, but I&#8217;m pretty satisfied with it and I want to share the technique behind.</p>
<p>My Javascript classes looks like this:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> MyClass<span style="color: #009900;">&#40;</span>arg1<span style="color: #339933;">,</span> arg2<span style="color: #339933;">,</span> arg3<span style="color: #339933;">,</span> arg4<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Private class properties declaration</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> _propertyName1<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_propertyName2<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_propertyName3<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_propertyName4<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Constructor</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> MyClass <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// assign values to properties</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _propertyName1 <span style="color: #339933;">=</span> arg1<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _propertyName2 <span style="color: #339933;">=</span> arg2<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _propertyName3 <span style="color: #339933;">=</span> arg3<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _propertyName4 <span style="color: #339933;">=</span> arg4<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// do eventually initialization stuff...</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Private method (can be used only inside MyClass)</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> privateMethod1 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// do something helpful...</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Public setter method</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setProperty1</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; _propertyName1 <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Public getter method</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getProperty1</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> _propertyName1<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Calls the constructor automatically</span><br />
&nbsp; &nbsp; MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>In this way when I will initialize an instance of &#8220;MyClass&#8221; (var mc = new MyClass()), the constructor will be automatically called and &#8220;mc&#8221; will have access only to setProperty1() and getProperty1() methods, because they have a &#8220;this&#8221; scope and not a &#8220;var&#8221; declaration, other stuff with &#8220;var&#8221; are so obscured to the instance. Anyway nothing can&#8217;t prevent the user to set property directly to the instance (mc._propertyName1 = value), but in that way the user is not setting the class property, rather is defining a new property on the instance, my desired property is safely encapsulated in the class and if the user wants to change its value he must use the provided setter method. For that concerning to the constructor (var MyClass), it would seems a bit &#8220;stupid&#8221; because the function &#8220;MyClass&#8221; is a constructor its self, anyway i find that this help me to maintain my js class in order and more readable.</p>
<p>Safety of encapsulation can be tested in this way:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> mc <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
mc.<span style="color: #660066;">setProperty1</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;setter value assignment&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
mc._propertyName1 <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;direct value assignment&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>mc.<span style="color: #660066;">getProperty1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>The alert will show &#8220;assignment&#8221;, mc._propertyName1 = &#8220;direct value assignment&#8221; hasn&#8217;t modified the class property!<br />
Of course if we alert &#8220;mc._propertyName1&#8243; we will get &#8220;direct value assignment&#8221;, but that&#8217;s another story&#8230; if we use properly getter/setter methods we can ensure a working encapsulation.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/03/10/object-oriented-programming-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>parseInt(): difference between Javascript and Actionscript</title>
		<link>http://www.daveoncode.com/2009/03/06/parseint-difference-between-javascript-and-actionscript/</link>
		<comments>http://www.daveoncode.com/2009/03/06/parseint-difference-between-javascript-and-actionscript/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 15:27:06 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=334</guid>
		<description><![CDATA[Today is the day of the little big discoveries. I realized that the parseInt() function, which objective is the same in both languages (Actionscript and Javascript), is a bit different between them. In Actionscript the function is based on base 10 numeration, in Javascript instead the funny thing is that it try to guess the]]></description>
			<content:encoded><![CDATA[<p>Today is the day of the little big discoveries. I realized that the parseInt() function, which objective is the same in both languages (Actionscript and Javascript), is a bit different between them. In Actionscript the function is based on base 10 numeration, in Javascript instead the funny thing is that it try to guess the radix by analyzing the string received as first argument, so if you want to be sure of the result returned you have to explicitly pass the radix in the second argument.</p>
<p>Example:</p>
<pre>var myString = "000123";
alert(parseInt(myString));</pre>
<p>The code above will alert <strong>83</strong> instead of <strong>123</strong> (because the default base guessed by Javascript isn&#8217;t 10 like in Actionscript), to get <strong>123</strong> the code must be:</p>
<pre>var myString = "000123";
alert(parseInt(myString, 10));</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/03/06/parseint-difference-between-javascript-and-actionscript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using getAttribute() to retrieve the label&#8217;s &quot;for&quot; attribute returns NULL on internet explorer!</title>
		<link>http://www.daveoncode.com/2008/12/12/using-getattribute-to-retrieve-the-labels-for-attribute-returns-null-on-internet-explorer/</link>
		<comments>http://www.daveoncode.com/2008/12/12/using-getattribute-to-retrieve-the-labels-for-attribute-returns-null-on-internet-explorer/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 15:40:47 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[browsers]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[browser incopatibility]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[crossbrowser]]></category>
		<category><![CDATA[ie]]></category>
		<category><![CDATA[internet explorer]]></category>

		<guid isPermaLink="false">http://daveoncode.wordpress.com/?p=136</guid>
		<description><![CDATA[Damned Internet Explorer!!! I&#8217;ve just faced another browser issue by using the javascript function getAttribute() in order to retrieve the &#8220;for&#8221; value of a form&#8217;s label, ie: myLabel.getAttribute("for"); (where &#8220;myLabel&#8221; is a reference to a &#60;label&#62; node) The function always returns NULL, even if the &#8220;for&#8221; attribute is manually specified into HTML code, ie: &#60;label]]></description>
			<content:encoded><![CDATA[<p>Damned Internet Explorer!!! I&#8217;ve just faced another browser issue by using the javascript function <em><strong>getAttribute()</strong></em> in order to retrieve the &#8220;for&#8221; value of a form&#8217;s label, ie:</p>
<pre>
myLabel.getAttribute("for");
</pre>
<p>(where &#8220;myLabel&#8221; is a reference to a &lt;label&gt; node)<br />
The function always returns NULL, even if the &#8220;for&#8221; attribute is manually specified into HTML code, ie:</p>
<pre>
&lt;label for="my_field_id"&gt;my label&lt;/label&gt;
</pre>
<p>Fortunately, after a brief search on Google, I&#8217;ve found a post on quirksmode.org, which contains a comment by Tino Zijdel, that show an easy and crossbrowser solution to get the &#8220;for&#8221; attribute (that can be used both to set a value or to get it):</p>
<pre>
myLabel.htmlFor
</pre>
<p>Thank you Tino!</p>
<p>In my search I&#8217;d even discovered that on Internet Explorer <strong><em>getAttribute()</em></strong> is different from other browsers, because it offers an extra argument called &#8220;iFlags&#8221; that is a number that can be 0, 1 or 2 (genial!) and means (I&#8217;m reporting the official microsoft documentation):</p>
<p>0 -&gt; Default. Performs a property search that is not case-sensitive, and returns an interpolated value if the property is found<br />
1 -&gt; Performs a case-sensitive property search. To find a match, the uppercase and lowercase letters in AttributeName  must exactly match those in the attribute name. If the iFlags  parameter for getAttribute is set to 1 and this option is set to 0 (default), the specified property name might not be found.<br />
2 -&gt; Returns the value exactly as it was set in script or in the source document</p>
<p>It would be a better world without Internet Explorer!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2008/12/12/using-getattribute-to-retrieve-the-labels-for-attribute-returns-null-on-internet-explorer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
