<?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>Finalint &#187; Programming</title>
	<atom:link href="http://www.finalint.com/category/technical/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.finalint.com</link>
	<description>So says Donnie Garvich</description>
	<lastBuildDate>Mon, 16 May 2011 18:43:05 +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>Making simplyScroll Scroll Left-to-Right</title>
		<link>http://www.finalint.com/2010/12/23/making-simplyscroll-scroll-left-to-right/</link>
		<comments>http://www.finalint.com/2010/12/23/making-simplyscroll-scroll-left-to-right/#comments</comments>
		<pubDate>Thu, 23 Dec 2010 23:17:10 +0000</pubDate>
		<dc:creator>Donnie</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.finalint.com/?p=177</guid>
		<description><![CDATA[The folks over at Logicbox have written a nifty little jQuery plugin called &#8220;simplyScroll&#8221; that allows you to easily and seamlessly scroll through a collection of images with little effort.  This is a great way to avoid making a Flash animation to accomplish the same task and&#8230; it works on the iPhone. We needed to [...]]]></description>
			<content:encoded><![CDATA[<p>The folks over at Logicbox have written a nifty little jQuery plugin called &#8220;<a title="See the plugin" href="http://logicbox.net/jquery/simplyscroll/" target="_blank">simplyScroll</a>&#8221; that allows you to easily and seamlessly scroll through a collection of images with little effort.  This is a great way to avoid making a Flash animation to accomplish the same task and&#8230; it works on the iPhone. We needed to use it to make a looped, seamless scrolling header for a site on the quick.</p>
<p>One caveat, however, it doesn&#8217;t have an easy built-in way to scroll in the opposite direction of the default (ie. &#8220;Backwards&#8221;) which is what our client wanted (at the last minute). We emailed the author and were told that it would take &#8220;quite a lot&#8221; of changes to make this happen.  The client was breathing down our necks on the tight deadline and we were running out of time, so I jumped in with both feet to hack the thing.</p>
<p>Now I realize this isn&#8217;t the &#8220;right&#8221; way to do it. This is an admitted hack because I didn&#8217;t have time to go back and do it correctly. Maybe the original plugin&#8217;s author will add the correct functionality or maybe I&#8217;ll have time to do it &#8220;right&#8221; and send it to them. But for now, this will absolutely work.</p>
<p>First, change the default function called when you use the &#8220;auto&#8221; functionality (line 152):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>152
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">moveForward</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>- becomes -</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>152
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">moveBack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now tell the position reset functionality that the default position is the sum of the width of all of your elements (line 219):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>219
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.$clip<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">scrollLeft</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>- becomes -</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>219
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.$clip<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">scrollLeft</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">5904</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The number &#8220;5904&#8243; is just the added up width of every element we ended up having in the animation.</p>
<p>It&#8217;s that easy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.finalint.com/2010/12/23/making-simplyscroll-scroll-left-to-right/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>jQuery, mootools and NextGEN Smooth Gallery &#8211; A tale of failure</title>
		<link>http://www.finalint.com/2010/11/29/jquery-mootools-and-nextgen-smooth-gallery-a-tale-of-failure/</link>
		<comments>http://www.finalint.com/2010/11/29/jquery-mootools-and-nextgen-smooth-gallery-a-tale-of-failure/#comments</comments>
		<pubDate>Mon, 29 Nov 2010 21:44:05 +0000</pubDate>
		<dc:creator>Donnie</dc:creator>
				<category><![CDATA[Rants]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.finalint.com/?p=173</guid>
		<description><![CDATA[Maybe I&#8217;m behind the ball on this, but I&#8217;ve never tried using multiple JavaScript frameworks on the same page.  I&#8217;m the kind of developer who likes to stick to a framework and then make it do what I need it to do.  Sure, all of the major frameworks have some unique and useful plugins, but [...]]]></description>
			<content:encoded><![CDATA[<p>Maybe I&#8217;m behind the ball on this, but I&#8217;ve never tried using multiple JavaScript frameworks on the same page.  I&#8217;m the kind of developer who likes to stick to a framework and then make it do what I need it to do.  Sure, all of the major frameworks have some unique and useful plugins, but cutting down on load time and complexity usually trumps any small amount of extra functionality I may gain from using multiple frameworks.</p>
<p>Today, however, I was asked to diagnose an issue with a client&#8217;s WordPress site.  All of a sudden the following JavaScript error had started popping up:</p>
<div id="attachment_174" class="wp-caption aligncenter" style="width: 410px"><a href="http://www.finalint.com/wp-content/uploads/2010/11/ErrorConsole.png"><img class="size-medium wp-image-174" title="Error Console" src="http://www.finalint.com/wp-content/uploads/2010/11/ErrorConsole-400x134.png" alt="Error Console" width="400" height="134" /></a><p class="wp-caption-text">The evidence</p></div>
<p>The folks who had been working with the site were at a loss as to why this would happen, so I started digging in.</p>
<p>After some digging and looking at the source code for the page I noticed that not only was jQuery being called twice (eesh) but mootools was being called as well.  The calls weren&#8217;t coming from the custom template that the site was using, they were coming from [dun dun dunnnn] INSIDE THE WORDPRESS CODE ITSELF!</p>
<p>After a little digging, I discovered that a Gallery plugin the client was using, NextGEN Smooth Gallery, was loading mootools to do some cool displays and animations for one of their image galleries.  The downside is that the call to mootools was on every page.</p>
<div id="attachment_175" class="wp-caption aligncenter" style="width: 410px"><a href="http://www.finalint.com/wp-content/uploads/2010/11/NextGENSmoothGallery.png"><img class="size-medium wp-image-175" title="NextGEN Smooth Gallery" src="http://www.finalint.com/wp-content/uploads/2010/11/NextGENSmoothGallery-400x34.png" alt="NextGEN Smooth Gallery" width="400" height="34" /></a><p class="wp-caption-text">The culprit</p></div>
<p>You see&#8230; both jQuery and mootools like to use the dollar sign ($) to denote shorthand for their calls.  While this can be pretty easily overcome manually by calling jQuery directly (using jQuery.yada) any plugins that you have for one of the specific frameworks will very likely use the shorthand notation somewhere in the code.  If they do, you&#8217;re screwed.</p>
<p>So let my couple of hours of research serve as a warning to those would be framework cross-mojinators out there.  It&#8217;s not worth it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.finalint.com/2010/11/29/jquery-mootools-and-nextgen-smooth-gallery-a-tale-of-failure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>10 Ways To NOT Suck At Programming</title>
		<link>http://www.finalint.com/2010/05/17/10-ways-to-not-suck-at-programming/</link>
		<comments>http://www.finalint.com/2010/05/17/10-ways-to-not-suck-at-programming/#comments</comments>
		<pubDate>Tue, 18 May 2010 00:09:59 +0000</pubDate>
		<dc:creator>Donnie</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.finalint.com/?p=144</guid>
		<description><![CDATA[Recently I wrote a therapeutic rant for myself, called &#8220;10 Ways To Suck At Programming&#8220;.  The article got syndicated to some link sharing sites and picked up quite a bit of traffic and comments.  The comments here on my site were, for the most part, nice.  Comments elsewhere were a mixed bag with many people saying [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I wrote a therapeutic rant for myself, called &#8220;<a title="10 Ways To Suck At Programming" href="http://www.finalint.com/2010/05/04/10-ways-to-suck-at-programming/">10 Ways To Suck At Programming</a>&#8220;.  The article got syndicated to some link sharing sites and picked up quite a bit of traffic and comments.  The comments here on my site were, for the most part, nice.  Comments elsewhere were a mixed bag with many people saying the sarcastic nature of the post wasn&#8217;t helpful.  While I didn&#8217;t see it as sarcastic (after all, it was quite literally telling you how to suck), and even though I&#8217;ve written many, many helpful technical articles that did nothing but collect dust, I figured I would take a shot at offsetting my previous rant with real steps programmers can follow to make themselves better.</p>
<p>Obviously, this is not a comprehensive list and most experienced programmers should already be well versed in what&#8217;s here, but it&#8217;s a decent start for newbies and probably a worthwhile reminder to some of the more experienced.</p>
<p>&#8230; and it will be interesting to see if an article intended to be helpful gets as much traffic as a 20 minute rant. ;)</p>
<h3>#10 Store settings in a configuration file</h3>
<p>Configuration files are a necessity in today&#8217;s programming world.  With most applications connecting to databases, e-mail servers, file shares and other outside resources it&#8217;s imperative that a good programmer store any information that has a possibility to change outside of the release cycle of the application in a configuration file.  This will allow the application&#8217;s settings to be changed without having to recompile (retest, redeploy) the application.</p>
<p>It is important, however, not to overuse configuration files.  Some things belong, some things don&#8217;t; and experience is really the best way to know which is which.  You can ask yourself, &#8220;Is this something that will likely change in the future?&#8221;  If the answer is yes, consider putting it in a configuration file.  The more often you can change the behavior of your application without having to recompile it, the more often you&#8217;ll look like a hero.</p>
<p>Examples of good information to store in a configuration file:</p>
<ul>
<li>Database connection strings</li>
<li>SMTP server information</li>
<li>Any standard to / from e-mail addresses</li>
<li>File upload / download / archive paths</li>
</ul>
<h3>#9 Scope and store your variables</h3>
<p>Databases are awesome tools and nowadays pretty much any application you&#8217;ll be asked to develop will use one of some sort.  However, when thinking of application performance database access and transactions are very expensive operations.  With that in mind a good programmer will access the database as few times as possible in order to fill the needs of the current application request.</p>
<p>One way to facilitate this noble goal is to store any information that your application needs in memory once it has been retrieved from the database.  A &#8220;get once, use often&#8221; approach can go a long way towards helping with performance (and to a lesser degree code readability).  Some information that is used so often that is even worth storing as a session variable in order to eliminate a request (database, disk, whatever) altogether.  Be careful with those, though, as they are often targeted for hacks and should never be solely depended on for functionality and should never contain any private information.</p>
<h3>#8 Never use arcane plugins</h3>
<p>Plugins and third-party code can make development go much, much faster.  Often times you can get more complete functionality by using them but there are trade-offs for this convenience.</p>
<ol>
<li>You have to learn to think like the person who wrote the plugin in order to use it</li>
<li>You usually don&#8217;t know how the internals work</li>
<li>They&#8217;re almost never meant for the exact situation you&#8217;re in</li>
</ol>
<p>With all that said, the right add-on or plugin can be a life saver and you must, must, must be careful when evaluating whether or not to use one.  Here are some questions to ask to help decide whether a plugin is right for your app:</p>
<ul>
<li>Does it do everything I need exactly as I need it right out of the box?</li>
<li>Is it well documented?</li>
<li>Does it have an active user and support community?</li>
<li>Has it been updated to coincide with major platform releases?</li>
<li>Is the documentation in your native language (yes, seriously)?</li>
<li>Will it take longer to write the functionality myself than learn to work with this plugin?</li>
<li>When I (or someone else) inevitably has to maintain or change this app in the future will it be easy to work with or around this plugin?</li>
</ul>
<p>If the answer to any of these questions is &#8220;no&#8221; you should be wary.</p>
<h3>#7 Always remove unneeded functionality</h3>
<p>This one kind of comes with a caveat:  every programmer should know to use a good and dependable source control tool.  Even if you are the only person working on the code, a good source control tool can (and likely will) prove to be a life saver at some point.  You&#8217;ll cruise along not even knowing you need it and then BAM &#8211; you implement sweeping code changes that break everything.  If you&#8217;re using a source control tool correctly, undoing your changes should be fairly quick and painless.  If not, you may have just lost a client or a job.</p>
<p>With that in mind, try to never, ever leave unneeded functionality in your code base.  If you have written a great piece of functionality that is no longer needed <em>delete it</em> <strong>immediately</strong>.</p>
<p>When you&#8217;re actually making changes that steer you away from existing functionality you understand <em>why</em> you no longer need the functionality and you likely have an idea of what else it touches.  When you come back in six months, after seeing two or three other projects, you will likely have <strong>NO</strong> idea why that functionality wasn&#8217;t previously removed.  You&#8217;ll think, &#8220;Boy, I didn&#8217;t remove this, something somewhere must be using it.&#8221;  This problem compounds itself when you&#8217;re forced to start writing code around undeleted, unnecessary functionality.  So in the end it&#8217;s easier to just remove code and keep your code base clean.</p>
<h3>#6 Always consider performance</h3>
<p>What makes a good programmer?  The ability to effectively juggle the following points and still deliver timely, effective solutions:</p>
<ul>
<li>Performance &#8211; is the code minimal and fast?</li>
<li>Scalability &#8211; is the code able to be expanded upon easily?</li>
<li>Manageability &#8211; how quickly can modifications be evaluated, estimated and made?</li>
</ul>
<p>The preference for which of these to focus on first differs from programmer to programmer and often from project to project.  This is part of what gives each programmer their individual style.  But <em>good</em> programmers always consider all three, no matter the situation.  Readability falls under manageability, abstraction falls under scalability and everything falls under performance.  So no matter what your programming personality and style is, you should be considering performance.</p>
<p>I&#8217;m not (necessarily) referring to spending hours pouring over 10 lines of code to make sure it executes as quickly as possible.  I&#8217;m talking about taking a solid fundamental approach to gathering and manipulating data that doesn&#8217;t do any one piece of functionality more times than absolutely necessary.  #9 above eludes to this, but to go a little further:</p>
<ul>
<li>Get your data once and use it in as many places as possible before disposing of it.</li>
<li>Have the absolute minimum amount of functionality inside loops.</li>
<li>Never perform accesses (disk, database, service) from within a loop unless absolutely necessary.</li>
<li>(In strongly typed languages) Know and use appropriate data types.</li>
<li>(In web apps) Perform as few server requests as possible.</li>
</ul>
<p>If you think of things like this from the beginning of your project you are less likely to have performance issues in the end.  If you don&#8217;t agree with me now that it&#8217;s worth the initial extra effort you will after you spend 4 weekends straight rewriting poor performing code because you tried to save 10 minutes on the front end.</p>
<h3>#5 Never nest major logic / functionality in loops</h3>
<p>I mentioned this one under heading #6 above, but it bears repeating.  Loops are often the heart of any application, but they will absolutely kill your app if used improperly.  So you must be wary of anything that falls into a loop.</p>
<p>Loops are something we have to use often, so every good programmer should know how to abstract functionality out of a loop (see #1 below for a simple example).  Besides the performance hits, tracing through functionality that is inside of a loop can be much more difficult because of all of the variables that come into play while looping.  Modern debuggers help tremendously with this task, but better tools shouldn&#8217;t excuse poor programming practices.</p>
<p>When you have time to review your code, start by reviewing loops and seeing if anything can be moved outside of them.</p>
<h3>#4 Document effectively</h3>
<p>There is a school of thought among some programmers that says, &#8220;Either you can read code or you can&#8217;t, why should I document it for you?&#8221;  And while this is true in theory, in practice adding appropriate documentation inside code makes it far, far easier to maintain.</p>
<p>Proper documentation is a skill and overdoing it is almost as big of an annoyance to those trying to maintain code as not doing it all.  Consider the following:</p>
<ul>
<li><strong>DO</strong> &#8211; denote reason and logic</li>
<li><strong>DO NOT</strong> &#8211; denote functionality or process</li>
</ul>
<p>What that means is:  tell the future programmer looking at this code <em>why</em> you did something, not necessarily how.  The &#8220;how&#8221; is the stuff they should usually be able to read on their own (unless you have to do some crazy, non-standard stuff).  More often when looking at the code they need to know &#8220;why&#8221; you did things a certain way.  Consider the following simple example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> var<span style="color: #008000;">.</span><span style="color: #0000FF;">length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    doSomething<span style="color: #008000;">&#40;</span>var<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Now let&#8217;s consider two commenting options:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// iterate over each var</span>
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> var<span style="color: #008000;">.</span><span style="color: #0000FF;">length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008080; font-style: italic;">// increment i if it's less than vars.length</span>
    doSomething<span style="color: #008000;">&#40;</span>var<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// do something with the var at position i</span>
<span style="color: #008000;">&#125;</span> <span style="color: #008080; font-style: italic;">// finish the loop</span></pre></td></tr></table></div>

<p>- or -</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> var<span style="color: #008000;">.</span><span style="color: #0000FF;">length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    doSomething<span style="color: #008000;">&#40;</span>var<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// mark inactive vars for deletion</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Now the first one tells you what is happening, about as pointlessly as a voice-over in a movie saying exactly what the characters on screen are doing.  The second one assumes that you know what is happening functionally and instead just tells you what&#8217;s going on logically, which is far more valuable information when you&#8217;re in &#8220;put out the fire of the forest variety&#8221; mode.  In the first case, the future programmer would need to look up doSomething(var) and figure out what it was accomplishing.  In the second version that information is at hand and valuable.</p>
<p>Which would you prefer if you got a call from an executive at 4:45PM on Friday that forced you to review this code?</p>
<h3>#3 Use effective and logical variable names</h3>
<p>There are a LOT of thoughts on this one and almost every programmer has a slightly different way of doing it.  So I&#8217;ll try to be ambiguous so we don&#8217;t start a war over it.</p>
<p>It is important to consider your variable names carefully.  Sometimes having the data type as part of the name is appropriate (intId) and sometimes that sort of thing can be inferred (id).  Either way, the name of the variable should hint towards the usage of the variable (note that &#8220;id&#8221; was in both).  Names like (vulSpok) and (humKirk) are not useful.</p>
<p>Now possibly just as important as choosing your style of naming variables carefully is being consistent.  As a programmer your code will have a certain &#8220;style&#8221; that others who inherit your code will have to learn in order to quickly and easily maintain your code.  That &#8220;style&#8221; may not be the same as theirs, but as long as it is consistent (and logical) you&#8217;ve done what you can do.</p>
<h3>#2 Catch errors, or don&#8217;t</h3>
<p>Let me start by saying that I know, academically, all errors should be caught and handled gracefully.  Let me also say that realistically, that almost never happens when deadlines are looming.</p>
<p>Hopefully the days of &#8220;Dr. Watson encountered an error&#8221; are behind us for good.  Most modern platforms will let applications die with a pretty decent error message.  So while it may not be ideal, letting errors fall through to the default error handling is better than manually mishandling the errors.  On internal or limited usage applications this can actually provide a pretty good idea of what the actual error is and where to start looking to fix it.</p>
<p>Now in an ideal world, programmers would have time to code a catch for anything that went wrong and do something cool, super-ninja style.  So if you have time and budget you should catch all possible errors and handle them.  If not, don&#8217;t catch them at all.</p>
<p>Just never, ever catch errors and do nothing.</p>
<h3>#1 Never duplicate functionality</h3>
<p>Reusable code is the holy grail of good programmers.  A good programmer should always be on the lookout for a way to write functionality once and then use it all over the place.  The benefits to this are too numerous to count.</p>
<p>So what is reusable code?</p>
<p>Let&#8217;s say, using our example above that you need to loop through your vars and doSomething() with them.  You could do it this way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> var<span style="color: #008000;">.</span><span style="color: #0000FF;">length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// mark inactive vars for deletion</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>var<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">isActive</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        var<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">readyToDelete</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #008000;">&#123;</span>
        var<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">readyToDelete</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>- or -</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> doSomething<span style="color: #008000;">&#40;</span>var<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>var<span style="color: #008000;">.</span><span style="color: #0000FF;">isActive</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        var<span style="color: #008000;">.</span><span style="color: #0000FF;">readyToDelete</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #008000;">&#123;</span>
        var<span style="color: #008000;">.</span><span style="color: #0000FF;">readyToDelete</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> var<span style="color: #008000;">.</span><span style="color: #0000FF;">length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    doSomething<span style="color: #008000;">&#40;</span>var<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// mark inactive vars for deletion</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>It ends up being 3 extra lines of code in this example that will save you 4 lines of code each time you use it somewhere else.  So the first time you re-use the code in this rudimentary example you&#8217;ve saved yourself 75% effort <em>across the board</em> (coding, debugging, refactoring, etc).  Pretty nifty, huh?</p>
<p>Additionally, if you ever need to change the evaluation logic to, say, make sure that there weren&#8217;t any records that may be orphaned by deletion, you could change that logic in a single place and it would work everywhere throughout your code.</p>
<p>If you have any major items to add, feel free to do so in the comments.  If I get enough good ones perhaps I&#8217;ll update this article to include them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.finalint.com/2010/05/17/10-ways-to-not-suck-at-programming/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>10 Ways To Suck At Programming</title>
		<link>http://www.finalint.com/2010/05/04/10-ways-to-suck-at-programming/</link>
		<comments>http://www.finalint.com/2010/05/04/10-ways-to-suck-at-programming/#comments</comments>
		<pubDate>Wed, 05 May 2010 00:32:31 +0000</pubDate>
		<dc:creator>Donnie</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rants]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.finalint.com/?p=133</guid>
		<description><![CDATA[I recently inherited a web app from a dirty, nasty, stinking contractor that claimed to be a competent enough programmer to be left alone to get things done. Unfortunately, we took him at his word. Functionally, most of the web app seemed to work at first glance. However, once the client took over the reins and [...]]]></description>
			<content:encoded><![CDATA[<p>I recently inherited a web app from a dirty, nasty, stinking contractor that claimed to be a competent enough programmer to be left alone to get things done. Unfortunately, we took him at his word. Functionally, most of the web app seemed to work at first glance. However, once the client took over the reins and actually started using it things went downhill <em>fast</em>.  The contractor disappeared after payment (die reputation DIE!) and I was left to try and get things working properly and performing up to snuff while the client limped along with what they had been given.</p>
<p>I decided to document a few of the things that I found wrong along the way.  These are really just things that every good programmer should already know to avoid&#8230; but obviously <strong>some</strong> people need to be reminded (or taught).</p>
<p><em>Update &#8211; 05.18.2010 &#8211; Some people thought that this article was too sarcastic.  While I thought it wasn&#8217;t sarcastic at all, it does exactly what the title promises and teaches people to suck, I&#8217;m always game for being constructive.  So I wrote an offset to this article &#8211; </em><a title="10 Ways To NOT Suck At Programming" href="http://www.finalint.com/2010/05/04/10-ways-to-suck-at-programming/"><em>10 Ways To NOT Suck At Programming</em></a><em>.</em></p>
<h3>#10 &#8211; Don&#8217;t store settings in a configuration file</h3>
<p>When you&#8217;re writing a sizable application, things like database connections and SMTP server information will be used throughout the app.  The best way to make sure your app is entirely immune to maintenance is to redefine those little bits of information every time you need them.  So instead of putting them in the configuration file (Web.config or whatever) just leave them in your compiled code.  Whoever inherits the app will thank you for sending them on a hunt through thousands of lines of code to change which SMTP server is being used.  What&#8217;s even more fun is when the next programmer only finds 14 of the 15 places where you&#8217;ve used this code and a single instance somewhere deep in the app silently breaks hundreds of times without anyone knowing.  Sometimes it&#8217;s helpful to build the variables in inconsistently concatenated strings. The repeated and more frequent interaction between the new developer and the disgruntled client will help strengthen their relationship.  And if you don&#8217;t hook up that love connection, who will?</p>
<h3>#9 &#8211; Don&#8217;t store variables in [any] memory scope</h3>
<p>One of the great things about databases is they store your bits of information and allow you to access them whenever you need them.  To make sure your app is as terrible as possible, you&#8217;ll want to be sure and access the database <em>every</em> time you need a bit of that information.  The more common the information is that&#8217;s needed, the bigger win you&#8217;ll have by making a new database connection to get that information.  Non-sensitive user information is a great use of this prinicple.  Don&#8217;t worry about defining a user&#8217;s information, such as &#8220;isAdmin&#8221; to a variable and using it throughout the current request.  Just query the database each time you need to know anything about the user.  After all, the client paid for that database, we&#8217;d better get as much spin out of it as possible!</p>
<h3>#8 &#8211; Use arcane plugins</h3>
<p>If the client has a non-standard request, such as formatting a table in a way that is outside the abilities of your WYSIWYG editor (colspan are hard).  You should definitely hit up the interwebs and search for random unsupported closed source plugins to do the work for you.  If you would spend almost an entire hour writing it yourself, you should spend at least 3 hours searching for and getting a plugin that does roughly but not exactly the same thing.  Bonus points if you can get a plugin that doesn&#8217;t do what you need, but offers 15MB+ of functionality you have no use for and include it without getting caught.  Bonus bonus points if the documentation for that plugin is in a language not native to your market (for example, if you&#8217;re working in an English speaking shop look for plugins that are documented only in Spanish or German).</p>
<h3>#7 &#8211; Never, ever remove functionality</h3>
<p>Over the course of developing a large application there are bound to be times when functionality you were working on proves itself to not be needed.  Now, to be sure to leave plenty of dead ends for those who come behind you to get lost in, never delete that functionality.  Maybe even comment out random parts of it, or even hundreds of lines of it at a time, but don&#8217;t delete it.  Imagine the hours of fun the future-crew for this app will have while they trace through this functionality only to find that it isn&#8217;t needed!  If you can make it seem needed, without actually needing it, it will even keep them from deleting it themselves&#8230;  This way the fun is exponential!  Oh, a bonus on this one&#8230; if your project uses source control and multiple server environments, be sure to have a different version of your files (both code and compiled) on each server and source control.  This way no one will know which one is live in production and who doesn&#8217;t love a good round of Code Russian Roulette?</p>
<h3>#6 &#8211; Screw performance</h3>
<p>Large applications, you know, the ones that pay the bills, are generally needed because they deal with large amounts of data.  Sure, during your development process you&#8217;ll create 20 or so test records.  Take my word for it, there&#8217;s no need to even worry about what happens once you get to 25 records, or even 1,000!  Obviously, all pagination will work just fine and performance will never take a hit.  So if it compiles, ship it!</p>
<h3>#5 &#8211; Nest major logic/functionality in loops</h3>
<p>Now obviously, we&#8217;ve already covered #6 so we know that we&#8217;re working with large amounts of data.  And inevitably, there will be plenty of looping through that data to do work with it.  If you want to make sure your application is really difficult to maintain and completely unusable to the client, you should embed major functionality and/or logic inside of these loops.  For example, instead of running a query against the database to get ALL the data you need, storing the data in memory and working with the variables in memory during your loop, just get all the data except one field up front and loop through it&#8230; then on each loop you should get all the data again, and now include your extra field.  This will guarantee that your app will never survive more than 5 concurrent users (re: #6).  So for review:  Get data &gt; create loop &gt; get data &gt; work with data.  I&#8217;m sure you see some room for added idiocy in that process, so feel free to nest this idea as many times as you want.</p>
<h3>#4 &#8211; Document NOTHING</h3>
<p>Look, documentation is for morons.  I mean either you can read the code or you can&#8217;t, right (this was actually said to me at one point)?  Of COURSE the next programmer will be able to read the code.  What&#8217;s really fun though, is when you document absolutely nothing, especially not your intent.  It pays to keep them guessing.  You&#8217;re mysterious, like a ninja.  No need to let someone get all up in your grill, knowing everything about what you were trying to do.  Because if you document what you&#8217;re trying to do and then don&#8217;t end up doing it&#8230; well&#8230; that&#8217;s just embarrassing.</p>
<h3>#3 &#8211; Use and reuse illogical variable names</h3>
<p>There&#8217;s going to be a LOT of variables needed to work on this app, so you&#8217;d better choose a movie or television show with enough characters to use all the names.  Lord of The Rings, Star Wars and Family Guy are all good choices for this.  Maybe you can even form relationships with the variables.  That way, you never have to kill them!  You can have variables that are chameleons, changing their usage and values throughout processing and you can recycle them for something new each time you need a variable for new functionality.  It&#8217;s like they&#8217;re growing up, evolving, right before your eyes!  After all, you&#8217;re trying to be green and reduce your carbon footprint so recycling variables just seems like the responsible thing to do!</p>
<h3>#2 &#8211; Catch all errors &#8212; and do nothing with them</h3>
<p>Most languages / platforms nowadays have really good error handling built in.  They&#8217;ll die somewhat gracefully and give enough details through the default error output to be helpful.  You must not allow this to happen!  Start off by wrapping nearly every tiny piece of functionality in a try / catch phrase.  Then&#8230; inside the catch&#8230; put a comment, like &#8220;// It borked lawl.&#8221;  This will ensure that if anyone wants to work on this app, they&#8217;d better spend the time getting to know the app and it&#8217;s adorable character variables before they start wrecking the app altogether.</p>
<h3>#1 &#8211; Duplicate functionality</h3>
<p>If the client tells you that they need two pages:  One for an administrator that has all of the details on an item along with a button to delete it; and one for a regular user which has all of the details on an item without a button, you should create multiple pages to accomplish this.  In fact, if you can make pages for each user group that would even be better.  Making a separate page for each user is the ultimate success.  So ninja up and get serious on this one, because it&#8217;s your last line of defense against the teeming hordes of qualified individuals who will inevitably be bewildered while trying to update your carefully constructed Pandora&#8217;s box of an app.</p>
<p>This is by no means a comprehensive list.  On this project alone I could name 10 more things that could make you suck.  But I&#8217;ll leave it at 10 for now.  Anyone have more to add?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.finalint.com/2010/05/04/10-ways-to-suck-at-programming/feed/</wfw:commentRss>
		<slash:comments>122</slash:comments>
		</item>
		<item>
		<title>Google Calendar .NET API &#8211; HTTP 417 Error</title>
		<link>http://www.finalint.com/2010/04/23/google-calendar-net-api-http-417-error/</link>
		<comments>http://www.finalint.com/2010/04/23/google-calendar-net-api-http-417-error/#comments</comments>
		<pubDate>Fri, 23 Apr 2010 14:47:37 +0000</pubDate>
		<dc:creator>Donnie</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.finalint.com/?p=129</guid>
		<description><![CDATA[I never even knew what a 417 error was until today. Apparently it&#8217;s an &#8220;Expectation Failed&#8221; error.  It&#8217;s rare, to say the least.  In my 15+ years of working with HTTP I&#8217;ve never come across it.  I had to do some research to get this sorted out and while the information needed to diagnose and [...]]]></description>
			<content:encoded><![CDATA[<p>I never even knew what a 417 error was until today.  Apparently it&#8217;s an <a title="HTTP 417 Error" href="http://www.checkupdown.com/status/E417.html" target="_blank">&#8220;Expectation Failed&#8221; error</a>.  It&#8217;s rare, to say the least.  In my 15+ years of working with HTTP I&#8217;ve never come across it.  I had to do some research to get this sorted out and while the information needed to diagnose and fix the problem existed elsewhere, it didn&#8217;t exist in one place.  So I&#8217;m somewhat consolidating here and trying to provide links back to my original references.</p>
<h3>What&#8217;s The History?</h3>
<p>So here&#8217;s the skinny on the issue.  There is a piece of an HTTP request called &#8220;Expect-100&#8243; which tells a client making a request to first confirm that things are working, and if so to continue with the rest of the request.  The response from the server if everything is working is a response of &#8220;100&#8243;.</p>
<p>Now technically, you&#8217;re only supposed to send &#8220;Expect-100&#8243; as true if the server is prepared to handle it [<a title="reference" href="http://haacked.com/archive/2004/05/15/http-web-request-expect-100-continue.aspx" target="_blank">reference</a>].  Depending on the server setup, this may or may not break things.  With more focus on web services, there&#8217;s more focus on the intricate settings that go into serving them (it&#8217;s not all just web browsers and strawberry jam nowadays, y&#8217;know).</p>
<h3>What&#8217;s The Issue?</h3>
<p>Since there is more focus on making sure things are done correctly, many services (Twitter and Google&#8217;s APIs are among them &#8211; [<a title="Twitter API 417 Issues" href="http://groups.google.com/group/twitter-development-talk/browse_thread/thread/7c67ff1a2407dee7" target="_blank">reference</a>]) have decided that their HTTP requests should be properly formed.  With the amount of traffic they generate, who can blame them.  However, Microsoft&#8217;s .NET by default includes &#8220;Expect-100&#8243; as &#8220;true&#8221; in all of its requests.  So using .NET to do something with those API&#8217;s can cause a nasty and hard to track down 417 error.  Fiddler to the rescue, right?</p>
<h3>What&#8217;s The Fix?</h3>
<p>Luckily, the fix is pretty easy once you figure out how to do it.  Before you make a service request, just add this line of code (C#):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">System.<span style="color: #0000FF;">Net</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">ServicePointManager</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Expect100Continue</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>[<a title="reference" href="http://groups.google.com/group/twitter-development-talk/browse_thread/thread/7c67ff1a2407dee7" target="_blank">reference</a>]</p>
<p>That should fix things right up.  Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.finalint.com/2010/04/23/google-calendar-net-api-http-417-error/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Making jQuery and ASP.NET AJAX Play Well Together</title>
		<link>http://www.finalint.com/2010/04/11/making-jquery-and-asp-net-ajax-play-well-together/</link>
		<comments>http://www.finalint.com/2010/04/11/making-jquery-and-asp-net-ajax-play-well-together/#comments</comments>
		<pubDate>Sun, 11 Apr 2010 21:22:41 +0000</pubDate>
		<dc:creator>Donnie</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.finalint.com/?p=102</guid>
		<description><![CDATA[This one caused me a few headaches tonight, so I figured I could throw something on the blog in the hopes that Google would lead someone to what turned out to be a less than logical solution, but a solution. ;) First, the setup:  Basically, I have a page that was written by someone else [...]]]></description>
			<content:encoded><![CDATA[<p>This one caused me a few headaches tonight, so I figured I could throw something on the blog in the hopes that Google would lead someone to what turned out to be a less than logical solution, but a solution. ;)</p>
<p>First, the setup:  Basically, I have a page that was written by someone else and that person had an affinity for the Microsoft ASP AJAX Control Toolkit (I have no idea why).  Well me, I have an affinity for jQuery.  So I set about building new functionality on the existing page in jQuery, while trying to keep the functionality that was already there untouched and in tact.  That turned out to be more difficult than expected.</p>
<p>I&#8217;m sure there are tons of issues to work through on this and I may update this article as those arise.  But first up I had to figure out why the standard jQuery $(document).ready() functionality wasn&#8217;t working when there was, most obviously, a PostBack occurring.  And, as it turns out, there&#8217;s a sort of fake PostBack that occurs when using the AJAX.NET toolkit.  Lame.  But here&#8217;s how to handle it (full code description follows):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="dot" style="font-family:monospace;"><span style="color: #66cc66;">&lt;%</span>@ <span style="color: #000066;">Page</span> Language<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;C#&quot;</span> AutoEventWireup<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;true&quot;</span>  CodeFile<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Default.aspx.cs&quot;</span> Inherits<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;_Default&quot;</span> <span style="color: #66cc66;">%&gt;</span>
&nbsp;
<span style="color: #66cc66;">&lt;!</span>DOCTYPE html PUBLIC <span style="color: #ff0000;">&quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span> <span style="color: #ff0000;">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;</span><span style="color: #66cc66;">&gt;</span>
&nbsp;
<span style="color: #66cc66;">&lt;</span>html xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span><span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>head runat<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;server&quot;</span><span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>title<span style="color: #66cc66;">&gt;</span>Untitled Page<span style="color: #66cc66;">&lt;/</span>title<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>script src<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;scripts/jquery-1.4.2.min.js&quot;</span> type<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #66cc66;">&gt;&lt;/</span>script<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>script type<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #66cc66;">&gt;</span>
		$<span style="color: #66cc66;">&#40;</span>document<span style="color: #66cc66;">&#41;</span>.ready<span style="color: #66cc66;">&#40;</span>function<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			EndRequestHandler<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		function load<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			Sys.WebForms.PageRequestManager.getInstance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.add_endRequest<span style="color: #66cc66;">&#40;</span>EndRequestHandler<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		function EndRequestHandler<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// do page load things</span>
		<span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&lt;/</span>script<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>head<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>body onload<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;load()&quot;</span><span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>form <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;form1&quot;</span> runat<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;server&quot;</span><span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>div<span style="color: #66cc66;">&gt;</span>
&nbsp;
    <span style="color: #66cc66;">&lt;/</span>div<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;/</span>form<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>body<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>html<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<h3>The Breakdown</h3>
<p>First, you need to create a client side (JavaScript) function to execute whenever a page is &#8220;loaded&#8221; (I put it in quotes because the load event sometimes fires and sometimes doesn&#8217;t when using AJAX.NET).  You need the two layers of abstraction here so the function can be called in the multiple scenarios that are created by a real page load and an AJAX.NET partial page load:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> EndRequestHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// do page load things</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now, once you have created that function you need to call it from both the jQuery and ASP.NET client side load events.</p>
<p>First up, the standard jQuery call will work pretty much as expected:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	EndRequestHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The ASP.NET version requires that you make another function and attach the first function to the client event&#8230; Yeah, I know, it&#8217;s wonky.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> load<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	Sys.<span style="color: #660066;">WebForms</span>.<span style="color: #660066;">PageRequestManager</span>.<span style="color: #660066;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add_endRequest</span><span style="color: #009900;">&#40;</span>EndRequestHandler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Once you&#8217;ve done all the jumping through flaming hoops, you can just attach the function to the body tag&#8217;s onload event and everything will be wired up and ready to go.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="dot" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>body onload<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;load()&quot;</span><span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.finalint.com/2010/04/11/making-jquery-and-asp-net-ajax-play-well-together/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How To Have Multiple Domains Using 1&amp;1</title>
		<link>http://www.finalint.com/2010/04/09/how-to-have-multiple-domains-using-11/</link>
		<comments>http://www.finalint.com/2010/04/09/how-to-have-multiple-domains-using-11/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 20:37:50 +0000</pubDate>
		<dc:creator>Donnie</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.finalint.com/?p=119</guid>
		<description><![CDATA[If you read my other post, you know about the problems I have had getting a client set up with multiple domains hosted under the same 1&#38;1 account. 1&#38;1&#8242;s decision to market their hosting as &#8220;have as many domains as you want&#8221; is misleading at best, because they require that each domain point to the [...]]]></description>
			<content:encoded><![CDATA[<p>If you read my other post, you know about the problems I have had getting a client set up with multiple domains hosted under the same 1&amp;1 account.  1&amp;1&#8242;s decision to market their hosting as &#8220;have as many domains as you want&#8221; is misleading at best, because they require that each domain point to the same place.  You have to handle ALL multiple domain requests using a coded solution.  This isn&#8217;t ideal for far too many reasons to list here, but I figured I could offer someone some help if they are running into the same problem.</p>
<h3>Pre-Conditions</h3>
<ol>
<li>First you have to have all of your files set up correctly.  The correct way is to have a sub-directory for each site that you want to host under the account.  You may have better luck, but I couldn&#8217;t get directory names with dots in them to work (just another thing on the long list).</li>
<li>Once you have your files set up, you need to make sure that each site&#8217;s domain is pointed to the account root.  Not the sub-directory that you have created for the site.</li>
</ol>
<h3>Primary Redirect File</h3>
<p>Now, in the account root directory, you need a file called default.asp that will include code similar to the following:</p>

<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="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span>EnableSessionState<span style="color: #006600; font-weight: bold;">=</span><span style="color: #0000ff; font-weight: bold;">False</span>
  host <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #990099; font-weight: bold;">Request</span>.<span style="color: #330066;">ServerVariables</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;HTTP_HOST&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color: #990099; font-weight: bold;">if</span> host <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;domain1.com&quot;</span> <span style="color: #0000ff; font-weight: bold;">or</span> host <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;www.domain1.com&quot;</span> <span style="color: #990099; font-weight: bold;">then</span>
    <span style="color: #990099; font-weight: bold;">response</span>.<span style="color: #330066;">redirect</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;http://domain1.com/domain1-directory/default.aspx&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
  <span style="color: #990099; font-weight: bold;">elseif</span> host <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;domain2.com&quot;</span> <span style="color: #0000ff; font-weight: bold;">or</span> host <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;www.domain2.com&quot;</span> <span style="color: #990099; font-weight: bold;">then</span>
    <span style="color: #990099; font-weight: bold;">response</span>.<span style="color: #330066;">redirect</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;http://domain2.com/domain2-directory/index.shtml&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
  <span style="color: #990099; font-weight: bold;">end</span> <span style="color: #990099; font-weight: bold;">if</span>
<span style="color: #000000; font-weight: bold;">%&gt;</span></pre></td></tr></table></div>

<div>Your sites will now technically work.  But wait, there&#8217;s more!  What if you want to have custom error pages for each site?</div>
<h3>Custom Error Pages</h3>
<div>Of course, there&#8217;s more hoops to jump through.  This can&#8217;t be done on the server side because the default IIS error files are .html files.  So you basically have to fool IIS into using your files by naming them the name that IIS uses by default.  Then, you have to use code similar to the following to make sure sites redirect whenever an error page is hit.  Yes, I know this isn&#8217;t ideal, but the entire account is now a hack because 1&amp;1 sucks, so I&#8217;ll live with it until I convince everyone to abandon 1&amp;1.</div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!</span>DOCTYPE HTML <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD HTML 4.01//EN&quot;</span> <span style="color: #0000ff;">&quot;http://www.w3.org/TR/html4/strict.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>meta http<span style="color: #339933;">-</span>equiv<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=iso-8859-1&quot;</span><span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>Error<span style="color: #339933;">!&lt;/</span>title<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>location<span style="color: #339933;">.</span>hostname <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;domain1.com&quot;</span> <span style="color: #339933;">||</span> location<span style="color: #339933;">.</span>hostname <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;www.domain1.com&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				window<span style="color: #339933;">.</span>location <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.domain1.com/domain1-directory/404.shtml&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>location<span style="color: #339933;">.</span>hostname <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;domain2.com&quot;</span> <span style="color: #339933;">||</span> location<span style="color: #339933;">.</span>hostname <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;www.domain2.com&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				window<span style="color: #339933;">.</span>location <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://domain2.com/domain2-directory/error404.html&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span>
	<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<h3>After All&#8230;</h3>
<div>While my preference would be to never have to deal with this crazy hack-happy format for having an account with multiple domains, I will undoubtedly run into this again.  Who knows, maybe someday you will to and this article will prove helpful.  I guess, in the end our world is all about hacking solutions into place, right?  ;)</div>
]]></content:encoded>
			<wfw:commentRss>http://www.finalint.com/2010/04/09/how-to-have-multiple-domains-using-11/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Google Chrome Launches</title>
		<link>http://www.finalint.com/2008/09/02/google-chrome-launches/</link>
		<comments>http://www.finalint.com/2008/09/02/google-chrome-launches/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 01:49:26 +0000</pubDate>
		<dc:creator>Donnie</dc:creator>
				<category><![CDATA[Reviews]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Google Chrome]]></category>

		<guid isPermaLink="false">http://www.finalint.com/?p=44</guid>
		<description><![CDATA[While it is admittedly early to be talking about a full-blown review, I&#8217;d like to at least take a moment to discuss one of the most significant browser releases in recent history. As someone who has spent the last 13 years writing web applications I&#8217;ve seen first hand the path modern browsers have taken to [...]]]></description>
			<content:encoded><![CDATA[<p>While it is admittedly early to be talking about a full-blown review, I&#8217;d like to at least take a moment to discuss one of the most significant browser releases in recent history.</p>
<p>As someone who has spent the last 13 years writing web applications I&#8217;ve seen first hand the path modern browsers have taken to get to where they are.  Knowing what I know about this path, I can also say that the journey has been a long one full of good intentions and without much concern for developers (or standards).  That&#8217;s why I&#8217;m so impressed with what Google is doing here.</p>
<p>Apparently Google &#8220;accidentally&#8221; sent a notification about their browser intentions a day early, resulting in a frenzy around <a title="Chrome Comic" href="http://www.google.com/googlebooks/chrome/" target="_blank">their creative and informative announcement &#8220;comic.&#8221;</a></p>
<p>Then, today, they officially released the browser that was the source of all of the discussion.  With their claims of speed, performance and standards compliance I was skeptical.  After all, like I said I&#8217;ve been fed the &#8220;we made it faster&#8221; line a thousand times over the past 13 years and I&#8217;ve almost never been impressed.  Sure, you can benchmark browsers and prove a 3% increase in speed but what does that buy me in the real world?</p>
<p>When we&#8217;re talking about Chrome, however, we&#8217;re talking about insanely fast.  Let me be clear, I&#8217;ve developed a LOT of applications for intranet usage (internal company networks) and I can say that the &#8220;slowness&#8221; often felt while using web pages is not a result of bandwidth limitations or slow downloading.  Instead, it is a latency experienced while the inefficient browser engines parse through the code needed to create a web page.  This is even more obvious when the page has complex (read:  useful) JavaScript in place.  All that is to say, Chrome is so fast it does away with the expectations you have about using web pages.</p>
<p>I&#8217;ve read where some folks had problems with pages, but I&#8217;ve looked at all of the ones I am responsible for and found no issues whatsoever.  This is probably because I test them in Safari, which uses the same engine for rendering as Chrome.</p>
<p>So for now, those are my thoughts.  I&#8217;ll post more here if things go insanely awry, but I plan to use Chrome as my primary browser at home for a while to see what I run into.  Oh&#8230; and I posted this using Chrome on WordPress, so we know that works!</p>
<p>More Chrome Information:  <a href="http://tools.google.com/chrome/intl/en/features.html?hl=en-US">http://tools.google.com/chrome/intl/en/features.html</a></p>
<p>Download Chrome:  <a href="http://tools.google.com/chrome/?hl=en-US">http://tools.google.com/chrome/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.finalint.com/2008/09/02/google-chrome-launches/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DOCTYPE affects iframe scrollbar styles</title>
		<link>http://www.finalint.com/2007/09/18/doctype-effects-iframe-scrollbar-styles/</link>
		<comments>http://www.finalint.com/2007/09/18/doctype-effects-iframe-scrollbar-styles/#comments</comments>
		<pubDate>Tue, 18 Sep 2007 21:16:54 +0000</pubDate>
		<dc:creator>Donnie</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.finalint.com/2007/09/18/doctype-effects-iframe-scrollbar-styles/</guid>
		<description><![CDATA[An application I am currently working on has been specifically targeted towards Internet Explorer and a technical design requirement is that the browser window the application is being run in is a fixed size. So inevitably, there will be scrolling. As part of the design we decided to use a document divided up into sections [...]]]></description>
			<content:encoded><![CDATA[<p>An application I am currently working on has been specifically targeted towards Internet Explorer and a technical design requirement is that the browser window the application is being run in is a fixed size. So inevitably, there will be scrolling.</p>
<p>As part of the design we decided to use a document divided up into sections using <code>DIV</code> tags and absolutely positioning rather than using frames all around. This leaves the primary content area of the application as an <code>iframe</code> taking up the majority of the page.</p>
<p>A week or so ago I put some styles in my CSS which were intended to color the scrollbars on the <code>iframe</code>. Oddly&#8230; this didn&#8217;t work. I didn&#8217;t really attempt to follow up on it until today, at which point I found an interesting solution.</p>
<p>Before, I had the following document definition in my HTML:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!</span>DOCTYPE HTML <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD HTML 4.01//EN&quot;</span> <span style="color: #0000ff;">&quot;http://www.w3.org/TR/html4/strict.dtd&quot;</span><span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>I changed the definition to:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!</span>DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span> <span style="color: #0000ff;">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;</span><span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>And now things work great. I&#8217;m not sure why, maybe when I get some time to look it up I&#8217;ll modify this post to reflect whatever reasons I find.</p>
<p>However, if you are having problems with your iframe scrollbars not taking your styles, this could help.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.finalint.com/2007/09/18/doctype-effects-iframe-scrollbar-styles/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Assigning a CSS class to a dynamically created DOM element</title>
		<link>http://www.finalint.com/2007/08/27/assigning-a-css-class-to-a-dynamically-created-dom-element/</link>
		<comments>http://www.finalint.com/2007/08/27/assigning-a-css-class-to-a-dynamically-created-dom-element/#comments</comments>
		<pubDate>Mon, 27 Aug 2007 18:05:36 +0000</pubDate>
		<dc:creator>Donnie</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.finalint.com/2007/08/27/assigning-a-css-class-to-a-dynamically-created-dom-element/</guid>
		<description><![CDATA[I recently discovered that Internet Explorer and Firefox don&#8217;t agree on the possibilities of assigning a CSS class to a dynamically created DOM element (created via JavaScript) using the &#8220;setAttribute&#8221; method. It took me a little time to figure out what was going on and how to fix it, so in the hopes that I [...]]]></description>
			<content:encoded><![CDATA[<p>I recently discovered that Internet Explorer and Firefox don&#8217;t agree on the possibilities of assigning a CSS class to a dynamically created DOM element (created via JavaScript) using the &#8220;setAttribute&#8221; method.  It took me a little time to figure out what was going on and how to fix it, so in the hopes that I can save someone else some time I decided to provide this post.</p>
<p>First, I should outline what I was trying to accomplish.  I have a need for a web page which has completely dynamic display properties.  During the use of the page many of the DOM elements will behave according to the user&#8217;s input, so I need to have each of them accessible in JavaScript.  The goal is to have a page which can adjust, update and move all of its elements without ever reloading the page or any data which has already been loaded.  The page is being written using .NET, JavaScript, (D)HTML and CSS.</p>
<p>Now, for the approach I decided to take.  Since JavaScript will need to have access to all the elements anyhow, I decided the better approach would be to have a JavaScript object for each &#8220;module&#8221; that will exist in the page.  This object carries some details about the object as well as a reference to a DOM object that will be associated with each module.  The goal here is to keep from duplicating code, so I either write the DOM object in the HTML (and use server side logic to manage it) or I write it in JavaScript (and use the client&#8217;s memory to manage it), but not both.</p>
<p>My page is made up of three files, I&#8217;ve simplified them for illustrative purposes below&#8230;</p>
<p>My JavaScript source file, <code>script.js</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">d <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
d<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Display<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Header&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Header&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> initializeDisplays<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> d.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    container <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;FrameworkContainer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    d<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">dome</span> <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    d<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">dome</span>.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">,</span> d<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    d<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">dome</span>.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;class&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;moduleBody&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    d<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">dome</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> d<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #000066;">name</span><span style="color: #339933;">;</span>
    container.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">dome</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> Display<span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #000066;">name</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>My CSS stylesheet, <code>style.css</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.moduleBody</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span>  Green<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>And finally, my HTML file, <code>test.html</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!</span>DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD XHTML 1.1//EN&quot;</span> <span style="color: #0000ff;">&quot;http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html xmlns<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.w3.org/1999/xhmtl&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>Test Page<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;script.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>link href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;style.css&quot;</span> rel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;stylesheet&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/css&quot;</span> <span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>body onload<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;initializeDisplays();&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;FrameworkContainer&quot;</span><span style="color: #339933;">&gt;&lt;/</span>div<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Now this code works fine in Firefox, the new <code>div</code> is loaded into the document as expected and the text is green, as expected.  However, using the same code in IE provides slightly different results.  The <code>div</code> is loaded as expected, but no styles are applied to it.  The reason, is that IE doesn&#8217;t support the following line of code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">d<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">dome</span>.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;class&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;moduleBody&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Instead, IE demands you use the following syntax:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">d<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">dome</span>.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;moduleBody&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Is it a big deal?  No, not really.  But it can still be very, very frustrating to find that IE has issues when applying styles to a dynamically created element which uses the consistent approach of <code>setAttribute</code> as a means to set a style class.  You will probably find that the <code>className</code> property is the recommended approach in tutorials and will find that it works in most browsers, but being a stickler for consistency I tried a different approach and ended up with this article to show for it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.finalint.com/2007/08/27/assigning-a-css-class-to-a-dynamically-created-dom-element/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

