<?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>sdm networks weblog &#187; javascript</title>
	<atom:link href="http://sdm-net.org/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://sdm-net.org</link>
	<description>The web and other things</description>
	<lastBuildDate>Mon, 21 Jun 2010 08:20:59 +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>Take care of your images</title>
		<link>http://sdm-net.org/2008/11/take-care-of-your-images/</link>
		<comments>http://sdm-net.org/2008/11/take-care-of-your-images/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 10:12:07 +0000</pubDate>
		<dc:creator>René Samselnig</dc:creator>
				<category><![CDATA[About]]></category>
		<category><![CDATA[dev]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://sdm-net.org/?p=277</guid>
		<description><![CDATA[<p>You might have heard of the double post issue, which causes form data to be sent a second time to the server. This can be resolved by a HTTP redirect after the post has been processed. Today another problem occured to me, one that reminded me of that issue. I prepared an IMG tag to [...]</p>
]]></description>
			<content:encoded><![CDATA[<p>You might have heard of the double post issue, which causes form data to be sent a second time to the server. This can be resolved by a HTTP redirect after the post has been processed. Today another problem occured to me, one that reminded me of that issue.<span id="more-277"></span></p>

<p>I prepared an IMG tag to display a specified image URL by setting the src attribute using JavaScript. The src attribute of that image has been left empty for that purpose (see code below).</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;imgWrapper&quot; style=&quot;display: none;&quot;&gt;
  &lt;img id=&quot;imgEl&quot; src=&quot;&quot; alt=&quot;Empty src attribute!&quot; /&gt;
&lt;/div&gt;</pre></td></tr></table></div>



<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> showImage<span style="color: #009900;">&#40;</span>src<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'imgEl'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> src;
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'imgWrapper'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>Later I discovered by using <a href="http://www.getfirebug.org">Firebug</a> that an additional request is being sent to the server. That request used the same path and query string as the original page request used. That way two tickets were added to the shopping cart instead of one. What was keeping me from discovering that issue earlier was that the first response from the server didn&#8217;t know yet about the second ticket &#8211; so the browser displayed only one ticket in the shopping cart. After reloading the second ticket showed up.</p>

<p>Some JavaScript debugging and HTML commenting later I found out that this behaviour comes from the IMG element with its empty src attribute. Firefox uses the currently called URL for that image to get its content! Even though the wrapping DIV is set to not display it the browser loads that images content.</p>

<p>Happy about figuring out what caused the problem I rewrote the JavaScript mechanism for displaying that specific image. Using the excellent <a href="http://www.prototypeapi.org">Prototype Library</a> the code snippet would look something like this:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;imgWrapper&quot; style=&quot;display: none;&quot;&gt;
&lt;/div&gt;</pre></td></tr></table></div>



<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> showImage<span style="color: #009900;">&#40;</span>src<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> wrapper <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'imgWrapper'</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #003366; font-weight: bold;">var</span> img <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'img'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
  img.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> src;
  img.<span style="color: #660066;">alt</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'Don<span style="color: #000099; font-weight: bold;">\'</span>t use an empty src attribute!'</span>;
  wrapper.<span style="color: #660066;">update</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>;
  wrapper.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>img<span style="color: #009900;">&#41;</span>;
  wrapper.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://sdm-net.org/2008/11/take-care-of-your-images/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Evaluation with untyped languages</title>
		<link>http://sdm-net.org/2008/10/evaluation-with-untyped-languages/</link>
		<comments>http://sdm-net.org/2008/10/evaluation-with-untyped-languages/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 10:52:39 +0000</pubDate>
		<dc:creator>René Samselnig</dc:creator>
				<category><![CDATA[About]]></category>
		<category><![CDATA[dev]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://sdm-net.org/?p=215</guid>
		<description><![CDATA[<p>When using JavaScript or PHP you need to keep in mind that these aren&#8217;t typed languages. Typed languages give us a lot of restrictions whilest keeping us from doing stupid things. Untyped languages do not have those restrictions so they leave us with a lot of freedom. Do we need this kind of freedom? Problem [...]</p>
]]></description>
			<content:encoded><![CDATA[<p>When using JavaScript or PHP you need to keep in mind that these aren&#8217;t typed languages. Typed languages give us a lot of restrictions whilest keeping us from doing stupid things. Untyped languages do not have those restrictions so they leave us with a lot of freedom. Do we need this kind of freedom?
<span id="more-215"></span></p>

<h4>Problem</h4>

<p>A typed language defines types (such as integer, string, boolean, &#8230;) to variables &#8211; that way the contents of variables are clearly defined. When not having this restriction (as JavaScript doesn&#8217;t) one might do the following (JavaScript code):</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> alpha <span style="color: #339933;">=</span> <span style="color: #3366CC;">'This is some String'</span>;
alpha <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span>;</pre></td></tr></table></div>


<p>Most of the time this is OK, but there are times when you clearly need to know how values are evaluated. You might check for content if you need to do special things for special types. Let us assume you want to assign <code>alpha</code> a default value if it is empty (<code>''</code>). Your code might look like this:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>3
4
5
</pre></td><td class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>alpha <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    alpha <span style="color: #339933;">=</span> <span style="color: #3366CC;">'-'</span>;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>This is perfectly OK if <code>alpha</code> is a string.</p>

<p>Nevertheless, <code>alpha</code> could always be an integer, too. And strangely, when the value of <code>alpha</code> is <code>0</code> (zero) the boolean expression <code>alpha == ''</code> evaluates to <strong>true</strong>! So <code>alpha</code> would become <code>'-'</code> in our case, which is not exactly what we intended to do, right? This could lead to some serious errors in your programme flow.</p>

<h4>Solution</h4>

<p>What can you do about it? Is there anything you could do? Yes, there is. When operating with values like <code>''</code> (empty string), <code>true</code>/<code>false</code> (boolean contents of variable) and <code>0</code> (zero) you should always use the strict equivalence operators: <code>===</code> (equals to and same type) and <code>!==</code> (not equals to or not same type). The above code would then look something like this:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>3
4
5
</pre></td><td class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>alpha <span style="color: #339933;">===</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    alpha <span style="color: #339933;">=</span> <span style="color: #3366CC;">'-'</span>;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>That small change effects the semantics of the code. It now sets the default value if and only if the value of <code>alpha</code> is equals to <code>''</code> and the type of <code>alpha</code> is the same as the type of <code>''</code> (which is string, respectively). In fact, this is what you wanted to do, isn&#8217;t it?</p>

<p>If you use strict equivalence operators correctly then you can make sure the code does what you want it to do. Keep this in mind if you like writing correct code using untyped languages.</p>

<h4>See also</h4>

<p>Here are some useful links for JavaScript and PHP:</p>

<ul>
<li><a href="http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Comparison_Operators">JavaScript &#8211; Comparison Operators</a></li>
<li><a href="http://www.php.net/manual/en/language.expressions.php">PHP &#8211; Expressions</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://sdm-net.org/2008/10/evaluation-with-untyped-languages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
