<?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>Jonathan Weiß &#187; JavaScript</title>
	<atom:link href="http://mysterycity.de/blog/category/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://mysterycity.de/blog</link>
	<description>Online Marketing</description>
	<lastBuildDate>Tue, 01 Nov 2011 09:28:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Side-effect while extending the Array class</title>
		<link>http://mysterycity.de/blog/side-effect-while-extending-the-array-object</link>
		<comments>http://mysterycity.de/blog/side-effect-while-extending-the-array-object#comments</comments>
		<pubDate>Wed, 21 Nov 2007 16:28:21 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://mysterycity.de/blog/side-effect-while-extending-the-array-object</guid>
		<description><![CDATA[Let us assume, that you wrote some code to extend the Array class to a method. In this case we will add the min() method, which will return the lowest numeric value of all elements inside the array: Array.prototype.min = function min&#40;&#41;&#123; &#160; var returnValue = Number.MAX_VALUE; &#160; if&#40;this.length === 0&#41;&#123; return false; &#125; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Let us assume, that you wrote some code to extend the <code>Array</code> class to a method. In this case we will add the <code>min()</code> method, which will return the lowest numeric value of all elements inside the array:</p>
<pre class="javascript">Array.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">min</span> = <span style="color: #003366; font-weight: bold;">function</span> min<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> returnValue = Number.<span style="color: #006600;">MAX_VALUE</span>;
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">length</span> === <span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i = <span style="color: #CC0000;">0</span>; i &lt; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">length</span>; i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> &lt; returnValue<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
      returnValue = <span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">return</span> returnValue;
&nbsp;
<span style="color: #66cc66;">&#125;</span>;</pre>
<p>This methods work fine, but you will notice a strange behavior if you cycle through all array elements using a <code>for</code> loop construct.</p>
<pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> list = <span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">1</span>, <span style="color: #CC0000;">2</span>, <span style="color: #CC0000;">3</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> list<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span>list<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>The code will produce this output:</p>
<pre class="javascript"><span style="color: #CC0000;">1</span>
<span style="color: #CC0000;">2</span>
<span style="color: #CC0000;">3</span>
min<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></pre>
<p>It seems that the for loop also iterates over the recently added method.</p>
<p>The method can only be removed from the <code>Array</code> class, not from an instance of it:
<pre class="javascript"><span style="color: #000066; font-weight: bold;">delete</span> Array.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">min</span>;</pre>
<p> It can not be removed, but overwritten by assigning a value to it:
<pre class="javascript">list.<span style="color: #006600;">min</span> = <span style="color: #CC0000;">0</span>;</pre>
<p>So, if you still want to use the <code>for</code> loop to iterate over the entries, you have to check the type of each entry:</p>
<pre class="javascript"><span style="color: #000066; font-weight: bold;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> list<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #66cc66;">&#40;</span>list<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> != <span style="color: #3366CC;">'function'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span>list<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://mysterycity.de/blog/side-effect-while-extending-the-array-object/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Documenting private methods with JSDoc</title>
		<link>http://mysterycity.de/blog/documenting-private-methods-with-jsdoc</link>
		<comments>http://mysterycity.de/blog/documenting-private-methods-with-jsdoc#comments</comments>
		<pubDate>Sun, 04 Nov 2007 18:44:10 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[jsdoc]]></category>

		<guid isPermaLink="false">http://mysterycity.de/blog/documenting-private-methods-with-jsdoc</guid>
		<description><![CDATA[The best way to comment directly in the source code is using JSDoc, the JavaScript equivalent of JavaDoc. To comment on the code, just insert specially formatted comment lines (starting with /** instead of /*) containing a description text and tags before each class, attribute or method. As in JavaDoc, there a several tags to [...]]]></description>
			<content:encoded><![CDATA[<p>The best way to comment directly in the source code is using <a href="http://jsdoc.sourceforge.net/">JSDoc</a>, the JavaScript equivalent of JavaDoc.</p>
<p>To comment on the code, just insert specially formatted comment lines (starting with <code>/**</code> instead of <code>/*</code>) containing a description text and <em>tags</em> before each class, attribute or method. As in JavaDoc, there a several tags to describe the source code: <code>@param</code>, <code>@return</code>, <code>@version</code> and <a href="http://jsdoc.sourceforge.net/#tagref">many more</a>.<br />
If you have included this comments, you have to run a perl script over the javascript files which creates a bunch of HTML files containing the information from your sources.</p>
<p>But be careful if you are commenting on private methods! In this example there will be no comment for <code>_privateMethod()</code>:</p>
<pre class="javascript"><span style="color: #009900; font-style: italic;">/**
 * Description for this MyObject..
*/</span>
<span style="color: #003366; font-weight: bold;">function</span> MyObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">/**
 * @param {object} myElement Description for myElement
 * @private
*/</span>
  <span style="color: #003366; font-weight: bold;">var</span> _privateMethod = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>myElement<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    ;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">/**
 * @return {number} Answer to Life, the Universe, and Everything
*/</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">publicMethod</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">42</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre>
<p>JSDoc does not support this way of defining private methods. You have to use this scheme: </p>
<pre class="javascript">&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> MyObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">function</span> _privateMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    ;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://mysterycity.de/blog/documenting-private-methods-with-jsdoc/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reading custom HTML attributes</title>
		<link>http://mysterycity.de/blog/reading-custom-html-attributes</link>
		<comments>http://mysterycity.de/blog/reading-custom-html-attributes#comments</comments>
		<pubDate>Wed, 25 Jul 2007 19:06:15 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Internet Explorer]]></category>
		<category><![CDATA[Opera]]></category>
		<category><![CDATA[Safari]]></category>

		<guid isPermaLink="false">http://mysterycity.de/blog/reading-custom-html-attributes</guid>
		<description><![CDATA[Sometimes you need to store some extra information for an HTML element, which provides additional data to your scripts. The best way to store this information is to use it as an attribute value on the element itself. You could use a special CSS class name for a group of elements which should be handled [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you need to store some extra information for an HTML element, which provides additional data to your scripts. The best way to store this information is to use it as an attribute value on the element itself.<br />
You could use a special CSS class name for a group of elements which should be handled identically, but you might lose the flexibility of CSS if these elements have completely different styles. Another possibility is to use custom (proprietary) attributes. The <a href="http://dojotoolkit.org/">Dojo Toolkit</a> makes massively use of this method. Please note that your document will not be valid (X)HTML, if you do so. Anyway, this can be quite useful but you have to be careful how to set and read the attributes.</p>
<p>At runtime an HTML element is presented as a DOM element object. You can add attributes by assigning them to the object.</p>
<pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> myElement = document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'testDiv'</span><span style="color: #66cc66;">&#41;</span>;
myElement.<span style="color: #006600;">mood</span> = <span style="color: #3366CC;">'happy'</span>;</pre>
<p>What happens if you are dealing with custom attributes that are not assigned at runtime but on the sever side? You will notice two characteristics: at first, all attribute names are lowercased by the browser and secondly this will not work as you expect.</p>
<p>Imagine you have this HTML element:
<pre class="html4strict">&nbsp;
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;testDiv&quot;</span> foo=<span style="color: #ff0000;">&quot;bar&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
&nbsp;</pre>
<p> and this script:
<pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> myElement = document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'testDiv'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>myElement.<span style="color: #006600;">foo</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>This alert box will show <em>undefined</em>. It seems that only HTML attributes which are described in the HTML specification (such as <code>id</code>, <code>lang</code> or <code>title</code>) are applied as attributes to the DOM element object. Of course, there are exceptions for this rule: the Internet Explorer applies <strong>all</strong> attributes to the object.</p>
<p>I  made a comparision sheet showing which browser supports which behavior.<br />
<table style="width:100%;text-align:center;border:1px #D0D0D0 solid;margin-bottom:10px; margin-top:2px;padding:4px;" cellspacing="0" cellpadding="2" border="0">
<tr>
<td></td>
<td></td>
<td rowspan="2"><img src="http://mysterycity.de/blog/wp-content/uploads/icons/firefox.jpg" width="32" height="33"><br>Firefox 2</td>
<td rowspan="2"><img src="http://mysterycity.de/blog//wp-content/uploads/icons/opera.jpg" width="32" height="33"><br>Opera 9</td>
<td rowspan="2"><img src="http://mysterycity.de/blog//wp-content/uploads/icons/safari.jpg" width="32" height="33"><br> Safari 3</td>
<td rowspan="2"><img src="http://mysterycity.de/blog//wp-content/uploads/icons/internet_explorer.jpg" width="32" height="33"><br>IE 7</td>
</tr>
<tr>
<td style="white-space: nowrap;text-align:left;vertical-align:bottom;" colspan="2"> attribute set by assigning </td>
<td></td>
</tr>
<tr style="background-color:#EEEEEE;">
<td style="background-color:white;width:20px;"></td>
<td style="white-space: nowrap;text-align:left;"> element.<em>attributeName</em> </td>
<td> <strong style="color:#006600">yes</strong> </td>
<td> <strong style="color:#006600">yes</strong> </td>
<td> <strong style="color:#006600">yes</strong> </td>
<td> <strong style="color:#006600">yes</strong> </td>
</tr>
<tr style="background-color:#EEEEEE;">
<td style="background-color:white;"></td>
<td style="white-space: nowrap;text-align:left;"> element.getAttribute() </td>
<td> <em style="color:#D84E05;">null</em> </td>
<td> <em style="color:#D84E05;">null</em> </td>
<td> <em style="color:#D84E05;">null</em> </td>
<td> <strong style="color:#006600">yes</strong> </td>
</tr>
<tr>
<td style="white-space: nowrap;text-align:left;" colspan="2"> attribute set by setAttribute() </td>
<td colspan="4"></td>
</tr>
<tr style="background-color:#EEEEEE;">
<td style="background-color:white;"></td>
<td style="white-space: nowrap;text-align:left;"> element.<em>attributeName</em></code> </td>
<td> <em style="color:#D84E05;">undefined</em> </td>
<td> <em style="color:#D84E05;">undefined</em> </td>
<td> <em style="color:#D84E05;">undefined</em> </td>
<td> <strong style="color:#006600">yes</strong> </td>
</tr>
<tr style="background-color:#EEEEEE;">
<td style="background-color:white;"></td>
<td style="text-align:left;white-space: nowrap;"> element.getAttribute() </td>
<td> <strong style="color:#006600">yes</strong> </td>
<td> <strong style="color:#006600">yes</strong> </td>
<td> <strong style="color:#006600">yes</strong> </td>
<td> <strong style="color:#006600">yes</strong> </td>
</tr>
<tr>
<td style="text-align:left;" colspan="2"> attribute set on server side </td>
<td colspan="5"></td>
</tr>
<tr style="background-color:#EEEEEE;">
<td style="background-color:white;"></td>
<td style="text-align:left;white-space: nowrap;"> element.<em>attributeName</em> </td>
<td> <em style="color:#D84E05;">undefined</em> </td>
<td> <em style="color:#D84E05;">undefined</em> </td>
<td> <em style="color:#D84E05;">undefined</em> </td>
<td> <strong style="color:#006600">yes</strong> </td>
</tr>
<tr style="background-color:#EEEEEE;">
<td style="background-color:white;"></td>
<td style="text-align:left;white-space: nowrap;"> element.getAttribute() </td>
<td> <strong style="color:#006600">yes</strong> </td>
<td> <strong style="color:#006600">yes</strong> </td>
<td> <strong style="color:#006600">yes</strong> </td>
<td> <strong style="color:#006600">yes</strong> </td>
</tr>
</table>
<p>So, if you want to use custom attributes while having HTML elements which are created on runtime <strong>and</strong> on the server side, there is only one possibility: use <code>setAttribute()</code> and <code>getAttribute()</code>. This combination works in every case and in every browser.</p>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://mysterycity.de/blog/reading-custom-html-attributes/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to get the actual style value of an element</title>
		<link>http://mysterycity.de/blog/how-to-get-the-actual-style-value-of-an-element</link>
		<comments>http://mysterycity.de/blog/how-to-get-the-actual-style-value-of-an-element#comments</comments>
		<pubDate>Thu, 28 Jun 2007 07:00:47 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://mysterycity.de/blog/how-to-get-the-actual-style-value-of-an-element</guid>
		<description><![CDATA[Golo asked me some time ago how to retrieve the actual style property value of an element. It is easy to get the value applied by the element's style attribute or the CSS class name. But the actual value is affected by many CSS rules which are applied to the element: element type, element's parents, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.goloroden.de">Golo</a> asked me some time ago how to retrieve the actual style property value of an element. It is easy to get the value applied by the element's <code>style</code> attribute or the CSS class name. But the actual value is affected by many CSS rules which are applied to the element: element type, element's parents, class name(s) and style attribute (which can, of course, be modified by JavaScript through runtime).</p>
<p><a href='http://mysterycity.de/blog/wp-content/uploads/2007/06/css_rule_example.png' title='CSS rules' rel="lightbox[13]"><img src='http://mysterycity.de/blog/wp-content/uploads/2007/06/css_rule_example.thumbnail.png' alt='CSS rules' /></a><br />
<a href='http://mysterycity.de/blog/wp-content/uploads/2007/06/css_rule_example.png' title='CSS rules'>An example of different CSS rules overwriting each other.</a></p>
<p>Luckily we do not have to worry about this &mdash; it is the browser's job to apply all rules correctly. There are two ways of retrieving the actual style information. Those on the dark side use <code>element.currentStyle[style property]</code> to get the actual property value. All others use the WC3 method <code>window.getComputedStyle()</code> to get a <a href="http://webkit.org/docs/a00018.html">CSSStyleDecoration object</a> which can be accesed via <code>getPropertyValue(style property)</code>.</p>
<p>Note, that the proprietary <code>currentStyle()</code> method has the same syntax as in your CSS files (e.g. <code>background-color</code>) while W3C DOM method uses the syntax you know from JavaScript (e.g. <code>backgroundColor</code>).</p>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://mysterycity.de/blog/how-to-get-the-actual-style-value-of-an-element/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

