<?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>Matt Thommes &#187; JavaScript</title>
	<atom:link href="http://matthom.com/archive/category/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://matthom.com</link>
	<description></description>
	<lastBuildDate>Fri, 24 May 2013 22:04:01 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Pretty JSON from PHP</title>
		<link>http://matthom.com/archive/2012/12/14/pretty-json-from-php</link>
		<comments>http://matthom.com/archive/2012/12/14/pretty-json-from-php#comments</comments>
		<pubDate>Sat, 15 Dec 2012 01:20:36 +0000</pubDate>
		<dc:creator>Matt Thommes</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://matthom.com/?p=6869</guid>
		<description><![CDATA[I recently needed to display a PHP array in the browser so it can be easily modified and then saved back to the server. I could easily loop through the array and create an HTML table, or some other visually readable HTML layout to examine and update the data, but that would be a lot [...]]]></description>
				<content:encoded><![CDATA[<p>I recently needed to display a PHP array in the browser so it can be easily modified and then saved back to the server.</p>
<p>I could easily loop through the array and create an HTML table, or some other visually readable HTML layout to examine and update the data, but that would be a lot of work. Not only would I have to code the HTML table layout (from the PHP array), I&#8217;d have to build in some sort of update mechanism to save the data back to the server.</p>
<p>Since this was intended for developers to access (and not average users), I didn&#8217;t really need to make it user-friendly, but I wanted it to at least make it <em>developer-friendly</em>.</p>
<p>So my thought was to just display a JavaScript object (which is human-readable by nature) that can be modified directly (perhaps in a <code>&lt;textarea&gt;</code> or something like that), and then be able to save the entire modified JavaScript object back to the server, as is. Since <a href="http://php.net/manual/en/function.json-decode.php">PHP can easily read a JSON string</a> and convert it to an object (which is what I wanted in the end), this seemed to be the quickest approach.</p>
<h2>The problem</h2>
<p>I was having a hard time with the following:</p>
<ol>
<li>Converting the PHP array into a valid JavaScript object. I could easily convert it to a JSON <strong>string</strong> (using <code>json_encode($array)</code>), but I didn&#8217;t want a JSON string &#8211; I wanted a literal object in JavaScript.</li>
<li>Displaying the JavaScript object in a readable fashion (broken out on separate lines, as opposed to everything on one line). <code>json_encode($array)</code> would provide me the string to display, but it put everything on a single line, making it very difficult to read.</li>
</ol>
<p>Here&#8217;s an example PHP array:</p>
<pre>$array = array(
  "key1" => array(
    "key1a" => "value1",
    "key1b" => "value2",
  ),
  "key2" => "something",
);
</pre>
<p>&#8230; and how I wanted it to look in the browser (inside a <code>&lt;textarea&gt;</code>):</p>
<pre>{
  "key1": {
    "key1a" : "value1",
    "key1b" : "value2"
  },
  "key2": "something"
}
</pre>
<h2>The solution</h2>
<p>In PHP, first encode your array into a JSON string:</p>
<pre>$json_string = json_encode($array);

// above will result in:
// {"key1":{"key1a":"value1","key1b":"value2"},"key2":"something"}
</pre>
<p>Then in JavaScript, parse the JSON string into a JavaScript object (using jQuery):</p>
<pre>var json_object = jQuery.parseJSON('&lt;?php echo $json_string; ?&gt;');
</pre>
<p>Then (also in JavaScript) convert the JSON object into a &#8220;pretty&#8221; string:</p>
<pre>var json_string = JSON.stringify(json_object, null, 2);
</pre>
<p>Write it to the DOM:</p>
<pre>&lt;textarea name="data_json" id="data_json"&gt;

$("#data_json").html(json_string);
</pre>
<p>This will make it readable <strong>and</strong> editable. Example:</p>
<pre><textarea style="height: 130px; width: 300px;">{
  "key1": {
    "key1a" : "value1",
    "key1b" : "value2"
  },
  "key2": "something"
}</textarea></pre>
<p>Then when saving it back to the server, PHP can once again convert it to an object so the data can be easily processed:</p>
<pre>$data = json_decode($_POST["data_json"]);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://matthom.com/archive/2012/12/14/pretty-json-from-php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Manipulating HTML with JavaScript: &lt;label&gt; element &#8220;for&#8221; attribute</title>
		<link>http://matthom.com/archive/2009/11/18/manipulating-html-with-javascript-label-element-for-attribute</link>
		<comments>http://matthom.com/archive/2009/11/18/manipulating-html-with-javascript-label-element-for-attribute#comments</comments>
		<pubDate>Wed, 18 Nov 2009 17:16:31 +0000</pubDate>
		<dc:creator>Matt Thommes</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://matthom.com/archive/2009/11/18/manipulating-html-with-javascript-label-element-for-attribute</guid>
		<description><![CDATA[When manipulating HTML documents via JavaScript and the DOM, it&#8217;s important to watch out for reserved-keyword-crossover issues, where certain HTML element or attribute names are actually reserved keywords in JavaScript. This is important to know when trying to declare HTML element attributes using JavaScript object.property syntax. For example, let&#8217;s say I want to create this [...]]]></description>
				<content:encoded><![CDATA[<p>When manipulating HTML documents via JavaScript and the DOM, it&#8217;s important to watch out for <strong>reserved-keyword-crossover</strong> issues, where certain HTML element or attribute names are actually reserved keywords in JavaScript.</p>
<p>This is important to know when trying to declare HTML element attributes using JavaScript <code>object.property</code> syntax.</p>
<p>For example, let&#8217;s say I want to create this HTML element using JavaScript:</p>
<p><code>
<div class="one"></div>
<p></code></p>
<p>I <strong>can&#8217;t</strong> do something like this:</p>
<pre>
var div1 = document.createElement("div");

div1.class = "one";
</pre>
<p>The word &#8220;class&#8221; is reserved in JavaScript, and although you&#8217;re intention is to declare the HTML elements&#8217; &#8220;class&#8221; <strong>attribute</strong>, this won&#8217;t work.</p>
<p>Instead, you need to do this:</p>
<pre>
div1.className = "one";
</pre>
<p>&#8220;className&#8221; is a subtle alternative that will apply the &#8220;class&#8221; attribute to any HTML element.</p>
<p><em>Side-note: I don&#8217;t quite understand why it&#8217;s called &#8220;className,&#8221; when &#8220;classAttributeValue,&#8221; or &#8220;classValue&#8221; would make more sense.</em></p>
<h2>HTML <code><label></code> &#8220;for&#8221; attribute</h2>
<p>Another tricky HTML element attribute to declare in JavaScript is the &#8220;for&#8221; attribute, which is used in the <code><label></code> element. For example:</p>
<pre>
<label for="one">Check One</label>

<input type="checkbox" id="one" />
</pre>
<p>In JavaScript, we <strong>can&#8217;t</strong> do this:</p>
<pre>
var label1 = document.createElement("label");

label1.for = "one";
</pre>
<p>&#8220;for&#8221; is also a reserved JavaScript keyword.</p>
<p>I bet you&#8217;re guessing we need to use <code>forName</code> in JavaScript? Unfortunately, this naming convention is not applicable to any attribute.</p>
<p>Instead, we can try something like this:</p>
<pre>
var label1 = document.createElement("label");

label1.setAttribute("for", "one");
</pre>
<p>This seems to work fine in most standards-aware browsers, but Internet Explorer 8 does not seem to support it, so at the moment it&#8217;s not a universal solution.</p>
<p>If anyone has another work-around for this, please help resolve this &#8220;pain.&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://matthom.com/archive/2009/11/18/manipulating-html-with-javascript-label-element-for-attribute/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JavaScript: calling events inline</title>
		<link>http://matthom.com/archive/2009/03/13/javascript-calling-events-inline</link>
		<comments>http://matthom.com/archive/2009/03/13/javascript-calling-events-inline#comments</comments>
		<pubDate>Fri, 13 Mar 2009 12:41:04 +0000</pubDate>
		<dc:creator>Matt Thommes</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://matthom.com/archive/2009/03/13/javascript-calling-events-inline</guid>
		<description><![CDATA[There are a few different ways to call events in JavaScript &#8211; the most popular being as inline attributes. There is often some confusion surrounding the proper way to designate JavaScript events inline, while maintaining application composure if JavaScript is turned off. The most commonly used approach looks something like this: Link Here we use [...]]]></description>
				<content:encoded><![CDATA[<p>There are a few different ways to call events in JavaScript &#8211; the most popular being as <strong>inline attributes</strong>. There is often some confusion surrounding the proper way to designate JavaScript events inline, while maintaining application composure if JavaScript is turned off.</p>
<p>The most commonly used approach looks something like this:</p>
<p><code><a href="#" onclick="my_function()">Link</a></code></p>
<p>Here we use a &#8220;pound&#8221; or &#8220;hash&#8221; character as the <code>href</code> value. This is because we need to put <em>something</em> in there, but we only care about the <code>onclick</code> event firing. Turns out this is not the best approach because it fails to do anything if JavaScript is turned off.</p>
<p>Ideally, you should provide a <strong>real</strong> location as the <code>href</code> value:</p>
<p><code><a href="page.htm" onclick="my_function(); return false;">Link</a></code></p>
<p>The above approach is safer, because if JavaScript is turned off, the browser will simply go to <code>page.htm</code>, and ignore the <code>onclick</code> attribute.</p>
<p><code>my_function()</code> could then look something like this:</p>
<pre>
function my_function()
{
    window.location = "page.htm";
}
</pre>
<p>With JavaScript turned on, the browser acknowledges the <code>onclick</code> attribute. We also add <code>return false;</code> inline, so the link does not go anywhere until we explicitly tell it to within the <code>my_function</code> function. This is useful in case there are reasons we&#8217;d like to stop the link from working &#8211; perhaps there is a user-submitted error on the page that needs to be corrected before proceeding.</p>
<h3>Same for <code><br />
<form></code></h3>
<p>This approach should be consistent when handling HTML forms.</p>
<p><code><br />
<form method="post" action="page.php" id="my_form" onsubmit="validate(); return false;"></code></p>
<p>Again, if JavaScript is turned off, the form submits to <code>page.php</code> right away, where we&#8217;d process the form details. If JavaScript is turned on, we first run a validation function to ensure the form values are accurate before submitting to the server:</p>
<pre>
function validate()
{
    var my_form = document.getElementById("my_form");

    my_form.submit();
}
</pre>
<p>Here we submit the form via JavaScript, but only if it&#8217;s passed all of our validation tests. If not, we just <strong>don&#8217;t</strong> run this line:</p>
<p><code>my_form.submit();</code></p>
<p>If that line is not run, the function will return true, then it will see the <code>return false;</code> <strong>inline</strong>, and stop everything. This is what we want, because we want to stop everything if the form should <strong>not</strong> be submitted.</p>
<p>Keep in mind, when handling submitted form data, you should always validate both client <em>and</em> server side. So, even if they pass the JavaScript validation tests, those same checks should be applied in the PHP page.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthom.com/archive/2009/03/13/javascript-calling-events-inline/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Shortwave: portable keyword searches</title>
		<link>http://matthom.com/archive/2008/07/07/shortwave-portable-keyword-searches</link>
		<comments>http://matthom.com/archive/2008/07/07/shortwave-portable-keyword-searches#comments</comments>
		<pubDate>Mon, 07 Jul 2008 12:58:59 +0000</pubDate>
		<dc:creator>Matt Thommes</dc:creator>
				<category><![CDATA[Firefox]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Search]]></category>

		<guid isPermaLink="false">http://matthom.com/archive/2008/07/07/shortwave-portable-keyword-searches</guid>
		<description><![CDATA[Shortwave is similar to Firefox keyword searches, except as a JavaScript bookmark, it is browser independent. This is useful if you use many different browsers, or just wish to have a central list of functioning keyword bookmarks. Although, I don&#8217;t like having to manually click on the bookmark before typing in my keyword search, but [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://shortwaveapp.com/">Shortwave</a> is similar to <a href="http://matthom.com/archive/2005/10/10/more-firefox-keyword-fun">Firefox keyword searches</a>, except as a JavaScript bookmark, it is browser independent. This is useful if you use many different browsers, or just wish to have a central list of functioning keyword bookmarks.</p>
<p>Although, I don&#8217;t like having to manually click on the bookmark before typing in my keyword search, but I suppose I could get used to it. The nice part about Firefox keyword search is that it allows you to use the address bar, which is where you&#8217;d type in your URL anyway.</p>
<p>However, I like the idea of having my keyword searches accessible regardless of the current browser I am using. In a way, this makes my keyword bookmarks <strong>portable</strong> and future-proof.</p>
<p>Also, it&#8217;s an absolute <em>must</em> for iPhone/iPod touch users. It will save time typing, as well as reduce page loading time over the slow EDGE network, by bringing you directly to the search results page, rather than first going to the search page, <em>then</em> the results.</p>
<h2>Create your own custom Shortwave</h2>
<p>Creating your own Shortwave is easy. Just download the default <a href="http://shortwaveapp.com/waves.txt">waves.txt file</a>, edit it with your own bookmarks, then upload to a web server of your own.</p>
<p>Go back to the <a href="http://shortwaveapp.com/">Shortwave home page</a>, and paste the URL to your waves.txt file into the box.</p>
<h2>Sync your iPhone/iPod bookmarks</h2>
<p>On your desktop Safari, drag the Shortwave bookmark to your bookmarks toolbar. In iTunes, sync your bookmarks to your iPhone or iPod touch.</p>
<p>You&#8217;ll then have a JavaScript-prompt bookmark saved on your mobile device, ready for your keyword searches.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthom.com/archive/2008/07/07/shortwave-portable-keyword-searches/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Blog widgets more harmful than good</title>
		<link>http://matthom.com/archive/2007/08/12/blog-widgets-more-harmful-than-good</link>
		<comments>http://matthom.com/archive/2007/08/12/blog-widgets-more-harmful-than-good#comments</comments>
		<pubDate>Sun, 12 Aug 2007 10:54:21 +0000</pubDate>
		<dc:creator>Matt Thommes</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Perception]]></category>

		<guid isPermaLink="false">http://matthom.com/archive/2007/08/12/blog-widgets-more-harmful-than-good</guid>
		<description><![CDATA[I&#8217;ve talked before about only pushing content relative to it&#8217;s domain: Avoid using feeds as content. Trying too hard to merge content. Flash and JavaScript widgets are tacky, gaudy, and unprofessional. I&#8217;ve experimented with widgets before, but lately I&#8217;ve been feeling they cause more harm than good. Depending on what kind of personal site/blog you [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve talked before about only pushing content relative to it&#8217;s domain:</p>
<ul>
<li><a href="http://www.matthom.com/archive/2007/07/27/avoid-using-feeds-as-content">Avoid using feeds as content</a>.</li>
<li><a href="http://www.matthom.com/archive/2007/05/21/trying-too-hard-to-merge-content">Trying too hard to merge content</a>.</li>
</ul>
<p>Flash and JavaScript widgets are tacky, gaudy, and unprofessional. <a href="http://www.matthom.com/archive/2007/03/21/my-twitter-badge-goes-live">I&#8217;ve experimented with widgets before</a>, but lately I&#8217;ve been feeling they cause more harm than good.</p>
<p>Depending on what kind of personal site/blog you are running, you may want widgets floating all over the place. Heck, I&#8217;ve seen some pretty effective sites with only one or two widgets.</p>
<p>But in most cases, widgets only &#8220;booger up&#8221; a web page. It&#8217;s almost like <a href="http://daringfireball.net/linked/2007/august#thu-09-keefe">sticking &#8220;Intel Inside&#8221; stickers all over your computer</a>.</p>
<p>Widgets promote a more cluttered look, often break HTML layouts, and reduce page loading speed.</p>
<p>Oh, and there&#8217;s <a href="http://jeremy.zawodny.com/blog/archives/008547.html">plenty more reasons why they suck</a>.</p>
<p>Any ideas on a solution to this problem? Should sites allow API&#8217;s, rather than widgets?</p>
]]></content:encoded>
			<wfw:commentRss>http://matthom.com/archive/2007/08/12/blog-widgets-more-harmful-than-good/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Removing all child nodes from an element</title>
		<link>http://matthom.com/archive/2007/05/03/removing-all-child-nodes-from-an-element</link>
		<comments>http://matthom.com/archive/2007/05/03/removing-all-child-nodes-from-an-element#comments</comments>
		<pubDate>Thu, 03 May 2007 11:36:51 +0000</pubDate>
		<dc:creator>Matt Thommes</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Video Embed]]></category>

		<guid isPermaLink="false">http://matthom.com/archive/2007/05/03/removing-all-child-nodes-from-an-element</guid>
		<description><![CDATA[When manipulating the DOM, it&#8217;s often useful to remove all child nodes from a specific element. This typically comes in handy when you&#8217;re looking to replace the content of an element with a separate form element, such as an , so the user can edit the actual value. Here&#8217;s an example of something I recently [...]]]></description>
				<content:encoded><![CDATA[<p>When manipulating the <acronym title="Document Object Model">DOM</acronym>, it&#8217;s often useful to remove all child nodes from a specific element. This typically comes in handy when you&#8217;re looking to <em>replace</em> the content of an element with a separate <strong>form element</strong>, such as an <code><input></code>, so the user can <em>edit</em> the actual value.</p>
<p>Here&#8217;s an example of something I recently created that illustrates my point:</p>
<p id="DynamicFormElements.flv"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</p>
<p><script type="text/javascript">
 var FO = { movie:"https://media.dreamhost.com/mediaplayer.swf",width:"480",height:"270",majorversion:"7",build:"0",bgcolor:"#FFFFFF",
            flashvars:"file=http://www.matthom.com/downloads/DynamicFormElements.flv&#038;showdigits=true&#038;autostart=false" };
UFO.create(FO,"DynamicFormElements.flv");
</script></p>
<p>These &#8220;dynamic form elements&#8221; are written to the page only when the user performs a certain action; in this case: clicking on a table cell.</p>
<p>The HTML for the table cell could look something like this:</p>
<p><code>
<td id="cell">$1.55</td>
<p></code></p>
<p>Once the user clicks on a cell, the HTML of the cell changes to:</p>
<p><code>
<td id="cell"><input type="text" value="$1.55" /></td>
<p></code></p>
<p>This gives the user a text box with which to edit the value.</p>
<p>In order to do this, you&#8217;ll need a quick way to <em>remove</em> any existing <strong>child nodes</strong> of the table cell.</p>
<p>There are many ways to do this in JavaScript, but I&#8217;ve discovered one that seems to work in all types of situations:</p>
<pre>
var cell = document.getElementById("cell");

if ( cell.hasChildNodes() )
{
    while ( cell.childNodes.length >= 1 )
    {
        cell.removeChild( cell.firstChild );       
    } 
}
</pre>
<p>No matter how many nested nodes exist, it removes them all. Quite handy.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthom.com/archive/2007/05/03/removing-all-child-nodes-from-an-element/feed</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
		<item>
		<title>My Twitter badge goes live</title>
		<link>http://matthom.com/archive/2007/03/21/my-twitter-badge-goes-live</link>
		<comments>http://matthom.com/archive/2007/03/21/my-twitter-badge-goes-live#comments</comments>
		<pubDate>Wed, 21 Mar 2007 20:00:54 +0000</pubDate>
		<dc:creator>Matt Thommes</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://matthom.com/archive/2007/03/21/my-twitter-badge-goes-live</guid>
		<description><![CDATA[I&#8217;ve &#8220;twitterized&#8221; the front page of this site, after a few weeks of debate. You&#8217;ll notice my Twitter thoughts on the upper right, above the &#8220;What I&#8217;m reading today&#8230;&#8221; section. First off, I&#8217;ve discussed Twitter before, and I still stand by my positive opinions. I did, however, have a small issue with posting yet another [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve &#8220;twitterized&#8221; the <a href="http://www.matthom.com/base/">front page of this site</a>, after a few weeks of debate. You&#8217;ll notice <a href="http://twitter.com/Matthom">my Twitter thoughts</a> on the upper right, above the &#8220;What I&#8217;m reading today&#8230;&#8221; section.</p>
<p>First off, I&#8217;ve <a href="http://www.matthom.com/archive/2007/01/17/twitter-the-universal-blog">discussed Twitter before</a>, and I still stand by my positive opinions. I did, however, have a small issue with posting yet <em>another</em> <strong>JavaScript badge</strong> onto my site.</p>
<p>It seems this is a current trend that can&#8217;t be avoided (at least, not avoided painlessly), so I am going to just do it, for now. I already have a couple JavaScript badges on <a href="http://www.matthom.com/base/">my front page</a>: the Google Reader Shared Items (&#8220;What I&#8217;m reading today&#8230;&#8221;), and my five most recent Flickr photos. I was hesitant to add yet another.</p>
<p>So what are my issues with JavaScript badges? I&#8217;ll let <a href="http://jeremy.zawodny.com/blog/archives/008547.html">Jeremy answer that one</a>, but in a nutshell:</p>
<ul>
<li>Slower page loading time.</li>
<li>Search engines can&#8217;t &#8220;see&#8221; this included content.</li>
</ul>
<p>The &#8220;slower page loading time&#8221; is due to the JavaScript badge making an external server call, each time the page is loaded. What happens when the external server is down? My site fails to load. This &#8220;reliance&#8221; on a third-party application is unsettling at best.</p>
<p>The biggest complaint is that search engines can&#8217;t &#8220;see&#8221; JavaScript badge text, so therefore it&#8217;s like the content doesn&#8217;t even exist on my site.</p>
<p>This complaint can be argued back and forth endlessly, because some people believe that <strong>web sites should be made for visitors, not search engines</strong>. <a href="http://www.google.com/support/webmasters/bin/answer.py?answer=35769">Google even says so</a>:</p>
<blockquote><p>
  Make pages for users, not for search engines.</p>
</blockquote>
<p>I agree with this sentiment, so that&#8217;s why I decided to just ignore my &#8220;gut instinct&#8221; for now.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthom.com/archive/2007/03/21/my-twitter-badge-goes-live/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript dynamic DOM retrieval</title>
		<link>http://matthom.com/archive/2007/01/03/javascript-dynamic-dom-retrieval</link>
		<comments>http://matthom.com/archive/2007/01/03/javascript-dynamic-dom-retrieval#comments</comments>
		<pubDate>Wed, 03 Jan 2007 17:54:32 +0000</pubDate>
		<dc:creator>Matt Thommes</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://matthom.com/archive/2007/01/03/javascript-dynamic-dom-retrieval</guid>
		<description><![CDATA[Your dynamic page generates an HTML table from a database query. Each record has a unique ID, and you use that ID for the id attribute on the elements: .... .... This provides each with a unique ID (id attributes must be unique, anyway), so you can now target specific table rows with JavaScript. Problem [...]]]></description>
				<content:encoded><![CDATA[<p>Your dynamic page generates an HTML table from a database query. Each record has a unique ID, and you use that ID for the <code>id</code> attribute on the <code><br />
<tr></code> elements:</p>
<pre>
....

<tr id="myRow_13">

    <td></td>

</tr>

<tr id="myRow_29">

    <td></td>

</tr>

....
</pre>
<p>This provides each <code><br />
<tr></code> with a unique ID (<code>id</code> attributes <strong>must</strong> be unique, anyway), so you can now target specific table rows with JavaScript.</p>
<p>Problem is&#8230; how do you know which rows are pulled? In other words, when the page loads the <strong>dynamic</strong> content, JavaScript won&#8217;t know which unique id&#8217;s are actually residing in the DOM.</p>
<blockquote><p>
  Proposed definition of &#8220;dynamic&#8221;: <strong>could change</strong>.</p>
</blockquote>
<h2>Pull all <code><br />
<tr></code> elements</h2>
<p>JavaScript typically targets specific elements using <code>getElementById()</code>. We won&#8217;t be able to use this, because we <em>don&#8217;t know</em> the id&#8217;s on the page. Instead, let&#8217;s use <code>getElementsByTagName()</code>.</p>
<p><code>getElementsByTagName()</code> does just what it sounds like &#8211; it gets all elements using a certain tag name.</p>
<p><code>var allTrElements = getElementsByTagName("tr");</code></p>
<p>This will grab all <code><br />
<tr></code> elements, and store them into an array, which we can then loop through to perform additional programming.</p>
<p>Problem solved?</p>
<p>Not quite. <code>getElementsByTagName("tr")</code> will pull <strong>all</strong> <code><br />
<tr></code> elements on the page, including ones you may not want or need access to.</p>
<p>So how do we pull <em>only</em> the <code><br />
<tr></code> elements from the specific table with the database rows?</p>
<h2>Pull only certain <code><br />
<tr></code> elements</h2>
<p>We can look at the <code>id</code> attribute, and base it off that. If the <code>id</code> attribute <strong>starts with</strong> &#8220;myRow&#8221; &#8211; then we know that&#8217;s a <code><br />
<tr></code> we want to work with.</p>
<pre>
// STORE ALL <tr> ELEMENTS IN AN ARRAY.

var allTrElements = getElementsByTagName("tr");


// LOOP THROUGH ALL <tr> ELEMENTS ON THE PAGE.

for ( var i=0; i < allTrElements.length; i++ )
{

    // SET THE CURRENT <tr> ELEMENT TO A VARIABLE.

    var tr = allTrElements[i];


    // IF THE CURRENT <tr> HAS AN id ATTRIBUTE
    // THAT BEGINS WITH "myRow", THEN PROCEED.

    if ( tr.id.substr( 0, 5 ) == "myRow" )
    {

        // EXTRACT THE id NUMBER OFF OF THE id ATTRIBUTE.

        var rowId = tr.id.substr( 6 );


        // TARGET THE SPECIFIC ROW.

        var rowTarget = document.getElementById( "myRow_" + rowId );


        // DO SOMETHING SPECIFIC TO THE ROW.

        rowTarget.className = "highlight";

    }

}
</pre>
<p>So, using the <code>id</code> attribute of the <code><br />
<tr></code> element, we can throw away the <code><br />
<tr></code> elements that we don&#8217;t want to target, as long as each <code>id</code> is named consistently.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthom.com/archive/2007/01/03/javascript-dynamic-dom-retrieval/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JavaScript open links in new window</title>
		<link>http://matthom.com/archive/2006/10/22/javascript-open-links-in-new-window</link>
		<comments>http://matthom.com/archive/2006/10/22/javascript-open-links-in-new-window#comments</comments>
		<pubDate>Sun, 22 Oct 2006 10:44:34 +0000</pubDate>
		<dc:creator>Matt Thommes</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://matthom.com/archive/2006/10/22/javascript-open-links-in-new-window</guid>
		<description><![CDATA[I&#8217;ve recently adopted this Sitepoint approach to opening links in new browser windows. Using this method, all I have to do is apply an attribute of rel="external" to any link that I wish to open in a new browser window. To me, this is much more preferable (and standards-compliant) than using target="_blank". The JavaScript function [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve recently adopted <a href="http://www.sitepoint.com/article/standards-compliant-world">this Sitepoint approach</a> to opening links in new browser windows.</p>
<p>Using this method, all I have to do is apply an attribute of <code>rel="external"</code> to any link that I wish to open in a new browser window. To me, this is much more preferable (and standards-compliant) than using <code>target="_blank"</code>.</p>
<p>The JavaScript function (obtained from Sitepoint) that controls this is relatively straight-forward. I also made an addition to the function that controls links that you want to open in the <strong>same external browser window</strong>. In other words, you open the external window once, and then every additional link opens in that same external window (as opposed to a new window each time).</p>
<pre>
function externalLinks()
{
    if (!document.getElementsByTagName) return;

    var anchors = document.getElementsByTagName("a");

    for ( var i=0; i < anchors.length; i++ )
    {
        var anchor = anchors[i];

        if ( anchor.getAttribute("href") &#038;&#038; anchor.getAttribute("rel") == "external" )
        {
            <strong>if ( anchor.getAttribute("className") )
            {
                anchor.target = anchor.getAttribute("className");
            }
            else
            {
                anchor.target = "_blank";
            }</strong>
        }
    }
}
</pre>
<p>For explanation on how the function works, please see the <a href="http://www.sitepoint.com/article/standards-compliant-world">original Sitepoint article</a>.</p>
<p>My addition, which is not part of the original script from Sitepoint, is in <strong>bold</strong>. It checks to see if the <code>class</code> attribute is set for the <code><a></code> element. If so, it uses that <code>class</code> attribute <em>value</em> as the <strong>name of the external window</strong>. So, as long as every link (that you wish to have open in the <strong>same</strong> external window) has the same <code>class</code> attribute &#8211; all of those links will open in the <strong>same parent window</strong>.</p>
<p>For example, here are three links that I want to open in the <strong>same</strong> external window:</p>
<p><code><a href="link1.php" class="newWindow" rel="external"> Link 1 </a></code></p>
<p><code><a href="link2.php" class="newWindow" rel="external"> Link 2 </a></code></p>
<p><code><a href="link3.php" class="newWindow" rel="external"> Link 3 </a></code></p>
<p>Here are two more links that will open in an external window, but not the same one as the above three links:</p>
<p><code><a href="link4.php" class="differentWindow" rel="external"> Link 4 </a></code></p>
<p><code><a href="link5.php" rel="external"> Link 5 </a></code></p>
<p>Notice the <code>class</code> attribute value has changed in <strong>Link 4</strong>, and the <code>class</code> attribute doesn&#8217;t even exist in <strong>Link 5</strong>.</p>
<h2>Cautions</h2>
<p>This entire script has only been tested in Firefox 1.5.x for Windows, and Internet Explorer 6.</p>
<p>There is also the slight problem of <strong>window focus</strong>. For example, if I click on a link that uses an existing window as it&#8217;s destination, the focus should be placed on that window. It is currently not happening that way.</p>
<p>Additions to this script may be made over time.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthom.com/archive/2006/10/22/javascript-open-links-in-new-window/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>IE capture option element onclick</title>
		<link>http://matthom.com/archive/2006/08/08/ie-capture-option-element-onclick</link>
		<comments>http://matthom.com/archive/2006/08/08/ie-capture-option-element-onclick#comments</comments>
		<pubDate>Tue, 08 Aug 2006 11:42:32 +0000</pubDate>
		<dc:creator>Matt Thommes</dc:creator>
				<category><![CDATA[Browsers]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://matthom.com/archive/2006/08/08/ie-capture-option-element-onclick</guid>
		<description><![CDATA[IE 6 doesn&#8217;t support this: Do This Do That This is aggravating, when you need to perform a certain function, dependent upon each option chosen from the list. For example, if someone chooses &#8220;Do This&#8221; from the select list, I need a certain function to run. However, if someone chooses &#8220;Do That&#8221; from the select [...]]]></description>
				<content:encoded><![CDATA[<p>IE 6 doesn&#8217;t support this:</p>
<pre>
<select id="do">

    <option <strong>onclick="do(&#39;Do This&#39;)"</strong> value="Do This"> Do This </option>
    <option <strong>onclick="do(&#39;Do That&#39;)"</strong> value="Do That"> Do That </option>

</select>
</pre>
<p>This is aggravating, when you need to perform a certain function, dependent upon <em>each option chosen from the list</em>.</p>
<p>For example, if someone chooses &#8220;Do This&#8221; from the select list, I need a certain function to run. However, if someone chooses &#8220;Do That&#8221; from the select list, I need a different function to run.</p>
<p>Turns out IE only recognizes <strong>onchange</strong>.</p>
<p>So, instead of applying an <code>onclick</code> to each <code><br />
<option></code> element, we&#8217;ll apply a single <code>onchange</code> to the entire select list:</p>
<pre>
<select id="do" <strong>onchange="do( this.value )"</strong>>

    <option value="Do This"> Do This </option>
    <option value="Do That"> Do That </option>

</select>
</pre>
<p>The JavaScript <code>do</code> function could look something like this:</p>
<pre>
function do( optionValue )
{
    switch( optionValue )
    {
        case "Do This" :
        
            // SPECIFIC CODE HERE
        
        break;
        
        case "Do That" :
        
            // SPECIFIC CODE HERE
        
        break;
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://matthom.com/archive/2006/08/08/ie-capture-option-element-onclick/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>
