<?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>Feet Have Been &#187; Javascript</title>
	<atom:link href="http://wheremy.feethavebeen.com/category/tutorials/javascript-tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://wheremy.feethavebeen.com</link>
	<description>Sometimes, you just go where your feet take you</description>
	<lastBuildDate>Tue, 24 Jan 2012 17:39:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Custom Select Element</title>
		<link>http://wheremy.feethavebeen.com/2010/08/custom-select-element/</link>
		<comments>http://wheremy.feethavebeen.com/2010/08/custom-select-element/#comments</comments>
		<pubDate>Mon, 16 Aug 2010 22:04:59 +0000</pubDate>
		<dc:creator>xangelo</dc:creator>
				<category><![CDATA[Concepts]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[custom]]></category>
		<category><![CDATA[dropdown]]></category>
		<category><![CDATA[element]]></category>
		<category><![CDATA[form]]></category>

		<guid isPermaLink="false">http://wheremy.feethavebeen.com/?p=475</guid>
		<description><![CDATA[Since the post on styling the input box I thought I&#8217;d post a another method to create custom drop-down lists. Again, most browsers won&#8217;t let you do very much in the way of styling. IE lets you mess with scrollbars and gecko and webkit let you adjust line-heights and padding, but really, you are STILL [...]]]></description>
			<content:encoded><![CDATA[<p>Since the post on styling the input box I thought I&#8217;d post a another method to create custom drop-down lists. Again, most browsers won&#8217;t let you do very much in the way of styling. IE lets you mess with scrollbars and gecko and webkit let you adjust line-heights and padding, but really, you are STILL left with the default select. However, utilizing some html, css and JavaScript (through jQuery) we can have our own pretty nifty looking drop down. This is part of a UI suite I&#8217;m hoping to work on called Clair. </p>
<div id="attachment_481" class="wp-caption aligncenter" style="width: 310px"><a href="http://wheremy.feethavebeen.com/wp-content/uploads/2010/08/all-browser.png"><img src="http://wheremy.feethavebeen.com/wp-content/uploads/2010/08/all-browser-300x260.png" alt="" title="Custom dropdown" width="300" height="260" class="size-medium wp-image-481" /></a><p class="wp-caption-text">Custom Dropdown</p></div>
<p>The functionality of our drop-down will mimic that of a regular dropdown box with one exception. Since we can not style the scrollbar, our drop-down will never display one. Instead, it will just keep expanding as you add new links. </p>
<p><strong>HTML</strong><br />
To start off with, our select needs a few different elements to work properly. First off, we need a container that will hold our select. We will also require a list of elements that we want to appear when you click the drop-down. Finally, we need a default element to hold the current selected item and an arrow to signify that we can click on it to display more options. I&#8217;ve assigned the classes and Id&#8217;s to the elements as necessary. <code>clair-select-active</code> is the default item that will be displayed as well as what is displayed when an element is clicked on.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;clair-select-container&quot;&gt;
  &lt;div class=&quot;clair-select&quot;&gt;
    &lt;div id=&quot;clair-select-active&quot;&gt;Where My.Feet Have Been&lt;/div&gt;
    &lt;ul class=&quot;clair-select-links&quot;&gt;
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Nookish&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Orange Pine Co&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Where My.Feet Have Been&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Your Website&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
&lt;/div&gt;</pre></div></div>

<p><strong>CSS</strong><br />
Before we get to the clair specific styles, these are the default ones applied to the entire page:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">body<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Arial<span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span> 
  <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">normal</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">14pt</span><span style="color: #00AA00;">;</span> 
  <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'bg.png'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
  <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
a <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#CCC</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">bold</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>The first style we get is the <code>clair-select</code> and the <code>clair-select-container</code> These style our entire select container.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">.clair-select-container<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #933;">40px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.clair-select</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span> 
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#CCC</span><span style="color: #00AA00;">;</span>
  -moz-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">12px</span><span style="color: #00AA00;">;</span>
  -webkit-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">12px</span><span style="color: #00AA00;">;</span>
  -o-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">12px</span><span style="color: #00AA00;">;</span>
  -khtml-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">12px</span><span style="color: #00AA00;">;</span>
  border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">12px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span> <span style="color: #933;">12px</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">14px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> inline-<span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#555</span><span style="color: #00AA00;">;</span> 
  <span style="color: #000000; font-weight: bold;">border-width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span> <span style="color: #933;">1px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">230px</span><span style="color: #00AA00;">;</span>
  -moz-box-sizing<span style="color: #00AA00;">:</span> border-box<span style="color: #00AA00;">;</span>
  box-sizing<span style="color: #00AA00;">:</span> border-box<span style="color: #00AA00;">;</span>
  opacity<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0.7</span><span style="color: #00AA00;">;</span>
  filter<span style="color: #00AA00;">:</span> alpha<span style="color: #00AA00;">&#40;</span>opacity <span style="color: #00AA00;">=</span> <span style="color: #cc66cc;">70</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">2</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>The purpose of <code>clair-select-container</code> is to make sure when we expand our select dropdown it doesn&#8217;t push elements out of the way. Instead of floats nicely above them, and our slight opacity setting will display a faint outline of what it&#8217;s coverting. </p>
<p>There is a lot of vendor specific styling going on in <code>clair-select</code>, but the only one that I think warrants explanation is <code>box-sizing</code> and <code>filter</code>. </p>
<ul>
<li><code>box-sizing</code>: is a CSS3 spec that allows us to change the way that the browser interprets the &#8220;box-model&#8221; I&#8217;ll have to do another post to fully explain the box-model, but essentially it is the difference between including padding and border sizes within the &#8220;width&#8221; element, or adding it on afterwards. This forces Firefox to render the box-model the way Internet Explorer would  <a class="simple-footnote" title="The IE way, takes your width and keeps your content that size, and adds borders and padding within it" id="return-note-475-1" href="#note-475-1"><sup>1</sup></a>. </li>
<li><code>filter</code>: is a <a href="http://msdn.microsoft.com/en-us/library/ms532847(VS.85).aspx">Microsoft proprietary spec</a> that allows us to use a number of IE only effects. The particular effect I am utilizing is alpha-blending which allows us to change the opacity of the element. I&#8217;ve turned the opacity done just a little so that things are still readable, while allowing us to see the background. This has the added benefit of a neat visual effect whereby it looks like our element is slightly inset.</li>
</ul>
<p>The next style we&#8217;re going to add is that of the active element. All we do here is add the arrow as a background image and position it to appear on the right side of the element.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#clair-select-active</span><span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'down.png'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
  <span style="color: #000000; font-weight: bold;">background-repeat</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Finally, we&#8217;re going to style all the links. Nothing too fancy here. We&#8217;re just getting rid of bullets, spacing everything out a little and treating the anchor tags as block elements within our li&#8217;s.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.clair-select-links</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span> 
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.clair-select-links</span> li<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.clair-select-links</span> li a<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">normal</span><span style="color: #00AA00;">;</span> 
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.clair-select-links</span> li a<span style="color: #3333ff;">:hover</span><span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#282828</span><span style="color: #00AA00;">;</span>
  -moz-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">8px</span><span style="color: #00AA00;">;</span>
  -webkit-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">8px</span><span style="color: #00AA00;">;</span>
  -o-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">8px</span><span style="color: #00AA00;">;</span>
  -khtml-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">8px</span><span style="color: #00AA00;">;</span>
  border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">8px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#DDD</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p><strong>JavaScript</strong><br />
To add functionality to our drop-down we need a bit of JavaScript. I&#8217;m utilizing jQuery, but your own JavaScript library should have similar functions. We need to add two kinds of functionality. </p>
<ol>
<li>We need to switch between minimized and maximized states whenever a user clicks on the <code>clair-select-active</code> element.</li>
<li>We need to switch to minimized and populate <code>clair-select-active</code> with the text from the menu item</li>
</ol>

<div class="wp_syntax"><div class="code"><pre class="js" style="font-family:monospace;">$(document).ready(function(e) {
  // This is just for cross-browser prettiness
  $('#clair-select-active').css('cursor','pointer');
&nbsp;
  // This will toggle everything as necessary
  $('#clair-select-active').click(function(e) {
    if($('.clair-select-links').css('display') == 'block') {
      $('.clair-select-links').slideUp(); 
    }
    else {
      $('.clair-select-links').slideDown();
    }
  });
&nbsp;
  // This will populate clair-select-active with whatever you click on
  $('.clair-select-links a').click(function(e) {
    e.preventDefault(); 
    $('#clair-select-active').html($(this).html()); 
    $('.clair-select-links').slideUp(); 
  });
});</pre></div></div>

<p>And that&#8217;s it, you&#8217;ve built a custom select drop-down. With a little JavaScript trickery, we can even go so far as to include it within forms. We&#8217;d just need a hidden form element and then when we populate <code>clair-select-active</code> we would also populate the value attribute of our form element.</p>
<p>This solution works in the following scenarios: </p>
<ul>
<li>IE 7+</li>
<li>Firefox 3.5+</li>
<li>Chrome 3+</li>
<li>Opera 10+</li>
</ul>
<p>You can download all the files used in this tutorial here: <a href='http://wheremy.feethavebeen.com/wp-content/uploads/2010/08/custom-select.zip'>Custom Select Files</a></p>
<div class="simple-footnotes"><p class="notes">Notes:</p><ol><li id="note-475-1">The IE way, takes your width and keeps your content that size, and adds borders and padding within it <a href="#return-note-475-1">&#8617;</a></li></ol></div>]]></content:encoded>
			<wfw:commentRss>http://wheremy.feethavebeen.com/2010/08/custom-select-element/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Inconsistencies with File Upload</title>
		<link>http://wheremy.feethavebeen.com/2010/08/inconsistencies-with-file-upload/</link>
		<comments>http://wheremy.feethavebeen.com/2010/08/inconsistencies-with-file-upload/#comments</comments>
		<pubDate>Fri, 13 Aug 2010 03:17:21 +0000</pubDate>
		<dc:creator>xangelo</dc:creator>
				<category><![CDATA[Browsers]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Internet Explorer]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[file upload]]></category>

		<guid isPermaLink="false">http://wheremy.feethavebeen.com/?p=456</guid>
		<description><![CDATA[Before I even begin I want to state that this shouldn&#8217;t be used as a definitive source for this information. I am utilizing an older version of Firefox (3.0) and IE7 for all my tests. I can&#8217;t vouch for Chrome, Opera or Safari&#8217;s take on any of this, but I gather that it is much [...]]]></description>
			<content:encoded><![CDATA[<p>Before I even begin I want to state that this shouldn&#8217;t be used as a definitive source for this information. I am utilizing an older version of Firefox (3.0) and IE7 for all my tests. I can&#8217;t vouch for Chrome, Opera or Safari&#8217;s take on any of this, but I gather that it is much the same.</p>
<p>At some point in your web programming or web design career you will inevitably encounter the dreaded file upload &#8220;utility&#8221;. This piece of crap, is only present to allow users to select a file on their machine and eventually transfer it to your servers. Of course, this is a vital piece now, but perhaps at the time it was something that was easily overlooked. After all, at a time when your ISP speeds were approaching the 14.4kbps mark, uploading files was really something only the computer geeks did. </p>
<p>Now however, with profile pictures, online resumes, cloud storage and so much more, the file upload feature is front and center. Some places have gone so far as to eliminate the default HTML object and instead go with a Flash equivalent. This doesn&#8217;t really fix the issue, it&#8217;s like me saying &#8220;oh your computer doesn&#8217;t work? Here, use my iPad&#8221; <a class="simple-footnote" title="I really don&#8217;t have one http://wheremy.feethavebeen.com/2010/02/blagging-about-the-ipad/" id="return-note-456-1" href="#note-456-1"><sup>1</sup></a> It&#8217;s just a bypass. We need to fix this now! It&#8217;s already too late, we&#8217;re just playing catch up.</p>
<p><strong>Understanding the Process</strong><br />
File uploading is one of those features that, if you&#8217;re unlucky, you&#8217;ll have to design a layout around. That&#8217;s because, for some reason, every browser has decided that the File upload object is completely immune to CSS. No amount of styling can ever achieve what you want. It will always look like ass.</p>
<div id="attachment_470" class="wp-caption aligncenter" style="width: 295px"><a href="http://wheremy.feethavebeen.com/wp-content/uploads/2010/08/file-upload.png"><img src="http://wheremy.feethavebeen.com/wp-content/uploads/2010/08/file-upload.png" alt="File upload in Firefox and IE" title="File upload in Firefox and IE" width="285" height="266" class="size-full wp-image-470" /></a><p class="wp-caption-text">File upload in Firefox and IE</p></div>
<p>There is no easy way to style it, there is no way to replace it (unless you use flash, but that&#8217;s a whole different issue) and the only way to get around it is to hide it or integrate it directly into your design. There is absolutely no way to pretty it up. You can&#8217;t adjust the size of the button or the size of the input box. You are essentially stuck, with what you see. No way around it. Or is there? </p>
<p>Short of changing your entire design to work with default form elements (euych!) the only way to fix the file upload feature is to hide it completely. See, because we can still apply some style features to an encapsulating element <a class="simple-footnote" title="A div that contains the file upload field, for example" id="return-note-456-2" href="#note-456-2"><sup>2</sup></a>. So we could technically just hide the entire form field, and absolutely position it over our custom designed field. If we do it all right (we WILL have to use either a custom IE stylesheet or a bunch of hacks) we can make it look like our pretty file upload field is actually doing the work!</p>
<p><strong>The Code</strong><br />
The code for this is primarily CSS with some JavaScript sprinkled in to make sure that everything is positioned where it should be. I am utilizing the jQuery library for a lot of this simply because I use it in a lot of my projects. If you use any other libraries, they should all have equivalent methods and if they don&#8217;t, you should ditch them and switch to something else. If you are trying to follow along in straight JavaScript good luck. One of the primary reasons I work with a JavaScript library is because of cross browser inconsistencies. With a library, I can be sure that someone has gone through the painstaking task of setting up different kinds of math and implementation for the different browsers.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;File Upload&lt;/title&gt;
  &lt;style type=&quot;text/css&quot;&gt;
&nbsp;
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;fake-file&quot;&gt;
  &lt;input type=&quot;text&quot; id=&quot;fake-file-title&quot; name=&quot;fake_file_title&quot; value=&quot;&quot;&gt;
  &lt;button&gt;Browse&lt;/button&gt;
&lt;/div&gt;
&lt;div id=&quot;file-constraint&quot;&gt;
  &lt;input type=&quot;file&quot; id=&quot;real-file&quot;&gt;
&lt;/div&gt;
&nbsp;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>The CSS below is what we will be using to style our new input fields.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#fake-file</span> input <span style="color: #00AA00;">&#123;</span>
    -moz-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span><span style="color: #00AA00;">;</span> 
    <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#555</span><span style="color: #00AA00;">;</span> 
    -webkit-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span><span style="color: #00AA00;">;</span> 
    -o-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span><span style="color: #00AA00;">;</span> 
    -khtml-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span><span style="color: #00AA00;">;</span> 
    border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">2px</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #cc00cc;">#fake-file</span> button <span style="color: #00AA00;">&#123;</span>
    -moz-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span><span style="color: #00AA00;">;</span> 
    <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#555</span><span style="color: #00AA00;">;</span> 
    -webkit-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span><span style="color: #00AA00;">;</span> 
    -o-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span><span style="color: #00AA00;">;</span> 
    -khtml-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span><span style="color: #00AA00;">;</span> 
    border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #933;">8px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#FFF</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Now it&#8217;s as simple as adding some positioning values to our file-constraint field. We will also alter the z-index and position it higher vertically than our fake file upload. Then we can simply utilize some JavaScript to make things work like it should.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#file-constraint</span> <span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span> 
<span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span> 
<span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">2</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Bam instant hidden. Based on the position of your actual fake file field the top/left attributes will obviously change. The JavaScript is just as simple. I am utilizing jQuery to hook in to the onChange event for the real-file field, but it&#8217;s not necessary. You can do the same without a library. I just prefer jQuery because it means I can target all browsers the exact same way.</p>

<div class="wp_syntax"><div class="code"><pre class="js" style="font-family:monospace;">$('#real-file').change(function(e) {
var str = $(this).val(); 
str = str.split('\\')[str.length-1]; 
$('#fake-file-title').val(str);
});</pre></div></div>

<p>Basically we get the value of the file upload field. In Firefox this is just the name of the file, while in IE it contains the complete path to the file. I prefer stripping out the path, but that is up to you. Basically the value from the real file field is just assigned to the fake file field. </p>
<p>Working with file uploads are a hassle and this really only covers a portion of the client-side. There are still many things to take into account such as temporary file locations, file permissions, fake-ajax / standard uploading etc. This tutorial isn&#8217;t meant to be a complete compendium of all things file uploading, but merely to point out some annoying UI inconsistencies. </p>
<div class="simple-footnotes"><p class="notes">Notes:</p><ol><li id="note-456-1">I really don&#8217;t have one <a href="http://wheremy.feethavebeen.com/2010/02/blagging-about-the-ipad/">http://wheremy.feethavebeen.com/2010/02/blagging-about-the-ipad/</a> <a href="#return-note-456-1">&#8617;</a></li><li id="note-456-2">A div that contains the file upload field, for example <a href="#return-note-456-2">&#8617;</a></li></ol></div>]]></content:encoded>
			<wfw:commentRss>http://wheremy.feethavebeen.com/2010/08/inconsistencies-with-file-upload/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some useful array functions for JavaScript</title>
		<link>http://wheremy.feethavebeen.com/2010/07/some-useful-array-functions-for-javascript/</link>
		<comments>http://wheremy.feethavebeen.com/2010/07/some-useful-array-functions-for-javascript/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 20:40:43 +0000</pubDate>
		<dc:creator>xangelo</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://wheremy.feethavebeen.com/?p=450</guid>
		<description><![CDATA[JavaScript is a great language. There was a time when I was rather against it (mainly during the browser wars) but with modern libraries and the like, cross browser processing is exactly where it should be. However, there are a lot of times when you just need some simple processing done and most libraries &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>JavaScript is a great language. There was a time when I was rather against it (mainly during the browser wars) but with modern libraries and the like, cross browser processing is exactly where it should be. However, there are a lot of times when you just need some simple processing done and most libraries &#8211; even their cores &#8211; are too bloated to be of any use.  Below I&#8217;ve outlined three functions has(), indexOf() and remove() that extend the existing Array prototype to add some much needed features.</p>

<div class="wp_syntax"><div class="code"><pre class="js" style="font-family:monospace;">Array.prototype.indexOf = function(val) {
	for(i = 0; i &lt; this.length; i++) {
		if(this[i] == val) {
			return i; 
		}
	}
	return -1;
};
&nbsp;
Array.prototype.has = function(val) {
	return this.indexOf(val) &gt; 0 ? true:false;
};
&nbsp;
Array.prototype.remove = function(val) {
	var x = this.indexOf(val), y = 0;
	for(i = 0; i &lt; this.length; i++) {
		if(i != x) {
			this[y]= this[i];
			y++;
		}
	}
	this.pop();
};</pre></div></div>

<p><strong>indexOf()</strong><br />
Often times you have a string and you need to figure out where in the array it appears. Since JavaScript doesn&#8217;t technically support associative arrays (array object conversion does occur implicitly) you can either loop through the array every time, or just extend array!</p>

<div class="wp_syntax"><div class="code"><pre class="js" style="font-family:monospace;">Example for indexOf()
var myArray = [&quot;an&quot;,&quot;array&quot;,&quot;with&quot;,&quot;a&quot;,&quot;bunch&quot;,&quot;of&quot;,&quot;words&quot;]; 
document.Write(myArray.indexOf(&quot;a&quot;));   // returns 3
document.Write(myArray.indexOf(&quot;491&quot;));  // returns -1</pre></div></div>

<p><strong>has()</strong><br />
Sometimes you just need to know if a value is present within an array. has() takes care of that returning a simple true:false boolean. Rather than re-inventing the wheel, it works in conjunction with indexOf()</p>

<div class="wp_syntax"><div class="code"><pre class="js" style="font-family:monospace;">Example for has()
var myArray = [&quot;an&quot;,&quot;array&quot;,&quot;with&quot;,&quot;a&quot;,&quot;bunch&quot;,&quot;of&quot;,&quot;words&quot;]; 
document.Write(myArray.has(&quot;with&quot;)); // returns true
document.Write(myArray.has(&quot;feet&quot;)); // returns false</pre></div></div>

<p><strong>remove()</strong><br />
Finally, sometimes removing a single element from an array is necessary.</p>

<div class="wp_syntax"><div class="code"><pre class="js" style="font-family:monospace;">Example for remove()
var myArray = [&quot;an&quot;,&quot;array&quot;,&quot;with&quot;,&quot;a&quot;,&quot;bunch&quot;,&quot;of&quot;,&quot;words&quot;]; 
myArray.remove(&quot;array&quot;); // myArray now contains [&quot;an&quot;,&quot;with&quot;,&quot;a&quot;,&quot;bunch&quot;,&quot;of&quot;,&quot;words&quot;]</pre></div></div>

<p>Hopefully the functions are of help! If there are a lot of people confused over Prototypal Inheritance, maybe that would be an interesting topic.</p>
]]></content:encoded>
			<wfw:commentRss>http://wheremy.feethavebeen.com/2010/07/some-useful-array-functions-for-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Search array for Substring</title>
		<link>http://wheremy.feethavebeen.com/2010/07/search-array-for-substring/</link>
		<comments>http://wheremy.feethavebeen.com/2010/07/search-array-for-substring/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 14:56:22 +0000</pubDate>
		<dc:creator>xangelo</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[Search]]></category>

		<guid isPermaLink="false">http://wheremy.feethavebeen.com/?p=397</guid>
		<description><![CDATA[Occasionally you&#8217;ll need to create some sort of auto-complete field. While you could power this entirely via AJAX requests on a per-character basis, with limited resources, you may want to look at this alternative. Essentially you&#8217;ll create a javascript array containing allowed values. Whenever a user types in some characters, we can use some regex [...]]]></description>
			<content:encoded><![CDATA[<p>Occasionally you&#8217;ll need to create some sort of auto-complete field. While you could power this entirely via AJAX requests on a per-character basis, with limited resources, you may want to look at this alternative.</p>
<p>Essentially you&#8217;ll create a javascript array containing allowed values. Whenever a user types in some characters, we can use some regex coupled with a quick iteration to see if whatever the user entered is valid. If it is, we can easily return the matched values.</p>
<p>The function below is written in straight JavaScript. I monitor my keypress events via jQuery, but any way you want to do it is fine. Just make sure you call arrayFind() with the string you want to find and the array you want to search in.</p>

<div class="wp_syntax"><div class="code"><pre class="js" style="font-family:monospace;">function arrayFind(needle,haystack) {
var matchList = [];
var regex = new RegExp(needle,'gi');
for(i in haystack) {
	if(haystack[i].match(regex)) {
		matchList[matchList.length] = haystack[i];
	}
}
return matchList;
}</pre></div></div>

<p>If you didn&#8217;t need to use a variable for the regex you could eliminate that line and just use /searchterm/gi for a global case-insensitive search of the array. Since we are however, we need to make use of the RegExp object.</p>
]]></content:encoded>
			<wfw:commentRss>http://wheremy.feethavebeen.com/2010/07/search-array-for-substring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Aggro Detection with Javascript</title>
		<link>http://wheremy.feethavebeen.com/2009/12/aggro-detection-with-javascript/</link>
		<comments>http://wheremy.feethavebeen.com/2009/12/aggro-detection-with-javascript/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 14:00:35 +0000</pubDate>
		<dc:creator>xangelo</dc:creator>
				<category><![CDATA[Concepts]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://wheremy.feethavebeen.com/?p=358</guid>
		<description><![CDATA[So you want to make some games? Sure it sounds easy, but until you dive into it you&#8217;ll never notice all the little things that go into the backend of it that make or break your game. While this isn&#8217;t as big a deal in current browser-based games (they&#8217;re either muds, flash or static maps) [...]]]></description>
			<content:encoded><![CDATA[<p>So you want to make some games? Sure it sounds easy, but until you dive into it you&#8217;ll never notice all the little things that go into the backend of it that make or break your game. While this isn&#8217;t as big a deal in current browser-based games (they&#8217;re either muds, flash or static maps) things are changing in the browser. Everything is getting faster, and things like javascript based animation is actually feasible! So what is an aspiring game developer to do except buckle in and start programming their games?</p>
<p>Once you begin researching you&#8217;ll notice that while a lot of browser-based text games take into account Collision detection, they seem to be missing Aggro detection. Aggro stands for either aggravation or aggression and is essentially a way to tell if a monster will attack you or not.. As you&#8217;ve probably noticed some monsters tend to rush at you from across the map, while others seem to completely ignore you until you attack them. This is Aggro.</p>
<p>One of the easiest ways to detect aggro is to simply add a variable to your monsters to signify &#8220;sight&#8221;. This sight variable will dictate how far away you need to be before a monster will attack you. Set this to 100 to have a monster rush at you from across your map or set it to 0 so that it will never attack unless you attack first.</p>
<p>Detection aggro is as simple as following two formula&#8217;s. The first will be to figure out the distance between you and the monster and the second will simply be a quick check to see if you fall within the monsters aggro radius. To start off, you need to grab the <a href="http://www.jquery.com">jQuery</a> javascript framework. While you CAN do this from scratch, jQuery lets you do it a lot easier, plus you can probably add a bit more jazz to your current game if you&#8217;re not already using it. If you&#8217;re unsure of how jQuery works, take a look at our quick <a href="http://wheremy.feethavebeen.com/2009/11/jquery-primer-an-intro-to-client-side-functionality/">intro to jQuery</a> or just check out the <a href="http://docs.jquery.com/Main_Page">jQuery documentation</a>. Now just cobble together a quick webpage and include the jQuery library. I&#8217;m using the minified version 1.3.2 of jQuery, so if you&#8217;re not using the same one, make sure you replace that line.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Collision Detection&lt;/title&gt;
	&lt;style type=&quot;text/css&quot;&gt;
	table#grid{
		border-style: solid; 
		border-width: 0px 1px 1px 0px; 
		border-color: #CCC; 
		padding: 0px;
	}
	table#grid tr th{
		border-style: solid; 
		border-width: 1px 0px 0px 1px; 
		border-color: #CCC; 
		text-align: center; 
		color: #333; 
		font-size: 0.7em; 
		font-weight: bold; 
		font-family: &quot;Arial&quot;;
		background-color: #EDEDED;
	}
	table#grid tr th.run{
		width: 12px;
	}
	table#grid tr td{
		width: 50px; 
		height: 50px; 
		border-style: solid; 
		border-width: 1px 0px 0px 1px; 
		border-color: #CCC; 
		text-align: center; 
		color: #E0E0E0; 
		font-size: 0.7em; 
		font-weight: normal; 
		font-family: &quot;Arial&quot;;
	}
	table#grid tr td:hover{
		color: #333;
		background-color: #F3F3F3;	
	}
	.player{
		background-color: #333; 
	}
	.monsterA{
		color: #333; 
		background-color: #AA0000;
	}
	div#debug{
		border-style: solid; 
		border-color: #333; 
		border-width: 1px; 
		float: right; 
		border-color: #EDEDED;
		text-align: left; 
		color: #333; 
		font-size: 0.9em; 
		font-weight: normal; 
		font-family: &quot;Arial&quot;;
		width: 300px; 
		padding: 10px;
	}
	.monsterN{
		background-color: #00AA00;
	}
	&lt;/style&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;jquery-1.3.2.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&nbsp;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>You&#8217;ll notice a bunch of CSS in there, but you can ignore that. It&#8217;s pretty straight forward and really it&#8217;s just meant to pretty things up a little. Now we&#8217;re going to add a quick grid so that you can get an easy visual representation of where you are and where your monsters are and if they will aggro or not. You can copy and paste the following line a few times, and change the y coordinate (x,y) to match which row you&#8217;re on.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;tr&gt;
	&lt;td&gt;0,1&lt;/td&gt;&lt;td&gt;1,1&lt;/td&gt;&lt;td&gt;2,1&lt;/td&gt;&lt;td&gt;3,1&lt;/td&gt;&lt;td&gt;4,1&lt;/td&gt;&lt;td&gt;5,1&lt;/td&gt;&lt;td&gt;6,1&lt;/td&gt;&lt;td&gt;7,1&lt;/td&gt;&lt;td&gt;8,1&lt;/td&gt;&lt;td&gt;9,1&lt;/td&gt;
&lt;/tr&gt;</pre></div></div>

<p>Finally, I&#8217;ve just added a &#8220;debug&#8221; section, so you can get a textual representation of what&#8217;s going on. It&#8217;s just a div placed above the grid.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;debug&quot;&gt;
	&lt;b&gt;Debug&lt;/b&gt;&lt;br&gt;&lt;br&gt;
&lt;/div&gt;</pre></div></div>

<p><strong>The Code</strong><br />
Now for the code that will give us a visual representation of our player and our monsters. First off we are going to initiate a few variables that we will be using throughout our script. They&#8217;re pretty self explanatory.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> playerPlaced <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> 
<span style="color: #003366; font-weight: bold;">var</span> monster <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
<span style="color: #003366; font-weight: bold;">var</span> player <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now we&#8217;re going to set it up so that when we click a square it will check if our player is already placed or not. If he is NOT placed, then we are going to place him and update our &#8220;debug&#8221; section.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#grid tr td'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> distance<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>playerPlaced<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	player<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'loc'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">','</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	player<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'sight'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> 
	$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'@'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'player'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	playerPlaced <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span> 
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#debug'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Player: '</span><span style="color: #339933;">+</span>player<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'loc'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'&lt;br&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Notice that we&#8217;ve set player['sight'] to 0. Why? Simply because your player doesn&#8217;t have an aggro radius. He may or may not attack at your discretion, so it&#8217;s irrelevant. A monster however, will ALWAYS attack if you get within its aggro radius (assuming no other modifiers).</p>
<p>The next bit will just place our monsters if there is already a player on the field. When we do, however, we are going to call some functions that will take care of calculating distance and checking if the monster will aggro. We&#8217;ll go over those first, since they&#8217;re more complicated.</p>
<p><strong>Distance</strong><br />
To calculate the distance of two points we are simply going to take the x,y coordinates of both our monster and player and perform some math on them. The formula that we will be using is actually the distance formula between two points (and you wondered when you would ever use it&#8230;) and something that&#8217;s been drilled into us in every math class. The formula itself is a basically sqrt(sq(x1-x2)+sq(y1-y2)) so translating that to our game code it will look more like</p>
<p>sqrt(sq(player[x]-monster[x])+sq(player[y]-monster[y]))</p>
<p>The squaring and square rooting would take us a lot more work, if JavaScript didn&#8217;t already have those built in! Our actual distance formula in JavaScript (after being placed in a function) will look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getDistance<span style="color: #009900;">&#40;</span>from<span style="color: #339933;">,</span>to<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	distance <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">sqrt</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>from<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span>to<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> Math.<span style="color: #660066;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>from<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span>to<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #000066; font-weight: bold;">return</span> distance<span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Intersection</strong><br />
Basically intersection in our scenario will take a look at the player and see if he falls within the aggro radius of the monster. It&#8217;s a very simple check that just goes if (distance &lt; aggroRad) then it will aggro.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> willAggro<span style="color: #009900;">&#40;</span>aggro<span style="color: #339933;">,</span>distance<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>distance <span style="color: #339933;">&gt;</span> aggro<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> 
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Monster Insertion</strong><br />
Finally, we&#8217;re going to do our check to see if a monster will aggro a player or not depending on its distance to the player.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">monster<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'loc'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">','</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
monster<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'sight'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> 
$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'X'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
distance <span style="color: #339933;">=</span> getDistance<span style="color: #009900;">&#40;</span>player<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'loc'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>monster<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'loc'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>willAggro<span style="color: #009900;">&#40;</span>monster<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'sight'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>distance<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'monsterA'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'monsterN'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#debug'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;b&gt;Monster:&lt;/b&gt; '</span><span style="color: #339933;">+</span>monster<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'loc'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#debug'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' &lt;b&gt;Distance:&lt;/b&gt; '</span><span style="color: #339933;">+</span>distance<span style="color: #339933;">+</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#debug'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' &lt;b&gt;Aggro:&lt;/b&gt; '</span><span style="color: #339933;">+</span>willAggro<span style="color: #009900;">&#40;</span>monster<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'sight'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>distance<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'&lt;br&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And that&#8217;s all there is to it. If you run this page and take a look, you&#8217;ll notice that clicking a grid will place your player and clicking anywhere else will cause the monsters to appear in either green (if too far away to aggro) or red (if it will aggro). This is also noted in the debug section!</p>
<p>Here is our completed javascript section</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> playerPlaced <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> 
	<span style="color: #003366; font-weight: bold;">var</span> monster <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
	<span style="color: #003366; font-weight: bold;">var</span> player <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#grid tr td'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> distance<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>playerPlaced<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			player<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'loc'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">','</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
			player<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'sight'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> 
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'@'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'player'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
			playerPlaced <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span> 
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#debug'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Player: '</span><span style="color: #339933;">+</span>player<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'loc'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'&lt;br&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
			monster<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'loc'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">','</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
			monster<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'sight'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> 
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'X'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			distance <span style="color: #339933;">=</span> getDistance<span style="color: #009900;">&#40;</span>player<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'loc'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>monster<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'loc'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>willAggro<span style="color: #009900;">&#40;</span>monster<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'sight'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>distance<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'monsterA'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'monsterN'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#debug'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;b&gt;Monster:&lt;/b&gt; '</span><span style="color: #339933;">+</span>monster<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'loc'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#debug'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' &lt;b&gt;Distance:&lt;/b&gt; '</span><span style="color: #339933;">+</span>distance<span style="color: #339933;">+</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#debug'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' &lt;b&gt;Aggro:&lt;/b&gt; '</span><span style="color: #339933;">+</span>willAggro<span style="color: #009900;">&#40;</span>monster<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'sight'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>distance<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'&lt;br&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> getDistance<span style="color: #009900;">&#40;</span>from<span style="color: #339933;">,</span>to<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	distance <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">sqrt</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>from<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span>to<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> Math.<span style="color: #660066;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>from<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span>to<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #000066; font-weight: bold;">return</span> distance<span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span>	
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> willAggro<span style="color: #009900;">&#40;</span>aggro<span style="color: #339933;">,</span>distance<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>distance <span style="color: #339933;">&gt;</span> aggro<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> 
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is a very simple way to check aggro and the reason I placed it in its own function was incase you needed to modify it in any way (for example if a monster was under 10 levels its aggro would be 0 always). <strong> </strong></p>
]]></content:encoded>
			<wfw:commentRss>http://wheremy.feethavebeen.com/2009/12/aggro-detection-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Website Instant Messenger &#8211; Client-side Functionality</title>
		<link>http://wheremy.feethavebeen.com/2009/11/simple-website-instant-messenger-client-side-functionality/</link>
		<comments>http://wheremy.feethavebeen.com/2009/11/simple-website-instant-messenger-client-side-functionality/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 05:22:58 +0000</pubDate>
		<dc:creator>xangelo</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://wheremy.feethavebeen.com/?p=319</guid>
		<description><![CDATA[This is part two of the Simple Website Instant Messenger (SWIM for short, I love it when acronyms just work themselves out) and will cover adding some client-side functionality to our system. For part 1, click here. Client-side functionality essentially translates itself to &#8220;using JavaScript to do some funcy (eh? get it? Funcy? Functional?) things&#8221;. [...]]]></description>
			<content:encoded><![CDATA[<p>This is part two of the Simple Website Instant Messenger (SWIM for short, I love it when acronyms just work themselves out) and will cover adding some client-side functionality to our system. <a href="http://wheremy.feethavebeen.com/2009/11/simple-website-instant-messenger-the-static-prototype/">For part 1, click here.</a></p>
<p>Client-side functionality essentially translates itself to &#8220;using JavaScript to do some funcy (eh? get it? Funcy? Functional?) things&#8221;. It will allow us to add a level of interaction that users are familiar with and that they come to expect from applications. We want them to have the ability to minimize or maximize the windows, as well as close them. We also want to do all of this without page refreshes. Since actions like this are on a per-session basis, we don&#8217;t need to have any server-side code to handle it. We can let the browsers do all the work for us.</p>
<p>For this section we&#8217;ll be making some heavy use of jQuery and some rather complex DOM manipulation. I&#8217;ve also opted to use the long-hand version of everything instead of shorthand. This is so that you guys will be able to read the code and such. I&#8217;ve opted to use the production version of jQuery, but if you&#8217;re just getting started, you may as well download the development version. The bonus of this is that if you&#8217;re just learning you can easily pop in to the jQuery code and see how things are done. As well, if you are using an advanced IDE (such as Dreamweaver) you&#8217;ll get full code hinting. You can do this by heading to <a href="http://jquery.com/">this website</a> and downloading the appropriate version. I also recommend that you bookmark the documentation for jQuery (<a href="http://docs.jquery.com">http://docs.jquery.com</a>) as you&#8217;ll end up using it a lot.</p>
<p>Setup jQuery in whatever folder you&#8217;ve stored the HTML/CSS file from the previous tutorial (setup is actually just copy/paste). You&#8217;ll then need to link the script in your HTML by adding the following line of code to your HTML file in the head section of the code</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script src=&quot;jquery.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;</pre></div></div>

<p>Note the src attribute contains the name of the jquery file that I am using. You may have to edit it so that the name of the file matches the name of the file you downloaded.</p>
<p>Directly below that you can add this line as well</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script src=&quot;im.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;</pre></div></div>

<p>That line of code will link to our not-yet-created javascript file which will hold all the client-side functionality for our IM script. Once that&#8217;s done you can pop open your editor and get started with the actual jQuery code. However, before we continue with the tutorial note that this is NOT a jQuery primer. This tutorial is expecting you to have some knowledge of jQuery and javascript syntax. If you do not, please read this <a href="http://wheremy.feethavebeen.com/2009/11/jquery-primer-an-intro-to-client-side-functionality/">jQuery primer tutorial</a> first before continuing.</p>
<p>The first thing we are going to do is ensure that our &#8220;information&#8221; window has a toggle-state. Instead of using jQuery&#8217;s built in toggle() method, I&#8217;ve decided to go with a simple boolean check.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #003366; font-weight: bold;">var</span> windowOpen <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.info'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>windowOpen<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.more'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fast'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'img'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'src'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'/icon/bullet_arrow_up.png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				windowOpen <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.more'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fast'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'img'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'src'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'/icon/bullet_arrow_down.png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				windowOpen <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Basically, this will apply the &#8220;onclick&#8221; event to our main window launching button. When clicked it will check if the window is already open and depending on the value of windowOpen, it will close/open it accordingly.</p>
<p>This next section of code will handle opening the chat windows. Since we want to make sure that only windows that AREN&#8217;T opened can be opened (redundant? yes, but completely necessary). This will ensure that if a window is already open, it won&#8217;t open it again.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.contacts span'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</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>
		<span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		<span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		<span style="color: #003366; font-weight: bold;">var</span> found <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> 
		<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #000066; font-weight: bold;">in</span> openWindows<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>openWindows<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> id<span style="color: #339933;">+</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				found <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>found<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.im-bar'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prepend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;span class=&quot;task&quot; id=&quot;'</span><span style="color: #339933;">+</span>id<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;&gt;&lt;div class=&quot;convo&quot;&gt;&lt;img src=&quot;/icon/cancel.png&quot; id=&quot;close-win&quot; alt=&quot;Cancel&quot;&gt;&lt;span class=&quot;system&quot;&gt;You have entered a conversation with '</span><span style="color: #339933;">+</span><span style="color: #000066;">name</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'&lt;/span&gt;&lt;textarea name=&quot;message&quot; id=&quot;message&quot;&gt;&lt;/textarea&gt;&lt;/div&gt;&lt;div class=&quot;win&quot; name=&quot;'</span><span style="color: #339933;">+</span><span style="color: #000066;">name</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;&gt;'</span><span style="color: #339933;">+</span><span style="color: #000066;">name</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span><span style="color: #339933;">+</span>id<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.convo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'inline'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			openWindows<span style="color: #009900;">&#91;</span>openWindows.<span style="color: #660066;">length</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">+</span><span style="color: #000066;">name</span><span style="color: #339933;">;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span><span style="color: #339933;">+</span>id<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.win'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'padding-top'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'50px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			convoOpen <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
			<span style="color: #003366; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>openWindows.<span style="color: #660066;">length</span><span style="color: #339933;">*</span><span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>openWindows.<span style="color: #660066;">length</span><span style="color: #339933;">*</span><span style="color: #CC0000;">15</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span><span style="color: #339933;">+</span>id<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.convo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'right'</span><span style="color: #339933;">,</span>offset<span style="color: #339933;">+</span><span style="color: #3366CC;">'px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span><span style="color: #339933;">+</span>id<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'textarea'</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>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.more'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fast'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.info'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'img'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'src'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'/icon/bullet_arrow_up.png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		windowOpen <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The code itself remains fairly straightforward albeit long. First it does some variable assignment and then checks the openWindows array to see if it can find this particular window. If it can&#8217;t, it simply continues with the code. First it creates the new window and places it at the front of the open-window list. Then it just adds it to the openWindows array and does a bit more manipulation to create a text-box and align it properly with the current tab. It also opens the window and focuses on the text area. Finally it fades out the main information window and resets the image and boolean var. </p>
<p>Something to note here: Since we want openWindows and convoOpen to be accessible to ALL our functions, we can place it next to the &#8220;openWindow&#8221; declaration at the top of the script. It would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> openWindows <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #003366; font-weight: bold;">var</span> convoOpen <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span></pre></div></div>

<p>This next bit will just pop open the window if you click on the tab, and minimize it again if you click again.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.task .win'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</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>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>convoOpen<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'none'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'padding-top'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'5px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
			convoOpen <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.convo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'block'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'padding-top'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'50px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			convoOpen <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>		   
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We&#8217;re nearing the end of our code. This block of code will handle the &#8220;Close&#8221; button on a conversation window. If it is clicked, the window is removed from the tab bar and the openWindows array and the remaining positions are re-calculated.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.task .convo #close-win'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</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>
		<span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #000066; font-weight: bold;">in</span> openWindows<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>openWindows<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> id<span style="color: #339933;">+</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				openWindows.<span style="color: #660066;">splice</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		convoOpen <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> openWindows.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.im-bar'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.convo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">15</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'right'</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span>offset<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			i<span style="color: #339933;">--;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This last block of code will take any text in our textarea and submit it to the server when you press enter. The AJAX portion of this tutorial will be following, so in the mean-time everything else is showed here. The message is sent to the server, and then it is also automatically placed into the conversation window. We also strip out all tags from the code to ensure that nothing breaks our layout or is able to get through.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.task .convo #message'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'keyup'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">which</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">13</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			data <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			data <span style="color: #339933;">=</span> data.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/&lt;\/?[^&gt;]+&gt;/gi</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #006600; font-style: italic;">// Send message</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'system'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'system'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>data <span style="color: #339933;">!=</span> <span style="color: #3366CC;">''</span> <span style="color: #339933;">&amp;&amp;</span> data <span style="color: #339933;">!=</span> <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;p&gt;&lt;b&gt;'</span><span style="color: #339933;">+</span>yourName<span style="color: #339933;">+</span><span style="color: #3366CC;">':&lt;/b&gt; '</span><span style="color: #339933;">+</span>data<span style="color: #339933;">+</span><span style="color: #3366CC;">'&lt;/p&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> scrollTop<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;scrollHeight&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That is the basics of our Client-Side functionality to our Simple Website Instant Messenger system. The next installment will cover adding the AJAX function and modifying this last section sightly to reflect it.</p>
]]></content:encoded>
			<wfw:commentRss>http://wheremy.feethavebeen.com/2009/11/simple-website-instant-messenger-client-side-functionality/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>jQuery Primer &#8211; An Intro to Client-side Functionality</title>
		<link>http://wheremy.feethavebeen.com/2009/11/jquery-primer-an-intro-to-client-side-functionality/</link>
		<comments>http://wheremy.feethavebeen.com/2009/11/jquery-primer-an-intro-to-client-side-functionality/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 14:00:10 +0000</pubDate>
		<dc:creator>xangelo</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://wheremy.feethavebeen.com/?p=321</guid>
		<description><![CDATA[If you&#8217;re interested in web development, you&#8217;ve doubtlessly come across jQuery and wondered about how you could utilize it in your own projects. jQuery looks a little daunting at first, but hopefully by the end of this little introduction to jQuery you&#8217;ll have a much firmer grasp on it and be able to take your [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re interested in web development, you&#8217;ve doubtlessly come across jQuery and wondered about how you could utilize it in your own projects. jQuery looks a little daunting at first, but hopefully by the end of this little introduction to jQuery you&#8217;ll have a much firmer grasp on it and be able to take your projects to the next level.</p>
<p><strong>What is it?<br />
<span style="font-weight: normal;"><a href="http://wheremy.feethavebeen.com/wp-content/uploads/2009/11/jquery1.PNG"><img class="alignright size-full wp-image-327" title="jquery1" src="http://wheremy.feethavebeen.com/wp-content/uploads/2009/11/jquery1.PNG" alt="jquery1" width="233" height="93" /></a>If you go by their website jQuery touts itself as being able to do damn near everything. And in truth, it does. It simplifies otherwise tedious tasks and allows you to add huge amounts of functionality to your website without much code. What it IS, however, is a Framework that provides you the ability to perform these tedious tasks in a way that&#8217;s simple and concise while achieving cross-browser support. This means that you can write your code quickly while knowing that it will work exactly how you expect it to in every browser. This cuts down on the amount of development time that you spend. Another bonus is the way that jQuery naming conventions make sense. Things like adding onclick handlers are as simple as adding .click(); to a selector (more on this later). This means that the learning curve is very shallow allowing you to literally just jump right now.</span></strong></p>
<p><strong>Getting started</strong><br />
<span style="font-weight: normal;">Your first step will be to download jQuery. To do that, just head over to their website (<a href="http://jquery.com/">http://www.jquery.com</a>) and you can download it. It will be up to you whether you want to download the production or development versions. Both are the same, it&#8217;s just that the production code has been minified so it results in a much smaller file size. If you are just starting out however, I&#8217;d recommend going with the development version. If you have a full fledged IDE then your code hinting will come into play and you&#8217;ll be able to see descriptions of what the arguments are and such. However, even if you don&#8217;t, the development code will definitely be something you&#8217;ll want to peruse through in your spare time. You can learn a lot from looking at other peoples code. Once you have it downloaded, you can use it by simply including the following: </span></p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;path/to/jquery.js&quot;&gt;&lt;/script&gt;</pre></div></div>

<p>I&#8217;ve intentionally left the &#8220;src&#8221; attribute pointing to a random file as you will have to change it to point to wherever your jQuery file is. </p>
<p><strong>First Steps</strong><br />
When using jQuery you have to remember that you&#8217;ll be spending a lot of time manipulating the DOM. In order for this to occur, you&#8217;ll have to wait till the DOM is loaded. However, because of where your script files are generally located, they may load before the DOM has completely finished. If this happens then your scripts will just run in to a lot of errors. jQuery comes with a built in way to check that the DOM is ready for use. All your jQuery scripts should run only AFTER the DOM is ready. In a separate script tag, you can add the following</p>

<div class="wp_syntax"><div class="code"><pre class="js" style="font-family:monospace;">$(document).ready(function(){ })</pre></div></div>

<p>jQuery makes use of anonymous functions quite a bit, so you may want to make a note of that. You don&#8217;t NEED to have a function name in JavaScript. $ is a global variable created by jQuery as an accessor to all of its methods. This means that all your scripts in jQuery will utilize $ to perform it&#8217;s functions. $ will accept multiple types of arguments. You can either provide an ID, a Class or even an html tag for jQuery to work its magic on. I recommend you read up on the documentation for $ as it is one thing that you will definitely need to understand to perform complex manipulations. </p>
<p>You can go ahead and add</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;#&quot;&gt;Link&lt;/a&gt;</pre></div></div>

<p> to the body section of your code. This is so that we have something to work with in jQuery. </p>
<p>Going back to our anonymous function, you can change it so that it looks like this</p>

<div class="wp_syntax"><div class="code"><pre class="js" style="font-family:monospace;">$(document).ready(function(){
$('a').click(function(){ alert('Works!'); }); 
}</pre></div></div>

<p>Because of how powerful jQuery is, that will actually apply itself to EVERY anchor tag in the DOM. You can pick and choose which ones by applying numerous <a href="http://docs.jquery.com/Selectors">selectors</a>. There are so many involved that that itself is the topic of another discussion. However, if you are looking to work on a particular id the code would be</p>

<div class="wp_syntax"><div class="code"><pre class="js" style="font-family:monospace;">$('#id')</pre></div></div>

<p> and if you were working on a class you could use</p>

<div class="wp_syntax"><div class="code"><pre class="js" style="font-family:monospace;">$('.class')</pre></div></div>

<p> Keep in mind that jQuery will always work on EVERYTHING that matches the selector that you have provided. </p>
<p>jQuery is also chainable. Because of how the code is written, you can keep tacking on different functions. For example, if we look through the <a href="http://docs.jquery.com/Effects">effects </a>section of the documentation you&#8217;ll see things like slideDown() and fadeIn(). These effects can be chained together to create multiple tiers of animations or events. </p>
<p>My recommendation is that you definitely check out the full documentation and spend a lot of time playing around with it. There will be a few more jQuery tutorials popping up on the website as I start preparing the gradual jump from no jQuery experience to enough experience to write the client-side functionality of our <a href="http://wheremy.feethavebeen.com/2009/11/simple-website-instant-messenger-the-static-prototype/">SWIM!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://wheremy.feethavebeen.com/2009/11/jquery-primer-an-intro-to-client-side-functionality/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tooltips</title>
		<link>http://wheremy.feethavebeen.com/2009/07/tooltips/</link>
		<comments>http://wheremy.feethavebeen.com/2009/07/tooltips/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 19:20:17 +0000</pubDate>
		<dc:creator>xangelo</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[tooltip]]></category>

		<guid isPermaLink="false">http://wheremy.feethavebeen.com/?p=132</guid>
		<description><![CDATA[You&#8217;ve probably seen them multiple times while never really wondering how they&#8217;re done. Tooltip are one of the handiest features developers have to let people know what a particular object does. Tooltips can be anything from descriptions to definitions, or even provide additional information about an object. Most frequently, you can see tooltips in games. [...]]]></description>
			<content:encoded><![CDATA[<p>You&#8217;ve probably seen them multiple times while never really wondering how they&#8217;re done. Tooltip are one of the handiest features developers have to let people know what a particular object does. Tooltips can be anything from descriptions to definitions, or even provide additional information about an object. Most frequently, you can see tooltips in games. Since you only have a limited amount of screen-room in games, developers need to think about how to show you most of the game, while minimizing the amount of helpful text that you see. In first person shooters this is often very easy, but when you move to games like Starcraft or Star Wars: Galactic Battlegrounds it becomes too hard to display enough information without layering modal windows and tooltips.</p>
<p>The same can be said about web pages. Rather than bombard a user with lists and paragraphs of text, maybe things can be more easily managed. The easiest example of this, would be definitions. Imagine coming across a word that you don&#8217;t understand. Wouldn&#8217;t it be easier if the developer just added a little feature where you could hover over a word and it would show a little popup telling you the definition?The following screenshots show a few examples of what I&#8217;m talking about:</p>
<p><img class="aligncenter size-full wp-image-133" title="jx01" src="http://wheremy.feethavebeen.com/wp-content/uploads/2009/07/jx01.jpg" alt="jx01" width="355" height="67" /><img class="aligncenter size-full wp-image-134" title="jx02" src="http://wheremy.feethavebeen.com/wp-content/uploads/2009/07/jx02.jpg" alt="jx02" width="445" height="102" />Note that with the second image, the tooltip is clearly hovering on top of some text, as well, the tooltip graphic is a little more detailed than the first, which is a simple span.</p>
<p>To achieve this rather &#8220;complex&#8221; effect, you can simple have attach onmouseover/onmouseout events to the trigger text and a simple function which accepts the ID of the target as an attribute. The code for this would simply be:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> tooltip<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>
 document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">else</span>
 document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;</span>a onmouseover<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;tooltip('target')&quot;</span> onmouseout<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;tooltip('target')&quot;</span><span style="color: #339933;">&gt;</span>Trigger<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;target&quot;</span> style<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;display: none; &quot;</span><span style="color: #339933;">&gt;</span>Some Text<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>

<p>While it seems simple enough, once you start monitoring a lot more events, things get a lot more confusing. The easiest way to handle something like this is to just create a function that will accept two values. The first will be the trigger and the second will be the target. A very simple concept, but one that takes a little more work than you would think.</p>
<p>For starters, there is no way for you to simply add &#8220;element.onmouseover&#8221; in javascript. Instead you have to use &#8220;element.addEventListener&#8221; which accepts 3 arguments. </p>
<p>Before we get to that stage, lets first set up our main function. Since javascript accepts $ as a string, lets use that, along with some other characters, as our function name. The reason being that it is almost guaranteed that you will never come across another function by that name unless you start using frameworks.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> $_jx<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A good way to think about programming is to think about use before implementing a structure that you may not like. For example, I want to be able to write</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$_jx.<span style="color: #660066;">tooltip</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'trigger'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'target'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and have it automatically assign the tooltip functionality to the trigger. In order to achieve this, we need to start working with functions within functions (and yes, even a fourth tier of that). Since tooltip is within $_jx, we can define it like this</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> $_jx<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">tooltip</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>trigger<span style="color: #339933;">,</span>target<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that we are using an anonymous function and using tooltip as an alias. A little strange, but it ensures easy use down the line. To attach the tooltip event to our trigger, we need to make use of &#8220;addEventListener&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> $_jx<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">tooltip</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>trigger<span style="color: #339933;">,</span>target<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> trigger <span style="color: #339933;">=</span> trigger<span style="color: #339933;">;</span> 
    <span style="color: #003366; font-weight: bold;">var</span> target <span style="color: #339933;">=</span> target<span style="color: #339933;">;</span> 
    <span style="color: #003366; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> offset<span style="color: #339933;">;</span> 
    document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>trigger<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover'</span><span style="color: #339933;">,</span>whattodo<span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>First we&#8217;re going to create some local variables that will be visible to this.tooltip and all its children. Since addEventListener does not let us pass arguments to functions defined within the &#8220;whattodo&#8221; section, we need some way of accessing them without over-writing any other variables. </p>
<p>The whattodo section will be a little confusing. What we will actually be doing is using yet another anonymous function. I&#8217;ll show you the function code below and then I&#8217;ll show you what our entire tooltip function will look like.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><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>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The entire function now looks like this</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> $_jx<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">tooltip</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>trigger<span style="color: #339933;">,</span>target<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> trigger <span style="color: #339933;">=</span> trigger<span style="color: #339933;">;</span> 
    <span style="color: #003366; font-weight: bold;">var</span> target <span style="color: #339933;">=</span> target<span style="color: #339933;">;</span> 
    <span style="color: #003366; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> offset<span style="color: #339933;">;</span> 
    document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>trigger<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover'</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>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It isn&#8217;t pretty, but it is effective. That will cause our &#8220;target&#8221; to by displayed when you hover over the &#8220;trigger&#8221;. However, if you move your mouse off the trigger, the target stays displayed. That is easily fixed and is essentially a copy/paste of the addEventListener section that we have right now. We will only change mouseover to mouseout and the value for document.getElementById(target).style.display to none.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> $_jx<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">tooltip</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>trigger<span style="color: #339933;">,</span>target<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> trigger <span style="color: #339933;">=</span> trigger<span style="color: #339933;">;</span> 
    <span style="color: #003366; font-weight: bold;">var</span> target <span style="color: #339933;">=</span> target<span style="color: #339933;">;</span> 
    <span style="color: #003366; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> offset<span style="color: #339933;">;</span> 
    document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>trigger<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseover'</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>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>trigger<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseout,function (){
        document.getElementById(target).style.display = '</span>none<span style="color: #3366CC;">'; 
    },false); 
  }
}</span></pre></div></div>

<p>And there you go. To use this all you will need to do is the following.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">1. Include the script after &lt;/body&gt; Since the tooltips don't need to be seen until AFTER the page is loaded, don't waste precious load time loading up the script. 
&nbsp;
&lt;script src=&quot;filename.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
<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>
$_jx <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> $_jx<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
$_jx.<span style="color: #660066;">tooltip</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'trigger'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'target'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>You just need to make sure that style=&#8221;display: none&#8221; is present in the target&#8217;s tag. Other than that, no modifications need to be made to your existing code to add tooltips. </p>
]]></content:encoded>
			<wfw:commentRss>http://wheremy.feethavebeen.com/2009/07/tooltips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

