<?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>IONCANNON &#187; javascript</title>
	<atom:link href="http://www.ioncannon.net/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ioncannon.net</link>
	<description>Thoughts on Software Development and Engineering</description>
	<lastBuildDate>Tue, 03 Jan 2012 13:59:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
	<atom:link rel='hub' href='http://www.ioncannon.net/?pushpress=hub'/>
		<item>
		<title>Range Requests with Ajax</title>
		<link>http://www.ioncannon.net/programming/1506/range-requests-with-ajax/</link>
		<comments>http://www.ioncannon.net/programming/1506/range-requests-with-ajax/#comments</comments>
		<pubDate>Tue, 22 Nov 2011 16:03:58 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/?p=1506</guid>
		<description><![CDATA[I ran across something the other day that made wonder about doing range requests using ajax. For some reason it wasn&#039;t obvious at first if this would be easy but as it turns out it is. If you aren&#039;t familiar with range requests head over to the HTTP RFC and check out the range header. [...]]]></description>
			<content:encoded><![CDATA[<p>I ran across something the other day that made wonder about doing range requests using ajax. For some reason it wasn&#039;t obvious at first if this would be easy but as it turns out it is.</p>
<p>If you aren&#039;t familiar with range requests head over to the HTTP RFC and check out the <a href="http://tools.ietf.org/html/rfc2616#section-14.35.2">range header</a>. Your web server needs to support range requests for this to be useful but most do so that shouldn&#039;t be a huge issue. As a bonus you will find that some CDNs support range request as well (Amazon S3 for example).</p>
<p><span id="more-1506"></span></p>
<p>To show how this works I&#039;ll start off by getting the offsets of a text file, then use curl to show the actual request and then put it into an ajax request. Even though I&#039;m using a text file the same thing could be done with a binary file as well. Here are the contents of the file I&#039;ve called range-test.txt:</p>
<pre class="brush: plain; title: ; notranslate">
First part of the file.

More text here.

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Some text after Lorem Ipsum.

End of the file.
</pre>
<p>Using egrep we can get the offset of each line pretty easy like so:</p>
<pre class="brush: plain; title: ; notranslate">
&gt; egrep -b &quot;\r|\n&quot; range-test.txt
0:First part of the file.
25:More text here.
42:Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
618:Some text after Lorem Ipsum.
660:End of the file.
</pre>
<p>Quickly verify that the range looks right in isolation using hexdump:</p>
<pre class="brush: plain; title: ; notranslate">
&gt; hexdump -c -s 618 -n 30 range-test.txt
000026a   S   o   m   e       t   e   x   t       a   f   t   e   r
000027a   L   o   r   e   m       I   p   s   u   m   .  \n  \n
0000288
</pre>
<p>Now we have a sample range so put the text file on your web server and use curl to send the range request headers (note that the hexdump takes an offset and count of bytes while the range header takes an start and end offset):</p>
<pre class="brush: plain; title: ; notranslate">
&gt; curl -H 'Range:bytes=618-647' http://localhost/range-test.txt
Some text after Lorem Ipsum.
</pre>
<p>Now that it makes sense how this range is used to make a request for part of a file we can move on to doing it with an ajax request. I&#039;m going to use <a href="http://jquery.com/">jQuery</a>&#039;s <a href="http://api.jquery.com/jQuery.ajax/">ajax</a> function to make this really simple. It has a feature that lets you set headers really easily. The following is all you need:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;html&gt;
  &lt;head&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;jquery-1.7.min.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
    $(function() {
      $.ajax({
        url: 'range-test.txt',
        headers: {Range: &quot;bytes=618-647&quot;},
        success: function( data ) { $('#results').html( data ); }
      });
    });
    &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id=&quot;results&quot;&gt;&lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>And that is all there is to it. This could be useful for all kinds of things like displaying just a section of a large log file at a time. This might also be useful in a situation where you want to stream binary data back to the browser a chunk at a time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/programming/1506/range-requests-with-ajax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using the Google Closure Compiler in Java</title>
		<link>http://www.ioncannon.net/programming/1447/using-the-google-closure-compiler-in-java/</link>
		<comments>http://www.ioncannon.net/programming/1447/using-the-google-closure-compiler-in-java/#comments</comments>
		<pubDate>Tue, 08 Mar 2011 14:29:55 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/?p=1447</guid>
		<description><![CDATA[I recently had a chance to try out Google&#039;s closure compiler. The closure compiler is similar to the YUI compressor except that along with minimizing it may rewrite the JavaScript. If you want to understand more about what it does start at the overview documentation and then go from there. What I needed was a [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had a chance to try out <a href="http://code.google.com/closure/compiler/">Google&#039;s closure compiler</a>. The closure compiler is similar to the <a href="http://developer.yahoo.com/yui/compressor/">YUI compressor</a> except that along with minimizing it may rewrite the JavaScript. If you want to understand more about what it does start at the <a href="http://code.google.com/closure/compiler/docs/overview.html">overview documentation</a> and then go from there.</p>
<p>What I needed was a way to use the closure compiler in an Ant task. The Ant task that comes with the library is good but there wasn&#039;t a way for me to integrate it into an existing system that wasn&#039;t going to change. After looking around for some example code and not finding any I went into the library&#039;s Ant task and figured out how to wire it all up myself.</p>
<p><span id="more-1447"></span></p>
<p>It isn&#039;t that hard to use the compiler library in your own Java code but without a simple example it takes some work to figure out what is needed and what isn&#039;t. The following code is just about as bare bones as you can get. It takes a number of JavaScript files and compile them using the medium setting, more about the choice of settings after the code:</p>
<pre class="brush: java; title: ; notranslate">
import com.google.javascript.jscomp.*;

import java.io.FileWriter;
import java.util.List;
import java.util.ArrayList;
import java.util.logging.Level;

public class Test
{
  public static void main(String[] args) throws Exception
  {
    // These are external JavaScript files you reference but don't want changed
    String externalJavascriptResources[] = {
        &quot;jquery.js&quot;,
        &quot;jqueryui.js&quot;
    };
    // These are the files you want optimized
    String primaryJavascriptToCompile[] = {
        &quot;somejavascript.js&quot;,
        &quot;otherjavascript.js&quot;
    };
    // This is where the optimized code will end up
    String outputFilename = &quot;combined.min.js&quot;;

    com.google.javascript.jscomp.Compiler.setLoggingLevel(Level.INFO);
    com.google.javascript.jscomp.Compiler compiler = new com.google.javascript.jscomp.Compiler();

    CompilerOptions options = new CompilerOptions();
    CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);

    WarningLevel.VERBOSE.setOptionsForWarningLevel(options);

    List&lt;JSSourceFile&gt; externalJavascriptFiles = new ArrayList&lt;JSSourceFile&gt;();
    for (String filename : externalJavascriptResources)
    {
      externalJavascriptFiles.add(JSSourceFile.fromFile(filename));
    }

    List&lt;JSSourceFile&gt; primaryJavascriptFiles = new ArrayList&lt;JSSourceFile&gt;();
    for (String filename : primaryJavascriptToCompile)
    {
      primaryJavascriptFiles.add(JSSourceFile.fromFile(filename));
    }

    compiler.compile(externalJavascriptFiles, primaryJavascriptFiles, options);

    for (JSError message : compiler.getWarnings())
    {
      System.err.println(&quot;Warning message: &quot; + message.toString());
    }

    for (JSError message : compiler.getErrors())
    {
      System.err.println(&quot;Error message: &quot; + message.toString());
    }

    FileWriter outputFile = new FileWriter(outputFilename);
    outputFile.write(compiler.toSource());
    outputFile.close();
  }
}
</pre>
<p>The above code is doing a number of things:</p>
<ul>
<li>It takes both external resources you don&#039;t want optimized as well as resources you do want optimized that refer to those external resources. You may not need to list external resources depending on what level of optimization you use.</li>
<li>It combines all the input files that are not external into one output file.</li>
<li>It compiles the javascript code given to it using the SIMPLE_OPTIMIZATIONS setting.</li>
<li>It lists any warnings or errors it found while compiling the code.</li>
</ul>
<p>One of the nice side effects of using the closure compiler is that because it compiles the code it can alert you to errors or potential issues using warnings.</p>
<p>What is listed above is using the middle of the road optimizations. There is a level above SIMPLE_OPTIMIZATIONS that you can set by changing that line in the above code to:</p>
<pre class="brush: java; title: ; notranslate">
  CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
</pre>
<p>Be aware that the advanced optimizations will do things, renaming your variables and functions to name two, that can break your code. The compiler gives you a way of alerting it to things you don&#039;t want changed using externs and exports. Before using the advanced option you should read the <a href="http://code.google.com/closure/compiler/docs/api-tutorial3.html">advanced options tutorial</a>.</p>
<p>If you do not like seeing all the steps the compiler takes you can set the logging level to QUIET by changing the WarningLevel line to the following:</p>
<pre class="brush: java; title: ; notranslate">
  WarningLevel.QUIET.setOptionsForWarningLevel(options);
</pre>
<p>One last note about the compiler is that the <a href="http://code.google.com/closure/compiler/docs/inspector.html">closure inspector</a> is a Firebug plugin that will let you see what the original line of code looked like before it was compiled. This can help a lot when something goes wrong.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/programming/1447/using-the-google-closure-compiler-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Modernizr &#8211; Simple Guide and Examples</title>
		<link>http://www.ioncannon.net/programming/1369/using-modernizr-simple-guide-and-examples/</link>
		<comments>http://www.ioncannon.net/programming/1369/using-modernizr-simple-guide-and-examples/#comments</comments>
		<pubDate>Fri, 19 Nov 2010 02:18:07 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[modernizr]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/?p=1369</guid>
		<description><![CDATA[Modernizr is a Javascript library that detects the presence of browser functionality. This makes life a lot easier when using more modern features on your website by encapsulating all the feature tests into a library you don&#039;t have to worry about. You can use Modernizr either in your own Javascript or you can use the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.modernizr.com/">Modernizr</a> is a Javascript library that detects the presence of browser functionality. This makes life a lot easier when using more modern features on your website by encapsulating all the feature tests into a library you don&#039;t have to worry about. You can use Modernizr either in your own Javascript or you can use the CSS classes it sets on the HTML element.</p>
<p>To get a full list of the browser functionality that is tested check out the <a href="http://www.modernizr.com/docs/">Modernizr docs</a>. If the feature you need a test for isn&#039;t available it is fairly easy to add new tests as well.</p>
<p>I&#039;ll start with an example that shows how to use the feature tests in Javascript. After Modernizr runs it populates the &#034;Modernizr&#034; structure with boolean values for each functionality test. This example waits for the page to load and then simply populates a DIV with text depending on your browsers support for web sockets:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;!DOCTYPE html&gt;
&lt;html class=&quot;no-js&quot;&gt;
  &lt;head&gt;
    &lt;title&gt;Modernizr - Javascript Example&lt;/title&gt;

    &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;modernizr.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
      $(document).ready(function()
      {
        if (Modernizr.websockets)
        {
          $(&quot;#result&quot;).html('Your browser has support for Web Sockets');
        }
        else
        {
          $(&quot;#result&quot;).html('Your browser does not support Web Sockets');
        }
      });
    &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;

    &lt;p id=&quot;result&quot;&gt;&lt;/p&gt;

  &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>See this code in action: <a href="http://www.ioncannon.net/examples/modjs.html">Modernizr Javascript example</a>. </p>
<p>You can try this example with an older version of Firefox and the text should indicate that there is no support for web sockets. Try again with a newer version of Google&#039;s Chrome browser and the page will indicate that there is support for web sockets.</p>
<p>The next example shows how to use the Modernizr CSS support. The key here is to know that the library will inject classes into the HTML tag. Those classes correspond to the functionality tests and are listed in the documentation. I am again testing for web sockets support and Modernizr will set the HTML class to either no-websockets or websockets and display one of two DIVs:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;!DOCTYPE html&gt;
&lt;html class=&quot;no-js&quot;&gt;
  &lt;head&gt;
    &lt;title&gt;Modernizr - CSS Example&lt;/title&gt;

    &lt;style type=&quot;text/css&quot; media=&quot;screen&quot;&gt;
      div.wsno, div.wsyes { display: none }
      .no-websockets div.wsno { display: block }
      .websockets div.wsyes { display: block }
    &lt;/style&gt;

    &lt;script src=&quot;modernizr.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;

    &lt;div class=&quot;wsno&quot;&gt;
      Your browser does not support WebSockets.
    &lt;/div&gt;

    &lt;div class=&quot;wsyes&quot;&gt;
      Your browser supports WebSockets.
    &lt;/div&gt;

  &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>See this code in action: <a href="http://www.ioncannon.net/examples/modcss.html">Modernizr CSS example</a>. </p>
<p>For a much more detailed CSS example check out <a href="http://www.alistapart.com/articles/taking-advantage-of-html5-and-css3-with-modernizr/">this A List Apart article</a>.</p>
<p>If you need the information Modernizr assembles outside of the browser check out <a href="https://github.com/jamesgpearce/modernizr-server">Modernizr on the server</a>.</p>
<p>While I was putting together the two demo pages for this I discovered a new service much like the <a href="http://code.google.com/apis/libraries/devguide.html">Google Javascript library CDN</a> but with more Javascript libraries. It is called <a href="http://cachedcommons.org/">Cached Commons</a> and it uses Github&#039;s CDN.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/programming/1369/using-modernizr-simple-guide-and-examples/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Good article on keeping javascript maintainable</title>
		<link>http://www.ioncannon.net/web-design/86/good-article-on-keeping-javascript-maintainable/</link>
		<comments>http://www.ioncannon.net/web-design/86/good-article-on-keeping-javascript-maintainable/#comments</comments>
		<pubDate>Mon, 17 Jul 2006 18:53:45 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[web design]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/web-design/86/good-article-on-keeping-javascript-maintainable/</guid>
		<description><![CDATA[Vitamin has a good article today on the importance of maintainable javascript. 95% of what the article covers is applicable to any code. The important parts of the article cover javascript specific things like: object literals, namespaces and where to go when you want to compress your javascript to save bandwidth/make it download faster. They [...]]]></description>
			<content:encoded><![CDATA[<p>Vitamin has a good article today on <a href="http://www.thinkvitamin.com/features/dev/the-importance-of-maintainable-javascript">the importance of maintainable javascript</a>. 95% of what the article covers is applicable to any code. The important parts of the article cover javascript specific things like: <a href="http://www.wait-till-i.com/index.php?p=239">object literals</a>, <a href="http://www.dustindiaz.com/namespace-your-javascript/">namespaces</a> and where to go when you want to compress your javascript to save bandwidth/make it download faster. They recommend <a href="http://dean.edwards.name/packer/">packer</a> and <a href="http://javascript.crockford.com/jsmin.html">JSMIN</a> as two javascript compressors but I think JSMIN is probably a better bet mainly because you can run it from the command line and make it part of a build script.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/web-design/86/good-article-on-keeping-javascript-maintainable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Easier scrolling images with Yahoo UI</title>
		<link>http://www.ioncannon.net/programming/60/easier-scrolling-images-with-yahoo-ui/</link>
		<comments>http://www.ioncannon.net/programming/60/easier-scrolling-images-with-yahoo-ui/#comments</comments>
		<pubDate>Mon, 03 Jul 2006 23:15:54 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/web-design/60/easier-scrolling-images-with-yahoo-ui/</guid>
		<description><![CDATA[This is a great article about the yahoo ui tools. I mention it mainly because they have a nice scrolling widget a lot like what I put together as an example of the things you can do with script.aculo.us (see Smooth Scrolling Image List). A link to the demo: Sliding demo The entire article: 15 [...]]]></description>
			<content:encoded><![CDATA[<p>This is a great article about the yahoo ui tools. I mention it mainly because they have a nice scrolling widget a lot like what I put together as an example of the things you can do with <a href="http://script.aculo.us/">script.aculo.us</a> (see <a href="http://www.ioncannon.net/javascript/48/smooth-scrolling-image-list/">Smooth Scrolling Image List</a>).</p>
<p>A link to the demo: <a href="http://www.thinkvitamin.com/misc/yui-demos/demo-04.html">Sliding demo</a> <br/></p>
<p>The entire article: <a href="http://www.thinkvitamin.com/features/javascript/15-things-you-can-do-with-yahoo-ui">15 things you can do with yahoo ui</a></p>
<p>Tags: <a href="http://technorati.com/tag/yahoo+ui" rel="tag">yahoo ui</a>, <a href="http://technorati.com/tag/script.aculo.us" rel="tag"> script.aculo.us</a>, <a href="http://technorati.com/tag/javascript" rel="tag"> javascript</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/programming/60/easier-scrolling-images-with-yahoo-ui/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some interesting and useful AJAX/Javascript code</title>
		<link>http://www.ioncannon.net/web-design/63/some-interesting-and-useful-ajaxjavascript-code/</link>
		<comments>http://www.ioncannon.net/web-design/63/some-interesting-and-useful-ajaxjavascript-code/#comments</comments>
		<pubDate>Fri, 23 Jun 2006 15:10:57 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[web design]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/web-design/63/some-interesting-and-useful-ajaxjavascript-code/</guid>
		<description><![CDATA[I like seeing more and more uses of prototype. I&#039;m not sure if the big guys will win out with their UI toolkits (Yahoo UI/GWT) or if it will always feel better to put things together by hand. Either way it is good to understand how this stuff works. This is an edit in place [...]]]></description>
			<content:encoded><![CDATA[<p>I like seeing more and more uses of prototype. I&#039;m not sure if the big guys will win out with their UI toolkits (Yahoo UI/GWT) or if it will always feel better to put things together by hand. Either way it is good to understand how this stuff works. This is an <a href="http://joseph.randomnetworks.com/archives/2006/06/07/ajax-edit-in-place-with-prototype-version-020/">edit in place</a> example that is similar to what you see on flickr.</p>
<p>I&#039;ve seen something like this a number of times when I&#039;ve visited sites. They want your feedback on something or other while you are browsing. It is a little floating plus feedback sign that hangs out in the lower right hand corner. <a href="http://www.ibegin.com/blog/p_ajax_feedback_mechanism.html">Check it out</a>.</p>
<p>Browsing digg I noticed an article on &#034;unobtrusive sidenotes&#034; and found that the idea is pretty cool: The <a href="http://blog.arc90.com/2006/05/introducing_unobtrusive_sideno.php">announcement</a> and <a href="http://lab.arc90.com/2006/05/unobtrusive_sidenotes.php">code</a>. They are done with javascript so you can turn them on and off on the fly.</p>
<p>Tags: <a href="http://technorati.com/tag/javascript" rel="tag">javascript</a>, <a href="http://technorati.com/tag/ajax" rel="tag"> ajax</a>, <a href="http://technorati.com/tag/sidenote" rel="tag"> sidenote</a>, <a href="http://technorati.com/tag/ajax+feedback" rel="tag"> ajax feedback</a>, <a href="http://technorati.com/tag/prototype" rel="tag"> prototype</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/web-design/63/some-interesting-and-useful-ajaxjavascript-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AJAX file upload progress for Java using commons fileupload and prototype</title>
		<link>http://www.ioncannon.net/programming/38/ajax-file-upload-progress-for-java-using-commons-fileupload-and-prototype/</link>
		<comments>http://www.ioncannon.net/programming/38/ajax-file-upload-progress-for-java-using-commons-fileupload-and-prototype/#comments</comments>
		<pubDate>Mon, 03 Apr 2006 02:15:15 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/uncategorized/38/ajax-file-upload-progress-for-java-using-commons-fileupload-and-prototype/</guid>
		<description><![CDATA[This has been done before with PHP (AJAX upload progress meter for PHP) etc but I needed something a little different because I wanted to upload a file and then have it loaded into a database. I looked around and found that someone had already made something that used the commons file upload package to [...]]]></description>
			<content:encoded><![CDATA[<p>This has been done before with PHP (<a href="http://bluga.net/projects/uploadProgressMeter/">AJAX upload progress meter for PHP</a>) etc but I needed something a little different because I wanted to upload a file and then have it loaded into a database. I looked around and found that someone had already made something that used the commons file upload package to do the upload part (<a href="http://www.telio.be/blog/2006/01/06/ajax-upload-progress-monitor-for-commons-fileupload-example/">AJAX Upload progress monitor for Commons-FileUpload Example</a>). It wasn&#039;t exactly what I was looking for but it a good start.</p>
<p><span id="more-38"></span></p>
<p>To understand the way this works I think it is easiest to break it down into parts:</p>
<ol>
<li>A file upload extention that counts bytes as they are uploaded</li>
<li>An interface that monitors the progress of something running on the server</li>
<li>AJAX to pull the monitoring into the current screen</li>
</ol>
<p><a></a></p>
<h3>Counting bytes when files are uploaded</h3>
<p>This was taken from the <a href="http://www.telio.be/blog/2006/01/06/ajax-upload-progress-monitor-for-commons-fileupload-example/">example listed above</a>. It extends and wraps parts of the commons File Upload classes so that you can count the bytes as they are uploaded to the server. You can download the <a href="http://www.ioncannon.net/examples/fileupload-ext.zip">source with build file</a> or the <a href="http://www.ioncannon.net/examples/fileupload-ext1.zip">binary</a>. You will also need the <a href="http://jakarta.apache.org/site/downloads/downloads_commons-fileupload.cgi">commons file upload</a>, <a href="http://jakarta.apache.org/site/downloads/downloads_commons-io.cgi">commons io</a> and <a href="http://jakarta.apache.org/site/downloads/downloads_commons-logging.cgi">commons logging</a>. If you download the source put the commons jars in the lib directory before building.</p>
<p>The code is fairly simple to follow. MonitoredDiskFileItemFactory replaces DiskFileItemFactory and the construction of a MonitoredDiskFileItemFactory takes a OutputStreamListener that will be passed on down the chain. The new factory creates MonitoredDiskFileItems instead of DiskFileItems for each file uploaded. When the file needs to be written to disk a MonitoredOutputStream is given back instead of a normal OutputStream. The MonitoredOutputStream calls the OutputStreamListener methods as the bytes are written and with that you now have a way to monitor the byte count as the file is created on the server.</p>
<p>Now to test this all out we can just have an OutputStreamListener that writes its progress out to a logfile or something.</p>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw1">class</span> FileUploadListener <span class="kw1">implements</span> OutputStreamListener<br />
<span class="br0">&#123;</span><br />
&nbsp; <span class="kw1">private</span> <span class="kw4">long</span> totalFileSize<span class="sy0">;</span><br />
&nbsp; <span class="kw1">private</span> <span class="kw4">long</span> currentFileRead<span class="sy0">;</span></p>
<p>&nbsp; <span class="kw1">public</span> FileUploadListener<span class="br0">&#40;</span><span class="kw4">long</span> totalFileSize<span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">totalFileSize</span> <span class="sy0">=</span> totalFileSize<span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">currentFileRead</span> <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span><br />
&nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; <span class="kw1">public</span> <span class="kw4">void</span> start<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; log.<span class="me1">debug</span><span class="br0">&#40;</span><span class="st0">&quot;Upload started. Total file size: &quot;</span> <span class="sy0">+</span> totalFileSize<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; <span class="kw1">public</span> <span class="kw4">void</span> bytesRead<span class="br0">&#40;</span><span class="kw4">int</span> byteCount<span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; log.<span class="me1">debug</span><span class="br0">&#40;</span><span class="st0">&quot;Read bytes. Currently &quot;</span> <span class="sy0">+</span> byteCount <span class="sy0">+</span> <span class="st0">&quot; out of &quot;</span> <span class="sy0">+</span> totalFileSize <span class="sy0">+</span> <span class="st0">&quot; bytes.&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; currentFileRead<span class="sy0">+=</span>byteCount<span class="sy0">;</span><br />
&nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; <span class="kw1">public</span> <span class="kw4">void</span> error<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> error<span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; log.<span class="me1">debug</span><span class="br0">&#40;</span><span class="st0">&quot;Hit an error: &quot;</span> <span class="sy0">+</span> error<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; <span class="kw1">public</span> <span class="kw4">void</span> done<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; log.<span class="me1">debug</span><span class="br0">&#40;</span><span class="st0">&quot;Upload done.&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; <span class="kw1">public</span> <span class="kw4">long</span> getTotalRead<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> currentFileRead<span class="sy0">;</span><br />
&nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; <span class="kw1">public</span> <span class="kw4">long</span> getTotalSize<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> totalFileSize<span class="sy0">;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Now we try it out. You can put this in a servlet or jsp so I&#039;m only going to list the parts that matter.</p>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;">FileUploadListener listener <span class="sy0">=</span> <span class="kw1">new</span> FileUploadListener<span class="br0">&#40;</span>request.<span class="me1">getContentLength</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; session.<span class="me1">setAttribute</span><span class="br0">&#40;</span><span class="st0">&quot;LISTENER&quot;</span>, listener<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; FileItemFactory factory <span class="sy0">=</span> <span class="kw1">new</span> MonitoredDiskFileItemFactory<span class="br0">&#40;</span>listener<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; ServletFileUpload upload <span class="sy0">=</span> <span class="kw1">new</span> ServletFileUpload<span class="br0">&#40;</span>factory<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">List</span></a> items <span class="sy0">=</span> upload.<span class="me1">parseRequest</span><span class="br0">&#40;</span>request<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aiterator+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Iterator</span></a> i <span class="sy0">=</span> items.<span class="me1">iterator</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> i.<span class="me1">hasNext</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; FileItem fileItem <span class="sy0">=</span> <span class="br0">&#40;</span>FileItem<span class="br0">&#41;</span> i.<span class="me1">next</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span>fileItem.<span class="me1">isFormField</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// code here to process the file</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;<span class="br0">&#125;</span></div>
</div>
<p>I&#039;m going to assume you can find the correct way to do the actual form upload part.</p>
<p>Note: One issue that you will face at some point is where the upload post goes to becuase when you get to the AJAXy part of things you want the post to stay on the same page. You can use a hidden iframe and the form&#039;s &#034;target&#034; parameter to do this (I have an example later). This is one thing the Java examples I found didn&#039;t have but the PHP examples did and I&#039;m not sure exactly how the Java examples work without it.</p>
<h3>Monitoring progress on the server</h3>
<p>The next step is to monitor the progress of the upload on the server. What you are monitoring on the server doesn&#039;t even need to be the upload. For the work I was doing the upload goes fairly quickly but what happens to the file after the upload takes a little longer. I wanted to monitor both and that is one reason I think it helps to break this up into parts because you aren&#039;t limited to just monitoring file uploads.</p>
<p>The main thing to keep in mind here is that the application server is multithreaded and you can make more than one request to the server at the same time. You probably know that you can open a tab in firefox or another window in ie and use the same session from the current webapp you are using. Knowing that you can create a page that monitors the status of things as they are running on the server. </p>
<p>From the example above you could toss the listener into the users session. Then insead of logging you just add a couple variables to keep track of the number of bytes that have been uploaded. Then create a simple jsp that pulls the Listener out of the session and dumps its data to a page. Open two windows, one to the upload page and another one to the status page. Start the upload and then start refreshing the status. You should see that the values change as the file is uploaded.</p>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="sy0">&lt;%</span>@page<span class="sy0">%&gt;</span><br />
<span class="sy0">&lt;%</span><br />
&nbsp; FileUploadListener listener <span class="sy0">=</span> <span class="br0">&#40;</span>FileUploadListener<span class="br0">&#41;</span>session.<span class="me1">getAttribute</span><span class="br0">&#40;</span><span class="st0">&quot;LISTENER&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="sy0">%&gt;</span><br />
Total size<span class="sy0">:</span> <span class="sy0">&lt;%=</span>listener.<span class="me1">getTotalSize</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">%&gt;&lt;</span>br<span class="sy0">/&gt;</span><br />
Read count<span class="sy0">:</span> <span class="sy0">&lt;%=</span>listener.<span class="me1">getTotalRead</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">%&gt;&lt;</span>br<span class="sy0">/&gt;</span></div>
</div>
<p>Of course you will probably want more than just the total size and bytes read as well as more formating like a little progress bar or something but I&#039;ll leave that up to you. </p>
<h3>AJAX integration with prototype</h3>
<p>You have the major parts to the upload progress done and now all you need is the AJAX part. To do this I chose to use <a href="http://prototype.conio.net/">prototype</a> because it cuts right to what you want to do. One call is all you need to use: Ajax.PeriodicalUpdater.</p>
<p>The Ajax.PeriodicalUpdater call will update a container (in my case a div) on a set interval. Here is an example of how to have it update a div with an id of &#034;status&#034; every second.</p>
<div class="codesnip-container" >
<div class="javascript codesnip" style="font-family:monospace;"><span class="kw2">new</span> Ajax.<span class="me1">PeriodicalUpdater</span><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#039;status&#039;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#039;status.jsp&#039;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>asynchronous<span class="sy0">:</span><span class="kw2">true</span><span class="sy0">,</span> frequency<span class="sy0">:</span><span class="nu0">1</span><span class="sy0">,</span> method<span class="sy0">:</span><span class="st0">&#039;get&#039;</span><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</div>
<p>The first argument is the id of the div, the second is the jsp that contains the data to stick into the div every second and the 3rd arguement is a set of options. There are more options availabe if you need them.</p>
<p>You would want to kick the update off whenever the form is posted. When the post is complete the iframe used as a place to post to will load with the results of the servlet or jsp that you posted to. If you return some javascript as a result for the iframe you will be able to create a final &#034;finished&#034; message on the page to let the user know the upload has completed and stop the processing of the AJAX updater.</p>
<p>So there you have it. The basics of setting up an upload progress bar using java and AJAX. I have left out a good bit but you should have enough to at least get you started.</p>
<p>By request I have created a simple example that pulls everything together. The source contains everything you need to create a war file including all source and an ant build file.</p>
<p><a href="http://www.ioncannon.net/examples/testupload.zip">Example Source</a>
</p>
<p>Tags: <a href="http://technorati.com/tag/java" rel="tag">java</a>, <a href="http://technorati.com/tag/ajax" rel="tag"> ajax</a>, <a href="http://technorati.com/tag/file+upload" rel="tag"> file upload</a>, <a href="http://technorati.com/tag/prototype" rel="tag"> prototype</a>, <a href="http://technorati.com/tag/javascript" rel="tag"> javascript</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/programming/38/ajax-file-upload-progress-for-java-using-commons-fileupload-and-prototype/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Smooth scrolling image list</title>
		<link>http://www.ioncannon.net/programming/48/smooth-scrolling-image-list/</link>
		<comments>http://www.ioncannon.net/programming/48/smooth-scrolling-image-list/#comments</comments>
		<pubDate>Thu, 16 Feb 2006 07:26:03 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/uncategorized/48/smooth-scrolling-image-list/</guid>
		<description><![CDATA[I had someone ask me about fitting more images in a small area and the way flickr does their image scrolling came to mind. I wanted to see how hard it was to do and as it turns out it isn&#039;t hard to do at all thanks to script.aculo.us. I&#039;m making this as simple as [...]]]></description>
			<content:encoded><![CDATA[<p>I had someone ask me about fitting more images in a small area and the way flickr does their image scrolling came to mind. I wanted to see how hard it was to do and as it turns out it isn&#039;t hard to do at all thanks to <a href="http://script.aculo.us/">script.aculo.us</a>.</p>
<p>I&#039;m making this as simple as I can to illustrate just how nice the new tools like script.aculo.us are.</p>
<p><strong>Step 1.</strong> You need a box and some images. You will also need to know the size of the images or at least the size of the largest one. In an attempt to keep this simple I&#039;m going to assume all the images are the same size.</p>
<p><span id="more-48"></span></p>
<div class="codesnip-container" >
<div class="html4strict codesnip" style="font-family:monospace;"><span class="sc2">&lt;<a href="http://december.com/html/4/element/div.html"><span class="kw2">div</span></a> <span class="kw3">id</span><span class="sy0">=</span><span class="st0">&quot;imageBox&quot;</span>&gt;</span><br />
&nbsp; <span class="sc2">&lt;<a href="http://december.com/html/4/element/div.html"><span class="kw2">div</span></a> <span class="kw3">id</span><span class="sy0">=</span><span class="st0">&quot;imageBoxInside&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span class="sc2">&lt;<a href="http://december.com/html/4/element/img.html"><span class="kw2">img</span></a> <span class="kw3">src</span><span class="sy0">=</span><span class="st0">&quot;http://www.ioncannon.net/examples/slide/images/turtle_sm_1.jpg&quot;</span> <span class="sy0">/</span>&gt;</span><br />
&nbsp; &nbsp; <span class="sc2">&lt;<a href="http://december.com/html/4/element/img.html"><span class="kw2">img</span></a> <span class="kw3">src</span><span class="sy0">=</span><span class="st0">&quot;http://www.ioncannon.net/examples/slide/images/elephants_sm_1.jpg&quot;</span> <span class="sy0">/</span>&gt;</span><br />
&nbsp; &nbsp; <span class="sc2">&lt;<a href="http://december.com/html/4/element/img.html"><span class="kw2">img</span></a> <span class="kw3">src</span><span class="sy0">=</span><span class="st0">&quot;http://www.ioncannon.net/examples/slide/images/turtle_sm_2.jpg&quot;</span> <span class="sy0">/</span>&gt;</span><br />
&nbsp; &nbsp; <span class="sc2">&lt;<a href="http://december.com/html/4/element/img.html"><span class="kw2">img</span></a> <span class="kw3">src</span><span class="sy0">=</span><span class="st0">&quot;http://www.ioncannon.net/examples/slide/images/elephants_sm_2.jpg&quot;</span> <span class="sy0">/</span>&gt;</span><br />
&nbsp; &nbsp; <span class="sc2">&lt;<a href="http://december.com/html/4/element/img.html"><span class="kw2">img</span></a> <span class="kw3">src</span><span class="sy0">=</span><span class="st0">&quot;http://www.ioncannon.net/examples/slide/mages/turtle_sm_3.jpg&quot;</span> <span class="sy0">/</span>&gt;</span><br />
&nbsp; &nbsp; <span class="sc2">&lt;<a href="http://december.com/html/4/element/img.html"><span class="kw2">img</span></a> <span class="kw3">src</span><span class="sy0">=</span><span class="st0">&quot;http://www.ioncannon.net/examples/slide/images/elephants_sm_3.jpg&quot;</span> <span class="sy0">/</span>&gt;</span><br />
&nbsp; &nbsp; <span class="sc2">&lt;<a href="http://december.com/html/4/element/img.html"><span class="kw2">img</span></a> <span class="kw3">src</span><span class="sy0">=</span><span class="st0">&quot;http://www.ioncannon.net/examples/slide/images/turtle_sm_4.jpg&quot;</span> <span class="sy0">/</span>&gt;</span><br />
&nbsp; &nbsp; <span class="sc2">&lt;<a href="http://december.com/html/4/element/img.html"><span class="kw2">img</span></a> <span class="kw3">src</span><span class="sy0">=</span><span class="st0">&quot;http://www.ioncannon.net/examples/slide/images/elephants_sm_4.jpg&quot;</span> <span class="sy0">/</span>&gt;</span><br />
&nbsp; &nbsp; <span class="sc2">&lt;<a href="http://december.com/html/4/element/br.html"><span class="kw2">br</span></a><span class="sy0">/</span>&gt;</span><br />
&nbsp; <span class="sc2">&lt;<span class="sy0">/</span><a href="http://december.com/html/4/element/div.html"><span class="kw2">div</span></a>&gt;</span><br />
<span class="sc2">&lt;<span class="sy0">/</span><a href="http://december.com/html/4/element/div.html"><span class="kw2">div</span></a>&gt;</span></div>
</div>
<p>Notice from this code that we have an outer box and an inner box. We will next want to make the outer box smaller than the inner box so that only a few of the images can be seen at one time.</p>
<p><strong>Step 2.</strong> To hide the extra images we give the outer box a set width, in this case I want to show only 2 images at a time and each image is 180px wide so I make the outer box 360px wide. Notice that the overflow is hidden. The hidden overflow is what keeps the images that are in the inner box but not within the outer box&#039;s width hidden.</p>
<p>I&#039;m using floats to lay the images out one next to the other. Because of this we need to give the inside box a large width so that it will not wrap the floated images.</p>
<div class="codesnip-container" >
<div class="css codesnip" style="font-family:monospace;">&lt;style type<span class="sy0">=</span><span class="st0">&quot;text/css&quot;</span><span class="sy0">&gt;</span><br />
<span class="re0">#imageBox</span> <span class="br0">&#123;</span> <span class="kw1">margin</span><span class="sy0">:</span> <span class="kw2">auto</span><span class="sy0">;</span> <span class="kw1">width</span><span class="sy0">:</span> <span class="re3">360px</span><span class="sy0">;</span> <span class="kw1">border</span><span class="sy0">:</span> <span class="re3">1px</span> <span class="re0">#000</span> <span class="kw2">solid</span><span class="sy0">;</span> <span class="kw1">overflow</span><span class="sy0">:</span> <span class="kw2">hidden</span><span class="sy0">;</span> <span class="br0">&#125;</span><br />
<span class="re0">#imageBoxInside</span> <span class="br0">&#123;</span> <span class="kw1">width</span><span class="sy0">:</span> <span class="re3">10000px</span><span class="sy0">;</span> <span class="br0">&#125;</span> &nbsp;<span class="re0">#imageBox</span> img <span class="br0">&#123;</span> <span class="kw1">float</span><span class="sy0">:</span> <span class="kw1">left</span><span class="sy0">;</span> <span class="kw1">padding</span><span class="sy0">:</span> <span class="re3">0px</span><span class="sy0">;</span> <span class="kw1">margin</span><span class="sy0">:</span> <span class="re3">0px</span><span class="sy0">;</span> <span class="br0">&#125;</span><br />
<span class="re0">#imageBox</span> br <span class="br0">&#123;</span> <span class="kw1">clear</span><span class="sy0">:</span> <span class="kw2">both</span><span class="sy0">;</span> <span class="br0">&#125;</span><br />
&lt;/style<span class="sy0">&gt;</span></div>
</div>
<p><strong>Step 3.</strong> Now for the magic. You need the latest version of script.aculo.us because I use the Effects.Move function and they have changed Effects.MoveBy to Effects.Move only recently.</p>
<p>We now create two functions to move the images ether one step to the right or one step to the left. Each step is the size of a single image so the when one is hit it moves one image out of thew view and another image into view. As you can see from the following code this is extremely easy using the script.aculo.us library.</p>
<div class="codesnip-container" >
<div class="javascript codesnip" style="font-family:monospace;"><span class="kw2">function</span> moveToPrevious<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; <span class="kw2">new</span> Effect.<span class="me1">Move</span><span class="br0">&#40;</span><span class="st0">&#039;imageBoxInside&#039;</span><span class="sy0">,</span> <span class="br0">&#123;</span> x<span class="sy0">:</span> 180<span class="sy0">,</span> y<span class="sy0">:</span> 0<span class="sy0">,</span> transition<span class="sy0">:</span> Effect.<span class="me1">Transitions</span>.<span class="me1">sinoidal</span> <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span> &nbsp;</p>
<p><span class="kw2">function</span> moveToNext<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; <span class="kw2">new</span> Effect.<span class="me1">Move</span><span class="br0">&#40;</span><span class="st0">&#039;imageBoxInside&#039;</span><span class="sy0">,</span> <span class="br0">&#123;</span> x<span class="sy0">:</span> <span class="sy0">-</span>180<span class="sy0">,</span> y<span class="sy0">:</span> 0<span class="sy0">,</span> transition<span class="sy0">:</span> Effect.<span class="me1">Transitions</span>.<span class="me1">sinoidal</span> <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p><strong>Step 4.</strong> The only thing that remains is to connect everything together. We add a couple links to move call the next and previous functions defined above.</p>
<div class="codesnip-container" >
<div class="html4strict codesnip" style="font-family:monospace;"><span class="sc2">&lt;<a href="http://december.com/html/4/element/a.html"><span class="kw2">a</span></a> <span class="kw3">href</span><span class="sy0">=</span><span class="st0">&quot;javascript:void(0);&quot;</span> <span class="kw3">href</span><span class="sy0">=</span><span class="st0">&quot;javascript:void(0);&quot;</span> <span class="kw3">onclick</span><span class="sy0">=</span><span class="st0">&quot;moveToPrevious(); return true;&quot;</span>&gt;&lt;<a href="http://december.com/html/4/element/img.html"><span class="kw2">img</span></a> <span class="kw3">src</span><span class="sy0">=</span><span class="st0">&quot;http://www.ioncannon.net/examples/slide/images/previous.png&quot;</span> <span class="sy0">/</span>&gt;&lt;<span class="sy0">/</span><a href="http://december.com/html/4/element/a.html"><span class="kw2">a</span></a>&gt;</span><br />
<span class="sc2">&lt;<a href="http://december.com/html/4/element/a.html"><span class="kw2">a</span></a> <span class="kw3">href</span><span class="sy0">=</span><span class="st0">&quot;javascript:void(0);&quot;</span> <span class="kw3">href</span><span class="sy0">=</span><span class="st0">&quot;javascript:void(0);&quot;</span> <span class="kw3">onclick</span><span class="sy0">=</span><span class="st0">&quot;moveToNext(); return true;&quot;</span>&gt;&lt;<a href="http://december.com/html/4/element/img.html"><span class="kw2">img</span></a> <span class="kw3">src</span><span class="sy0">=</span><span class="st0">&quot;http://www.ioncannon.net/examples/slide/images/next.png&quot;</span> <span class="sy0">/</span>&gt;&lt;<span class="sy0">/</span><a href="http://december.com/html/4/element/a.html"><span class="kw2">a</span></a>&gt;</span></div>
</div>
<p>And that is all there is to it. See it in <a href="http://www.ioncannon.net/examples/slide/example1.html">action</a>!
</p>
<p>Tags: <a href="http://technorati.com/tag/javascript" rel="tag">javascript</a>, <a href="http://technorati.com/tag/script.aculo.us" rel="tag"> script.aculo.us</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/programming/48/smooth-scrolling-image-list/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>No javascript detection</title>
		<link>http://www.ioncannon.net/web-design/50/no-javascript-detection/</link>
		<comments>http://www.ioncannon.net/web-design/50/no-javascript-detection/#comments</comments>
		<pubDate>Thu, 16 Feb 2006 01:08:44 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[web design]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/uncategorized/50/no-javascript-detection/</guid>
		<description><![CDATA[Have you ever wondered what you can do if someone doesn&#039;t have javascript turned on and it is needed on the page they are sitting at? Well here is the answer: If the user doesn&#039;t have javascript turned on they will be redirected to the page nojscript.html. It doesn&#039;t work as well as having a [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever wondered what you can do if someone doesn&#039;t have javascript turned on and it is needed on the page they are sitting at? Well here is the answer:</p>
<div class="codesnip-container" >
<div class="html4strict codesnip" style="font-family:monospace;"><span class="sc2">&lt;<a href="http://december.com/html/4/element/html.html"><span class="kw2">html</span></a>&gt;</span><br />
&nbsp; <span class="sc2">&lt;<a href="http://december.com/html/4/element/head.html"><span class="kw2">head</span></a>&gt;</span><br />
&nbsp; &nbsp; <span class="sc2">&lt;<a href="http://december.com/html/4/element/noscript.html"><span class="kw2">noscript</span></a>&gt;&lt;<a href="http://december.com/html/4/element/meta.html"><span class="kw2">meta</span></a> <span class="kw3">http-equiv</span><span class="sy0">=</span><span class="st0">&quot;refresh&quot;</span> <span class="kw3">content</span><span class="sy0">=</span><span class="st0">&quot;0; URL=nojscript.html&quot;</span><span class="sy0">/</span>&gt;&lt;<span class="sy0">/</span><a href="http://december.com/html/4/element/noscript.html"><span class="kw2">noscript</span></a>&gt;</span><br />
&nbsp; <span class="sc2">&lt;<span class="sy0">/</span><a href="http://december.com/html/4/element/head.html"><span class="kw2">head</span></a>&gt;</span><br />
&nbsp; <span class="sc2">&lt;<a href="http://december.com/html/4/element/body.html"><span class="kw2">body</span></a>&gt;</span><br />
&nbsp; &nbsp; A javascript test.<br />
&nbsp; <span class="sc2">&lt;<span class="sy0">/</span><a href="http://december.com/html/4/element/body.html"><span class="kw2">body</span></a>&gt;</span><br />
<span class="sc2">&lt;<span class="sy0">/</span><a href="http://december.com/html/4/element/html.html"><span class="kw2">html</span></a>&gt;</span></div>
</div>
<p>If the user doesn&#039;t have javascript turned on they will be redirected to the page nojscript.html. It doesn&#039;t work as well as having a page in between that does the detection but if you have to do it on the page you need the javascript on this is the way to go.
</p>
<p>Tags: <a href="http://technorati.com/tag/javascript" rel="tag">javascript</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/web-design/50/no-javascript-detection/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

