<?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>The Error Message &#187; JavaScript</title>
	<atom:link href="http://www.theerrormessage.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.theerrormessage.com</link>
	<description>Fix your error</description>
	<lastBuildDate>Tue, 13 Dec 2011 20:16:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Delayed javascript function/handler gets the right variable/parameter values</title>
		<link>http://www.theerrormessage.com/2011/01/delayed-javascript-functionhandler-gets-the-right-parameter-values/</link>
		<comments>http://www.theerrormessage.com/2011/01/delayed-javascript-functionhandler-gets-the-right-parameter-values/#comments</comments>
		<pubDate>Thu, 13 Jan 2011 23:22:39 +0000</pubDate>
		<dc:creator>gbl</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/?p=286</guid>
		<description><![CDATA[So we have a function that adds a handler function that is called after an ajax load or at a later time and we need to execute the handler function code for each item inside an array allowing the function to access the array values. Usually with the simplest try you would get some code [...]]]></description>
			<content:encoded><![CDATA[<p>So we have a function that adds a handler function that is called after an ajax load or at a later time and we need to execute the handler function code for each item inside an array allowing the function to access the array values. Usually with the simplest try you would get some code that get&#8217;s executed with invalid variable values. Examples are below.</p>
<p>The wrong code:</p>
<pre class="code brush:javascript">
//the function that will do the stuff for each array
var dostuff = function (arr) {
var n = arr.length,
    i;
    for(i = 0; i < n; i++) {
        var o = arr[i];
        //event bind or delayed function call
        setTimeout(function () {
                //we want to access here the correct arr[i]
                //when the code gets here the value of o is arr[arr.length-1]
                console.log(o);
        }, 1000);
    }
}
dostuff([1,2,3,4]);
</pre>
<p>You can load the code into your javascript console to see it in action.<br />
The output appears after 1 second and it is like this:</p>
<pre class="code brush:text">
4
4
4
4
</pre>
<p>I don't need that. I need something that works so we need to wrap the handler call into a function call that will pass the current values as parameters.</p>
<pre class="code brush:javascript">
var dostuff = function (arr) {
var n = arr.length,
    i;
    for(i = 0; i < n; i++) {
        var o = arr[i];
        (function (y) {
            setTimeout(function () {
                console.log(y);
            }, 1000);
        }) (o);
    }
}
dostuff([1,2,3,4]);
</pre>
<p>The output appears after 1 second and it is like this:</p>
<pre class="code brush:text">
1
2
3
4
</pre>
<p>The final best solution would be to have the function inside for already defined so you don't define it on every loop:</p>
<pre class="code brush:javascript">
var dostuff_item = function (y) {
            setTimeout(function () {
                console.log(y);
         }, 1000);
 }
var dostuff = function (arr) {
var n = arr.length,
    i;
    for(i = 0; i < n; i++) {
        var o = arr[i];
        dostuff_item (o);
    }
}
dostuff([1,2,3,4]);
</pre>
<p>Tada!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2011/01/delayed-javascript-functionhandler-gets-the-right-parameter-values/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>base html tag does not apply correctly to all javascript and style files</title>
		<link>http://www.theerrormessage.com/2009/07/base-html-tag-does-not-apply-correctly-to-all-javascript-and-style-files/</link>
		<comments>http://www.theerrormessage.com/2009/07/base-html-tag-does-not-apply-correctly-to-all-javascript-and-style-files/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 15:21:40 +0000</pubDate>
		<dc:creator>gbl</dc:creator>
				<category><![CDATA[Firefox]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Internet Explorer]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2009/07/03/base-html-tag-does-not-apply-correctly-to-all-javascript-and-style-files/</guid>
		<description><![CDATA[Problem: The &#60;base href="http://example.com/path/to/res/"&#62;   did not work at all on Internet Explorer &#8211; no javascript or style files were loaded and worked partially in Mozilla Firefox &#8211; a few javascript files were not pointing to the path that should be transformed by base href attribute. Example: Array( [0] =&#62; 1 ) &#60;!DOCTYPE html PUBLIC [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Problem</strong>: The
<pre class="code brush:html">&lt;base href="http://example.com/path/to/res/"&gt;</pre>
<p>  did not work at all on Internet Explorer &#8211; no javascript or style files were loaded and worked partially in Mozilla Firefox &#8211; a few javascript files were not pointing to the path that should be transformed by base href attribute.</p>
<p>Example:</p>
<pre class="code brush:html">
Array(
[0] =&gt; 1
)
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;base href="http://example.com/path/to/res/"&gt;
&lt;link rel="stylesheet" href="./css/styles.css" type="text/css" /&gt;
&lt;scrupt src="./js/jquery.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;scrupt src="./js/j1.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;scrupt src="./js/j2.js" type="text/javascript"&gt;&lt;/script&gt;
</pre>
<p>This code produced a page with no style files and  no javascript files included in Interenet Explorer 8 and a few javascript files (j1.js and j2.js) were not included in Firefox 3.5 because base tag was not applied. After several tests and all kind of different arrangements we discovered that the problem was the output before the page source the
<pre class="code brush:plain">Array ...</pre>
<p> thing.</p>
<p><strong>Solution</strong>: no output before the page&#8217;s headers otherwise you can get all kind of strange results including a html base tag not working correctly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2009/07/base-html-tag-does-not-apply-correctly-to-all-javascript-and-style-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Object doesn&#8217;t support property or method. Adding options in a &lt;select&gt; from a child window on Internet Explorer</title>
		<link>http://www.theerrormessage.com/2009/03/object-doesnt-support-property-or-method-adding-options-in-a-select-from-a-child-window-on-internet-explorer/</link>
		<comments>http://www.theerrormessage.com/2009/03/object-doesnt-support-property-or-method-adding-options-in-a-select-from-a-child-window-on-internet-explorer/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 01:55:36 +0000</pubDate>
		<dc:creator>gbl</dc:creator>
				<category><![CDATA[Internet Explorer]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2009/03/17/object-doesnt-support-property-or-method-adding-options-in-a-from-a-child-window-on-internet-explorer/</guid>
		<description><![CDATA[Full error message:  Object doesn&#8217;t support property or method. I got this error message in Internet Explorer 7 every time i tried to run code similar to the next snippet: window.opener.form.select[select.length]  = new Option(text, value); The problem is that on Internet Explorer due to some mysterious security restrictions you are not allowed to add new [...]]]></description>
			<content:encoded><![CDATA[<p>Full error message:  <em>Object doesn&#8217;t support property or method</em>. I got this error message in Internet Explorer 7 every time i tried to run code similar to the next snippet:</p>
<pre class="code brush:javascript">window.opener.form.select[select.length]  = new Option(text, value);</pre>
<p>The problem is that on Internet Explorer due to some mysterious security restrictions you are not allowed to add new options in a select element that resides in the parent (opener) window. In order to do this you need a workaround.</p>
<p>The easiest <strong>workaround</strong> is to <strong>write a function in the parent window that adds elements in the select element</strong>. The function should look something like this:</p>
<pre class="code brush:javascript">function addOptionToSelect(formName, selectName, optionText, optionValue) {
var elem = document.forms[formName].elements[selectName];
elem.options[elem.length] = new Option(optionText, optionValue);
}</pre>
<p>This function has to be written in the parent window&#8217;s javascript and it will be accessed from child window with some code like this:</p>
<pre class="code brush:javascript">window.opener.addOptionToSelect(formName, selectName, optionText, optioValue);</pre>
<p><strong>Conclusion:</strong> Internet Explorer sucks.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2009/03/object-doesnt-support-property-or-method-adding-options-in-a-select-from-a-child-window-on-internet-explorer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>jquery pngFix fixed</title>
		<link>http://www.theerrormessage.com/2009/03/jquery-pngfix-fixed/</link>
		<comments>http://www.theerrormessage.com/2009/03/jquery-pngfix-fixed/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 05:00:31 +0000</pubDate>
		<dc:creator>gbl</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2009/03/11/jquery-pngfix-fixed/</guid>
		<description><![CDATA[I tried to use the pngFix from this website: http://jquery.andreaseberhard.de/pngFix/. But it didn&#8217;t work and i received an javascript error in IE6. After several minutes of debugging and tries i found out that the error is generated by old syntax in selecting elements. It used jQuery(this).find("img[@src$=.png]").each(...); instead of jQuery(this).find("img[src$=.png]").each(...); So the fix is: Remove the [...]]]></description>
			<content:encoded><![CDATA[<p>I tried to use the pngFix from this website: <a href="http://jquery.andreaseberhard.de/pngFix/" target="_blank">http://jquery.andreaseberhard.de/pngFix/</a>. But it didn&#8217;t work and i received an javascript error in IE6.</p>
<p>After several minutes of debugging and tries i found out that the error is generated by old syntax in selecting elements. It used
<pre class="code brush:javascript">jQuery(this).find("img[@src$=.png]").each(...);</pre>
<p> instead of
<pre class="code brush:javascript">jQuery(this).find("img[src$=.png]").each(...);</pre>
<p>So <strong>the fix</strong> is: <strong>Remove the &#8220;@&#8221; character form the selector string. </strong></p>
<p>The same thing for the &#8220;pack&#8221; version except that the replaced string is:
<pre class="code brush:javascript">[@m$=.M]</pre>
<p> and the  replacement string is:
<pre class="code brush:javascript">[m$=.M]</pre>
<p>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2009/03/jquery-pngfix-fixed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to check/uncheck a bunch of checkboxes without using ids for the checkbox inputs</title>
		<link>http://www.theerrormessage.com/2007/12/how-to-selectdeselect-a-bunch-of-checkboxes-without-using-ids-for-the-checkbox-inputs/</link>
		<comments>http://www.theerrormessage.com/2007/12/how-to-selectdeselect-a-bunch-of-checkboxes-without-using-ids-for-the-checkbox-inputs/#comments</comments>
		<pubDate>Sun, 09 Dec 2007 18:21:04 +0000</pubDate>
		<dc:creator>thalissar</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[How to]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2007/12/09/how-to-selectdeselect-a-bunch-of-checkboxes-without-using-ids-for-the-checkbox-inputs/</guid>
		<description><![CDATA[My solution of checking/unchecking a group of HTML checkboxes using Javascript implies using an array of checkboxes, which means naming all the inputs of type &#8216;checkbox&#8217; like array_name[]. Example: &#60;form name="cb_form"&#62; &#60;input type="checkbox" name="cb[]" value="0" /&#62;Zero &#60;input type="checkbox" name="cb[]" value="1" /&#62;One &#60;input type="checkbox" name="cb[]" value="2" /&#62;Two &#60;input type="checkbox" name="cb[]" value="3" /&#62;Three &#60;/form&#62; In this example, [...]]]></description>
			<content:encoded><![CDATA[<p>My solution of checking/unchecking a group of HTML checkboxes using Javascript implies using an array of checkboxes, which means naming all the inputs of type &#8216;checkbox&#8217; like <em>array_name[]</em>. Example:</p>
<pre class="code brush:xhtml">&lt;form name="cb_form"&gt;
&lt;input type="checkbox" name="cb[]" value="0" /&gt;Zero
&lt;input type="checkbox" name="cb[]" value="1" /&gt;One
&lt;input type="checkbox" name="cb[]" value="2" /&gt;Two
&lt;input type="checkbox" name="cb[]" value="3" /&gt;Three
&lt;/form&gt;</pre>
<p>In this example, the name of the checkboxes array is <em>cb</em>.</p>
<p>Next, we place two links for checking/unchecking all checkboxes in our array. If someone clicks one of these links, the <em>check</em><em>All()</em> JavaScript function is called:</p>
<pre class="code brush:html">&lt;a href="" onclick="checkAll('cb_form', 'cb[]', true); return false;"&gt;Check all&lt;a&gt;
&lt;a href="" onclick="checkAll('cb_form', 'cb[]', false); return false;"&gt;Uncheck all&lt;a&gt;</pre>
<p>The checking/unchecking all checkboxes function in JavaScipt looks like this:</p>
<pre class="code brush:javascript">function checkAll(form_name, cb_name, value)
{
var cb_arr = document.forms[form_name].elements[cb_name];
// if the checkboxes exist
if(cb_arr)
{
// if the number of checkboxes is at least 2
if(cb_arr.length &gt; 1)
{
// for each checkbox
for(i = 0; i &lt; cb_arr.length; i++)
{
// check (value == true) or uncheck (value == false) it
cb_arr[i].checked = value;
}
}
else // cb_arr.length is undefined which means there is a single checkbox element that is not considered an array of one element
{
cb_arr.checked = value;
}
}
}
</pre>
<p>Note that if we only have one checkbox, the variable <em>cb</em> it is not considered an array, but a normal variable. This is useful in the situation of dinamically generated HTML pages (using PHP, for example) and the number of checkboxes varies from page to page.</p>
<p>You can test the example here:</p>
<p><script type="text/javascript"> function selectAll(form_name, cb_name, value) { var cb_arr = document.forms[form_name].elements[cb_name]; if(cb_arr) { if(cb_arr.length > 1) { for(i = 0; i < cb_arr.length; i++) { cb_arr[i].checked = value; } } else if(cb_arr.length == 1) { cb_arr.checked = value; } } } </script></p>
<form name="cb_form">
<input name="cb[]" value="0" type="checkbox" />Zero<br />
<input name="cb[]" value="1" type="checkbox" />One<br />
<input name="cb[]" value="2" type="checkbox" />Two<br />
<input name="cb[]" value="3" type="checkbox" /> Three<br />
</form>
<p><a onclick="selectAll('cb_form', 'cb[]', true); return false;" style="color: #000000; text-decoration: underline; cursor: pointer">Check all</a> <a onclick="selectAll('cb_form', 'cb[]', false); return false;" style="color: #000000; text-decoration: underline; cursor: pointer">Uncheck all</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2007/12/how-to-selectdeselect-a-bunch-of-checkboxes-without-using-ids-for-the-checkbox-inputs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

