<?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>Ruelicke.net &#187; JavaScript</title>
	<atom:link href="http://www.ruelicke.net/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ruelicke.net</link>
	<description>...when coding passion, gaming addiction, Internet surfing syndrome and the real life collide...</description>
	<lastBuildDate>Mon, 05 Apr 2010 17:11:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JavaScript &#8211; Playing with a Form</title>
		<link>http://www.ruelicke.net/2007/09/09/javascript-playing-with-a-form/</link>
		<comments>http://www.ruelicke.net/2007/09/09/javascript-playing-with-a-form/#comments</comments>
		<pubDate>Sun, 09 Sep 2007 17:26:24 +0000</pubDate>
		<dc:creator>Marco</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.ruelicke.net/2007/09/javascript-playing-with-a-form/</guid>
		<description><![CDATA[I assume you already saw one of those forms which show something like &#8220;Enter query&#8221; in one of the input fields and as soon as you enter a letter or press a key on the keyboard, the &#8220;Enter query&#8221; vanishes.
As I needed such a feature for a project of mine, I decided to write this [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.ruelicke.net/wp-content/themes/ruelicke/images/icon-js.png" alt="JS - JavaScript" title="JS - JavaScript" class="alignleft" /><span class="c_letter">I</span> assume you already saw one of those forms which show something like &#8220;Enter query&#8221; in one of the input fields and as soon as you enter a letter or press a key on the keyboard, the &#8220;Enter query&#8221; vanishes.</p>
<p><span class="c_letter">A</span>s I needed such a feature for a project of mine, I decided to write this tutorial to share a couple of techniques you can use for such an interesting form.</p>
<h3><a href="http://www.ruelicke.net/example/javascript/tutorial02.html#form-1">Form #1</a></h3>
<p><span class="c_letter">T</span>he first form has the mentioned feature that removes the text in the input field as soon as you hit a key:<span id="more-123"></span><br />
<code style="text-align:left;">&lt;script type="text/javascript"&gt;<br />
&lt;!--<br />
function clear_input($input)<br />
{<br />
if (document.getElementById($input).value == "Name" || document.getElementById($input).value == "Password")<br />
{<br />
document.getElementById($input).value = '';<br />
};<br />
}<br />
window.onload = document.getElementById("name-1").value="Name";<br />
window.onload = document.getElementById("password-1").value="Password";<br />
//--&gt;<br />
&lt;/script&gt;</code></p>
<p><span class="c_letter">P</span>ut this code at the bottom of the page, <em>replace the getElementByID values and IDs with the ones you are using</em>, then add the event <code style="text-align:left;">onkeypress=""</code> to the input box you want to have using this feature. Inside the <code style="text-align:left;">onkeypress=""</code> you add <code>clear_input('id of the input field');return true;</code>.<br />
For <a href="http://www.ruelicke.net/example/javascript/tutorial02.html#form-1">my example</a> it would be for the &#8220;name&#8221; input field: <code>onkeypress=" clear_input('name-1'); return true;"</code></p>
<p><span class="c_letter">T</span>he &#8220;return true&#8221; is a bug fix for some browsers which won&#8217;t clear the input field as we want it.</p>
<h3><a href="http://www.ruelicke.net/example/javascript/tutorial02.html#form-2">Form #2</a></h3>
<p><span class="c_letter">T</span>his form will check the input data before it submits it:<br />
<code style="text-align:left;">&lt;script type="text/javascript"&gt;<br />
&lt;!--<br />
function check_input()<br />
{<br />
if (document.getElementById("name-2").value != "Name" &#038;&#038; document.getElementById("name-2").value != "" &#038;&#038; document.getElementById("password-2").value != "Password" &#038;&#038; document.getElementById("password-2").value != "")<br />
{<br />
return true;<br />
}<br />
else<br />
{<br />
return false;<br />
}<br />
}<br />
function clear_input($input)<br />
{<br />
if (document.getElementById($input).value == "Name" || document.getElementById($input).value == "Password")<br />
{<br />
document.getElementById($input).value = '';<br />
};<br />
}<br />
window.onload = document.getElementById("name-2").value="Name";<br />
window.onload = document.getElementById("password-2").value="Password";<br />
//--&gt;<br />
&lt;/script&gt;</code></p>
<p><span class="c_letter">A</span>s you see I&#8217;m using the <code>clear_input()</code> function again and added a <code>check_input</code> function. Add <code>onsubmit="return check_input;"</code> to the opening form tag and make sure you are using the right IDs in your script.</p>
<p><span class="c_letter">I</span>f everything is working, your <a href="http://www.ruelicke.net/example/javascript/tutorial02.html#form-2">form should behave like mine</a>.</p>
<h3>Wrap Up</h3>
<p><span class="c_letter">I</span> hope this tutorial gave you an idea or two on how to improve your form a little bit. An idea which just pops up right now is to combine the <code>clear_input</code> method with Ajax to provide search suggestions, if it is a search form. Or you add a check_mail variant which validate an inserted email on the fly.</p>
<p><span class="c_letter">T</span>here are many options. Do you know a special one and have no idea how to achieve it? Or do you want to share an idea or script? I&#8217;m happy about any kind comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ruelicke.net/2007/09/09/javascript-playing-with-a-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript &#8211; Hello World</title>
		<link>http://www.ruelicke.net/2007/09/03/javascript-hello-world/</link>
		<comments>http://www.ruelicke.net/2007/09/03/javascript-hello-world/#comments</comments>
		<pubDate>Mon, 03 Sep 2007 10:00:11 +0000</pubDate>
		<dc:creator>Marco</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.ruelicke.net/2007/09/javascript-hello-world/</guid>
		<description><![CDATA[Unlike my PHP and HTML tutorials this tutorial will be different.
Although I know JavaScript I don&#8217;t use it often because of a few reasons. So I&#8217;m using this &#8220;tutorial&#8221; to
1. refresh my knowledge about JavaScript
and
2. teach you the one or the other useful thing.
Let&#8217;s begin
For this &#8220;tutorial&#8221; you should have some basic understanding about HTML [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.ruelicke.net/wp-content/themes/ruelicke/images/icon-js.png" alt="JS - JavaScript" title="JS - JavaScript" class="alignleft" /><span class="c_letter">U</span>nlike my <a href="http://www.ruelicke.net/category/tutorial/php/">PHP</a> and <a href="http://www.ruelicke.net/category/tutorial/html/">HTML</a> tutorials this tutorial will be different.</p>
<p><span class="c_letter">A</span>lthough I know JavaScript I don&#8217;t use it often because of a few reasons. So I&#8217;m using this &#8220;tutorial&#8221; to<br />
<strong>1. refresh my knowledge about JavaScript</strong><br />
and<br />
<strong>2. teach you the one or the other useful thing.</strong></p>
<h3>Let&#8217;s begin</h3>
<p><span class="c_letter">F</span>or this &#8220;tutorial&#8221; you should have some basic understanding about <a href="http://www.ruelicke.net/category/tutorial/html/">HTML</a> and CSS, later it could be useful to know <a href="http://www.ruelicke.net/category/tutorial/php/">PHP</a>, but that&#8217;s some time later&#8230;<span id="more-116"></span></p>
<p><strong><span class="c_letter">T</span>o use JavaScript in an HTML document</strong> you have 2 methods:</p>
<ul>
<li>Using an external JavaScript file.</li>
<li>Having the JavaScript code right on the page.</li>
</ul>
<p><span class="c_letter">W</span>hich method you use is up to you but at the end you need to use more or less same technique to include the JavaScript in the HTML page.<br />
To embed a JavaScript you are using:<br />
<code style="text-align:left;"><br />
&lt;script type="text/javascript"  src="path/to/file"&gt;&lt;/script&gt;<br />
</code><br />
<strong><span class="c_letter">T</span>he above example is used when you have an external javascript file.</strong> Otherwise you will be using this code:<br />
<code style="text-align:left;"><br />
&lt;script type="text/javascript"&gt;<br />
&lt;!--<br />
<em>...javascript code...</em><br />
//--&gt;<br />
&lt;/script&gt;<br />
</code></p>
<p><span class="c_letter">Y</span>ou place this HTML code either in the &lt;head&gt; or &lt;body&gt; part of the page, then you are ready to write your first Javascript script.</p>
<h3>Hello World!</h3>
<p><span class="c_letter">W</span>e want to write our first script and we want to execute it as soon as the page is loaded. The JavaScript event handler we need for this is onload() although we could also do it in a different way.</p>
<p><span class="c_letter">O</span>ur &#8220;hello world&#8221; script will bring up an alert box with the message &#8220;Hello world!&#8221;:<br />
<code style="text-align:left;">&lt;script type="text/javascript"&gt;<br />
&lt;!--<br />
function hello_world()<br />
{<br />
alert("Hello world!");<br />
}<br />
//--&gt;<br />
&lt;/script&gt;<br />
</code><br />
<em>Note: If you are using an external file, just take the javascript inside the html comments and put it into the external file.</em></p>
<p><span class="c_letter">W</span>e got our first function and now we need to call it somehow. As I said already, we will be using the onload() handler.<br />
To make sure that the code works as intended, you have to put the code in the &lt;head&gt; part of your HTML page.<br />
Now add <code>onload="hello_world();"</code> in the opening body tag. Save the page and load it in a browser.<br />
To have a comparison, <a href="javascript:alert('Hello%20world!');">try my &#8220;Hello world&#8221; alert</a>.</p>
<p><strong><span class="c_letter">T</span>he <code>onload()</code> handler is useful, but is often abused.</strong> I recommend you only use it when you have to. Same applies for the <code>alert()</code> message.</p>
<h3>Better &#8220;Hello World!&#8221;</h3>
<p><span class="c_letter">D</span>epending on the amount of your JavaScript code, the page needs longer to load. If you put the code into the head, it will take some time before the visitors see any content of your page. <strong>It is recommended to put the JavaScript into the foot part of the body.</strong><br />
<span class="c_letter">I</span>f you try it, you will see that <code>onload()</code> will not work, so we have to use a different approach:<br />
<code style="text-align:left;">&lt;script type="text/javascript"&gt;<br />
&lt;!--<br />
function hello_world()<br />
{<br />
alert("Hello world!");<br />
}<br />
window.onload = hello_world();<br />
//--&gt;<br />
&lt;/script&gt;<br />
</code><br />
<span class="c_letter">N</span>ow the script will be <strong>executed after everything on the page is loaded</strong>.</p>
<h3>Wrap up and some hints</h3>
<p><span class="c_letter">T</span>his is all I want to teach you for now. Have a look at <a href="http://www.w3schools.com/js/default.asp">w3school&#8217;s JavaScript Tutorial</a> if you can&#8217;t wait to go on with learning JavaScript. Before you visit this page, please <strong>listen to my hints as these are based on my knowledge of coding nice, user-friendly websites</strong>:</p>
<ul>
<li>Never ever use JavaScript to open PopUps <strong><em>without</em></strong> informing the user before.</li>
<li>Only use JavaScript if it increases the experience of the website.</li>
<li>Try to <strong>avoid excessive use of JavaScript</strong> just because you can do that.
<li>Don&#8217;t use JavaScript to &#8220;link&#8221; to another page. It can break the browser history and result in a bad experience of the user.</li>
<li>Never ever use JavaScript to resize the browser window of the user</li>
<li>When using JavaScript, make sure that those who disabled JavaScript won&#8217;t miss parts of the website someone with JavaScript enabled would be able to see.</li>
</ul>
<p><span class="c_letter">I</span> know, if you read this list you will most likely not know how to create the mentioned effects. I suggest you keep this list in mind when I continue with my tutorial because then I will show you some of those things you shouldn&#8217;t do just for the purpose of showing it.</p>
<p><span class="c_letter">I</span> hope you enjoyed this introduction to my JavaScript tutorials. If you want to be informed on the publish of new tutorials, <a href="http://www.ruelicke.net/what-the-heck-is-rss/"><strong>subscribe to my RSS Feed</strong></a> or <a href="http://www.ruelicke.net/mail-subscription/"><strong>sign up for the newsletter</strong></a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ruelicke.net/2007/09/03/javascript-hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
