<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Haveacafe&#039;s Weblog &#187; new Function</title>
	<atom:link href="http://haveacafe.wordpress.com/tag/new-function/feed/" rel="self" type="application/rss+xml" />
	<link>http://haveacafe.wordpress.com</link>
	<description>Java, unix, web and other intersting tricks !</description>
	<lastBuildDate>Wed, 17 Feb 2010 16:12:44 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>fr</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='haveacafe.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/ec698bdf3bb36e6d0e5d3b1af4c9422d?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>Haveacafe&#039;s Weblog &#187; new Function</title>
		<link>http://haveacafe.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://haveacafe.wordpress.com/osd.xml" title="Haveacafe&#039;s Weblog" />
	<atom:link rel='hub' href='http://haveacafe.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Dealing with Javascript variable scope and for-loop or external function.</title>
		<link>http://haveacafe.wordpress.com/2008/08/26/dealing-with-javascript-variable-scope-and-for-loop-or-external-function/</link>
		<comments>http://haveacafe.wordpress.com/2008/08/26/dealing-with-javascript-variable-scope-and-for-loop-or-external-function/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 12:44:34 +0000</pubDate>
		<dc:creator>haveacafe</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[english publications]]></category>
		<category><![CDATA[ECMAScript]]></category>
		<category><![CDATA[new Function]]></category>

		<guid isPermaLink="false">http://haveacafe.wordpress.com/?p=8</guid>
		<description><![CDATA[Sometimes, you need to set some HTML attributes automatically with a batch declaration.
By example imagine you want to set an mouseover over hundred of &#60;a&#62; links, in order to display their title content in an alert box. How to proceed ?
You could try to achieve this using a loop. Something like:

&#60;html&#62;
&#60;a id="0" href="./0" title="the first"&#62; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haveacafe.wordpress.com&blog=2198367&post=8&subd=haveacafe&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Sometimes, you need to set some HTML attributes automatically with a batch declaration.<br />
By example imagine you want to set an mouseover over hundred of &lt;a&gt; links, in order to display their title content in an alert box. How to proceed ?</p>
<p>You could try to achieve this using a loop. Something like:<br />
<code><br />
&lt;html&gt;<br />
&lt;a id="0" href="./0" title="the first"&gt; link number 0 &lt;/a&gt;<br />
&lt;a id="1" href="./1" title="the second"&gt; link number 1 &lt;/a&gt;<br />
&lt;a id="2" href="./2" title="the third"&gt; link number 2 &lt;/a&gt;<br />
&lt;a id="3" href="./3" title="the fourth"&gt; link number 3 &lt;/a&gt;<br />
&lt;a id="4" href="./4" title="the fifth"&gt; link number 4 &lt;/a&gt;<br />
&lt;/html&gt;<br />
</code><br />
<code><br />
&lt;script&gt;<br />
var number_of_link = 5;<br />
for(i=0;i&lt;number_of_link;i++){<br />
document.getElementById(i).onmouseover =<br />
function(){<br />
alert("I'm the number "+i+" link")<br />
};<br />
}<br />
&lt;/script&gt;<br />
</code></p>
<p>Just try this tiny snippet: have you seen the problem ?<br />
Every link has a function showing the last value of the i variable!</p>
<p>So, what does happened ?</p>
<p>We start iterating over a integer called i. This variable is in a global socpe. For each element found, we set a new function, wich display the i value. But the variable is passed by raference, and we want it to be passed by value !</p>
<p>Fortunately, we have a solution: we will dynamically add a new Function object, which will result in &laquo;&nbsp;statically&nbsp;&raquo; defined function.</p>
<p><code><br />
&lt;html&gt;<br />
&lt;a id="0" href="./0" title="the first"&gt; link number 0 &lt;/a&gt;<br />
&lt;a id="1" href="./1" title="the second"&gt; link number 1 &lt;/a&gt;<br />
&lt;a id="2" href="./2" title="the third"&gt; link number 2 &lt;/a&gt;<br />
&lt;a id="3" href="./3" title="the fourth"&gt; link number 3 &lt;/a&gt;<br />
&lt;a id="4" href="./4" title="the fifth"&gt; link number 4 &lt;/a&gt;<br />
&lt;/html&gt;<br />
</code><br />
<code><br />
&lt;script&gt;<br />
var number_of_link = 5;<br />
for(i=0;i&lt;number_of_link;i++){<br />
document.getElementById(i).onmouseover =<br />
new Function("alert("I'm the number "+i+" link)");<br />
}<br />
&lt;/script&gt;<br />
</code></p>
<p>The <em>Function</em> constructor allow us to take a String, and declaring it as a function. This tip forces the js interpetor to build the string, and only after, to build the function itself. So we achieve to have a static number written in each function we defined and desired omouseover effect.</p>
<p>For further information about function creation, i should recommend you this page:</p>
<p><a href="http://www.howtocreate.co.uk/tutorials/javascript/functions">www.howtocreate.co.uk/tutorials/javascript/functions</a></p>
<p>Have a look too, to some article describing the javascript closure:</p>
<p><a href="http://www.jibbering.com/faq/faq_notes/closures.html">www.jibbering.com/faq/faq_notes/closures.html</a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/haveacafe.wordpress.com/8/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/haveacafe.wordpress.com/8/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/haveacafe.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/haveacafe.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/haveacafe.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/haveacafe.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/haveacafe.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/haveacafe.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/haveacafe.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/haveacafe.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/haveacafe.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/haveacafe.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=haveacafe.wordpress.com&blog=2198367&post=8&subd=haveacafe&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://haveacafe.wordpress.com/2008/08/26/dealing-with-javascript-variable-scope-and-for-loop-or-external-function/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6b8e035a4a7eba7f8f4674a34c21b97b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">haveacafe</media:title>
		</media:content>
	</item>
	</channel>
</rss>