<?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>The Why and The How &#187; Javascript</title> <atom:link href="http://www.thewhyandthehow.com/tag/javascript/feed/" rel="self" type="application/rss+xml" /><link>http://www.thewhyandthehow.com</link> <description>Just another WordPress weblog</description> <lastBuildDate>Fri, 09 Jul 2010 02:32:51 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.8.4</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>SCP for Komodo IDE</title><link>http://www.thewhyandthehow.com/scp-for-komodo-ide/</link> <comments>http://www.thewhyandthehow.com/scp-for-komodo-ide/#comments</comments> <pubDate>Tue, 18 Aug 2009 00:15:33 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Development]]></category> <category><![CDATA[Javascript]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Technology]]></category><guid
isPermaLink="false">http://www.thewhyandthehow.com/?p=746</guid> <description><![CDATA[I have just finished developing my first Komodo IDE plugin. I love Komodo. It&#8217;s the first IDE for PHP that I&#8217;ve been very, very happy using. I&#8217;ve run it on Windows, Linux and now on a MacBook. The only frustration I&#8217;ve had is that Komodo doesn&#8217;t support basic integrated file uploading via SCP. While it [...]]]></description> <content:encoded><![CDATA[<p>I have just finished developing my first <a
href="http://www.activestate.com/komodo/">Komodo IDE</a> plugin. I love Komodo. It&#8217;s the first IDE for PHP that I&#8217;ve been very, very happy using. I&#8217;ve run it on Windows, Linux and now on a MacBook. The only frustration I&#8217;ve had is that Komodo doesn&#8217;t support basic integrated file uploading via SCP. While it is possible to save a file remotely, there is no simple way to save a file locally and upload it to a mapped location on a remote server from within the IDE.</p><p>Fortunately Komodo also provides a simple method for creating extensions within the IDE itself, so I gave it a try. Within a few hours I was able to build an extension to do exactly what I needed and configure it on a per-project basis. I&#8217;ve made the extension available as an <a
href="http://code.google.com/p/scp-for-komodo-ide/">MIT-licensed, open-source project</a>. As always, this project is included on the <a
href="http://www.thewhyandthehow.com/open-source-tools/">open source tools page</a>.</p> ]]></content:encoded> <wfw:commentRss>http://www.thewhyandthehow.com/scp-for-komodo-ide/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Improve Google Analytics load delays using jQuery</title><link>http://www.thewhyandthehow.com/improve-google-analytics-load-delays-using-jquery/</link> <comments>http://www.thewhyandthehow.com/improve-google-analytics-load-delays-using-jquery/#comments</comments> <pubDate>Thu, 14 May 2009 23:19:02 +0000</pubDate> <dc:creator>Blake Schwendiman</dc:creator> <category><![CDATA[Javascript]]></category> <category><![CDATA[Technology]]></category> <category><![CDATA[jQuery]]></category><guid
isPermaLink="false">http://www.thewhyandthehow.com/?p=700</guid> <description><![CDATA[I&#8217;ve spoken about the complex interdependencies of modern web applications before so today&#8217;s massive failure of Google services (#GoogleFail) reminds us again to be careful and deliberate when building our applications.
For my own sites, I&#8217;ve noticed occasional slowdowns while loading Google Analytics. While the analytics are important to me, they&#8217;re not important to you (my [...]]]></description> <content:encoded><![CDATA[<p>I&#8217;ve spoken about the <a
href="http://www.thewhyandthehow.com/twitter-oauth-vulnerability-and-service-interconnectedness/">complex interdependencies of modern web applications</a> before so today&#8217;s <a
href="http://www.wired.com/epicenter/2009/05/when-google-goes-down-it-goes-down-hard/">massive failure of Google services</a> (<a
href="http://search.twitter.com/search?q=%23GoogleFail">#GoogleFail</a>) reminds us again to be careful and deliberate when building our applications.</p><p>For my own sites, I&#8217;ve noticed occasional slowdowns while loading Google Analytics. While the analytics are important to me, they&#8217;re not important to you (my readers) and if waiting for a component to load causes a bad experience on the site, you assume it&#8217;s just a bad site.</p><p>Fortunately there is (as always) a jQuery option to help mitigate this problem. I found one solution called the <a
href="http://aktagon.com/projects/jquery/google-analytics/">jQuery Google Analyics plugin</a> that does much more than just defer the loading of Google Analytics, but the key feature I was looking for was the deferral. This plugin appears to be a good solution to the problem and if you study the source code, you&#8217;ll see that making your own deferred Google Analytics loader shouldn&#8217;t be too hard.</p><p>I haven&#8217;t tried it yet, but it looks like the code will be something like this:</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p7002');">[<span
id="p7002_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p700code2'); return false;">View Code</a> JAVASCRIPT</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p7002"><td
class="code" id="p700code2"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> load_and_track_ga<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> account_id <span style="color: #339933;">=</span> <span style="color: #3366CC;">'UA-XXX-XXX'</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> host <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;https:&quot;</span> <span style="color: #339933;">==</span> document.<span style="color: #006600;">location</span>.<span style="color: #006600;">protocol</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;https://ssl.&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> src <span style="color: #339933;">=</span> host <span style="color: #339933;">+</span> <span style="color: #3366CC;">'google-analytics.com/ga.js'</span><span style="color: #339933;">;</span>
    $.<span style="color: #006600;">getScript</span><span style="color: #009900;">&#40;</span>src<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><span style="color: #000066; font-weight: bold;">typeof</span> _gat <span style="color: #339933;">!=</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        pageTracker <span style="color: #339933;">=</span> _gat._getTracker<span style="color: #009900;">&#40;</span>account_id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        pageTracker._trackPageview<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #3366CC;">&quot;_gat is undefined&quot;</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;">&#125;</span></pre></td></tr></table></div><p>What are your thoughts? Have you tried this or something like it? Does it improve your site performance?</p> ]]></content:encoded> <wfw:commentRss>http://www.thewhyandthehow.com/improve-google-analytics-load-delays-using-jquery/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>jQuery date chooser</title><link>http://www.thewhyandthehow.com/jquery-date-chooser/</link> <comments>http://www.thewhyandthehow.com/jquery-date-chooser/#comments</comments> <pubDate>Tue, 12 May 2009 17:25:41 +0000</pubDate> <dc:creator>Blake Schwendiman</dc:creator> <category><![CDATA[Javascript]]></category> <category><![CDATA[Tutorial]]></category> <category><![CDATA[jQuery]]></category> <category><![CDATA[jquery ui]]></category><guid
isPermaLink="false">http://www.thewhyandthehow.com/?p=687</guid> <description><![CDATA[Providing a simple, consistent method for allowing your end-users to enter a date in a form is another great reason to use jQuery and jQuery UI. There have been many date selection tools created &#8212; you may have invented one yourself &#8212; but few are as great as the jQuery UI datepicker.
As with all things [...]]]></description> <content:encoded><![CDATA[<p>Providing a simple, consistent method for allowing your end-users to enter a date in a form is another great reason to use jQuery and jQuery UI. There have been many date selection tools created &#8212; you may have invented one yourself &#8212; but few are as great as the <a
href="http://jqueryui.com/demos/datepicker/">jQuery UI datepicker</a>.</p><p>As with all things jQuery, there is very little required. Just set up a form, add a quick call to jQuery and you&#8217;re done. By default, when the datepicker is bound to an edit input, all that is required is to click on the edit (or tab into it) and the calendar control will appear.</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p6875');">[<span
id="p6875_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p687code5'); return false;">View Code</a> JAVASCRIPT</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p6875"><td
class="code" id="p687code5"><pre class="javascript"><span style="color: #339933;">&lt;</span>form<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>fieldset<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>label<span style="color: #339933;">&gt;</span>Select a date<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;the_date&quot;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;the_date&quot;</span> <span style="color: #339933;">/&gt;&lt;/</span>label<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>fieldset<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span>
&nbsp;
<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>
  jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#the_date'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">datepicker</span><span style="color: #009900;">&#40;</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></td></tr></table></div><p>Here it is in action (click on the input field):</p><form
class="example"><fieldset><label>Select a date:<br
/> <input
type="text" id="the_date" name="the_date" /></label></fieldset></form><p><script type="text/javascript">jQuery('#the_date').datepicker();</script></p><p>Personally, I like a little more visual indication on date fields. Providing a calendar icon to the right of the input field has become a de facto standard on the web, so I&#8217;ll show how simple it is to add that to a jQuery UI datepicker.</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p6876');">[<span
id="p6876_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p687code6'); return false;">View Code</a> JAVASCRIPT</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p6876"><td
class="code" id="p687code6"><pre class="javascript"><span style="color: #339933;">&lt;</span>form<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>fieldset<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>label<span style="color: #339933;">&gt;</span>Select a date<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;the_date&quot;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;the_date&quot;</span> <span style="color: #339933;">/&gt;&lt;/</span>label<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>fieldset<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span>
&nbsp;
<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>
  jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#the_date'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">datepicker</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>showOn<span style="color: #339933;">:</span> <span style="color: #3366CC;">'both'</span><span style="color: #339933;">,</span> buttonImage<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/wp-content/themes/thesis/custom/images/ical.png'</span><span style="color: #339933;">,</span> buttonImageOnly<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</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></td></tr></table></div><p>As you can see, I added a small set of options to the <strong>datepicker()</strong> method. The showOn option indicates to show the calendar when the button is clicked and when the control has focus. The buttonImage option is the URL to the image and the buttonImageOnly option indicates to display the image alone, not as part of an actual button control. Visit the jQuery UI site for more <a
href="http://jqueryui.com/demos/datepicker/">datepicker control options</a>.</p><p>Here it is with the calendar button (<a
href="http://www.smashingmagazine.com/2009/01/23/friday-freebies-flavours-icon-set-and-cute-tweeters-icon-set/">download the button image here</a>):</p><form
class="example"><fieldset><label>Select a date:<br
/> <input
type="text" id="the_date2" name="the_date" /></label></fieldset></form><p><script type="text/javascript">jQuery('#the_date2').datepicker({showOn:'both',buttonImage:'/wp-content/themes/thesis/custom/images/ical_sm.png',buttonImageOnly:true});</script></p><p>The <a
href="http://jqueryui.com/demos/datepicker/">jQuery UI datepicker</a> provides a great deal of flexibility and control with a minimum required effort. Check it out for yourself!</p> ]]></content:encoded> <wfw:commentRss>http://www.thewhyandthehow.com/jquery-date-chooser/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Using Google docs as a data store</title><link>http://www.thewhyandthehow.com/using-google-docs-as-a-data-store/</link> <comments>http://www.thewhyandthehow.com/using-google-docs-as-a-data-store/#comments</comments> <pubDate>Wed, 06 May 2009 21:20:00 +0000</pubDate> <dc:creator>Blake Schwendiman</dc:creator> <category><![CDATA[Ajax]]></category> <category><![CDATA[Javascript]]></category> <category><![CDATA[Tutorial]]></category> <category><![CDATA[jQuery]]></category> <category><![CDATA[google docs]]></category><guid
isPermaLink="false">http://www.thewhyandthehow.com/?p=615</guid> <description><![CDATA[Have you ever wished you had a web-accessible data store for a project but you either can&#8217;t or prefer not to create a database? Recently while developing a Google gadget, I ran into just that scenario. The gadget needs to display some mostly read only data. By mostly, I mean that the data needs to [...]]]></description> <content:encoded><![CDATA[<p>Have you ever wished you had a web-accessible data store for a project but you either can&#8217;t or prefer not to create a database? Recently while developing a Google gadget, I ran into just that scenario. The gadget needs to display some <em>mostly</em> read only data. By mostly, I mean that the data needs to be editable, but the primary function of the gadget is display.</p><p>I really didn&#8217;t want to set up a special server for the data and build a database and the connector PHP for retrieving the data via Ajax because that&#8217;s a lot of work. The great news is that Google provides a direct solution via <a
href="http://docs.google.com/">Google docs</a>.</p><p><a
href="http://www.thewhyandthehow.com/wp-content/uploads/2009/05/google-docs-spreadsheet-sample.png"><img
src="http://www.thewhyandthehow.com/wp-content/uploads/2009/05/google-docs-spreadsheet-sample-150x150.png" alt="google-docs-spreadsheet-sample" title="google-docs-spreadsheet-sample" width="150" height="150" class="left frame size-thumbnail wp-image-668" /></a>To start, I simply created a new spreadsheet and entered the data. Next, using the <strong>share</strong> button, I selected <strong>publish as web page</strong>. Finally, to get the data in a format that I can easily consume using Javascript, I clicked the <strong>more publishing options</strong> link on the publish page to generate the URL to download a CSV version of the spreadsheet.</p><p><a
href="http://www.thewhyandthehow.com/wp-content/uploads/2009/05/google-docs-publish-options.png"><img
src="http://www.thewhyandthehow.com/wp-content/uploads/2009/05/google-docs-publish-options-150x150.png" alt="google-docs-publish-options" title="google-docs-publish-options" width="150" height="150" class="right frame size-thumbnail wp-image-670" /></a>Within minutes, I had created a Javascript-accessible data store that it easy to update, shareable and hosted via Google. To access the data in the spreadsheet, I now use the URL provided by the sharing dialogs, <a
href="http://spreadsheets.google.com/pub?key=rocYeX7smxw1E2FFdhwsiiQ&#038;output=csv&#038;gid=0">http://spreadsheets.google.com/pub?key=rocYeX7smxw1E2FFdhwsiiQ&#038;output=csv&#038;gid=0</a>.</p><p>Now with just a little bit of jQuery, I can easily access the data in the spreadsheet and use it however I like. For example:</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p6158');">[<span
id="p6158_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p615code8'); return false;">View Code</a> JAVASCRIPT</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p6158"><td
class="code" id="p615code8"><pre class="javascript"><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>
  jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #006600;">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>
    jQuery.<span style="color: #006600;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'proxy.php'</span><span style="color: #339933;">,</span> 
                    <span style="color: #009900;">&#123;</span>uri<span style="color: #339933;">:</span> <span style="color: #3366CC;">'http://spreadsheets.google.com/pub?key=rocYeX7smxw1E2FFdhwsiiQ&amp;output=csv&amp;gid=0'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 
                    <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>resp<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                      do_somthing_with<span style="color: #009900;">&#40;</span>resp<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>
  <span style="color: #009900;">&#125;</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></td></tr></table></div><p>Obviously, the <strong>do_somthing_with</strong> function above would be able to do anything with the result data. Also, notice that I&#8217;m using a content proxy (example only) to retrieve the data. For more details about that, read my post about <a
href="http://www.thewhyandthehow.com/building-an-ajax-content-proxy-in-php/">building a simple content proxy in PHP</a>. Below I have written a simple example that retrieves the data and displays it in the following container. The code for the example is identical to the above with the exception of displaying the results.</p><pre id="sample_docs_data" style="margin: 0 12px 0 12px;"></pre><p><script type="text/javascript">/*<![CDATA[*/jQuery(document).ready(function(){jQuery.post('/wp-content/themes/thesis/custom/ajax/proxy.php',{uri:'http://spreadsheets.google.com/pub?key=rocYeX7smxw1E2FFdhwsiiQ&#038;output=csv&#038;gid=0'},function(resp){jQuery('#sample_docs_data').html(resp);});});/*]]>*/</script></p><p>As you can see, using Google documents as a data store can be a valuable addition to your web site implementation. The advantages include easy, sharable access to updating the data, always-on access to the data, and multiple formats for retrieving the data. Several features supported by Google docs publishing such as filtering and querying data and retrieving JSON results have not be covered here.</p> ]]></content:encoded> <wfw:commentRss>http://www.thewhyandthehow.com/using-google-docs-as-a-data-store/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Building an Ajax content proxy in PHP</title><link>http://www.thewhyandthehow.com/building-an-ajax-content-proxy-in-php/</link> <comments>http://www.thewhyandthehow.com/building-an-ajax-content-proxy-in-php/#comments</comments> <pubDate>Tue, 05 May 2009 17:21:47 +0000</pubDate> <dc:creator>Blake Schwendiman</dc:creator> <category><![CDATA[Development]]></category> <category><![CDATA[Javascript]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Technology]]></category> <category><![CDATA[Tutorial]]></category> <category><![CDATA[jQuery]]></category> <category><![CDATA[Ajax]]></category><guid
isPermaLink="false">http://www.thewhyandthehow.com/?p=644</guid> <description><![CDATA[Last week I wanted to write an article about using Google docs as a data store for use in Javascript, but in order to do so, I first need to provide a basic article about content proxies.
For security reasons, Ajax requests are only allowed to domains from which the code originates. For example, if your [...]]]></description> <content:encoded><![CDATA[<p>Last week I wanted to write an article about using Google docs as a data store for use in Javascript, but in order to do so, I first need to provide a basic article about content proxies.</p><p>For security reasons, Ajax requests are only allowed to domains from which the code originates. For example, if your site is at example.com, and your Javascript is served from that domain, you may only use Ajax to make HTTP requests to the same domain, example.com. If you want to retrieve data from other web services such as <a
href="http://developer.yahoo.com/finance/">financial information</a> or <a
href="http://developer.yahoo.com/finance/">weather</a> from <a
href="http://developer.yahoo.com/">Yahoo!</a>, you cannot make a direct Ajax request to yahoo.com, but you can make a request to a content proxy on example.com that gets the data from Yahoo! and returns it for you.</p><p>A very simple content proxy in PHP is implemented (using curl) like this:</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p64412');">[<span
id="p64412_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p644code12'); return false;">View Code</a> PHP</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p64412"><td
class="code" id="p644code12"><pre class="php"><span style="color: #666666; font-style: italic;">/**
* perform a remote connection request and return the results
* 
* @param string uri of remote resource
* @param int timeout in seconds
* @param int connection timeout in seconds
* 
* @return string data returned from uri or null
*/</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> remote_load<span style="color: #009900;">&#40;</span><span style="color: #000033;">$uri</span><span style="color: #339933;">,</span> <span style="color: #000033;">$timeout</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #000033;">$connect_timeout</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000033;">$curl_handle</span> <span style="color: #339933;">=</span> curl_init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  curl_setopt<span style="color: #009900;">&#40;</span><span style="color: #000033;">$curl_handle</span><span style="color: #339933;">,</span> CURLOPT_USERAGENT<span style="color: #339933;">,</span> <span style="color: #0000ff;">'example.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  curl_setopt<span style="color: #009900;">&#40;</span><span style="color: #000033;">$curl_handle</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000033;">$uri</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  curl_setopt<span style="color: #009900;">&#40;</span><span style="color: #000033;">$curl_handle</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  curl_setopt<span style="color: #009900;">&#40;</span><span style="color: #000033;">$curl_handle</span><span style="color: #339933;">,</span> CURLOPT_CONNECTTIMEOUT<span style="color: #339933;">,</span> <span style="color: #000033;">$connect_timeout</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  curl_setopt<span style="color: #009900;">&#40;</span><span style="color: #000033;">$curl_handle</span><span style="color: #339933;">,</span> CURLOPT_TIMEOUT<span style="color: #339933;">,</span> <span style="color: #000033;">$timeout</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  curl_setopt<span style="color: #009900;">&#40;</span><span style="color: #000033;">$curl_handle</span><span style="color: #339933;">,</span> CURLOPT_FOLLOWLOCATION<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000033;">$buffer</span>   <span style="color: #339933;">=</span> curl_exec<span style="color: #009900;">&#40;</span><span style="color: #000033;">$curl_handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000033;">$curlinfo</span> <span style="color: #339933;">=</span> curl_getinfo<span style="color: #009900;">&#40;</span><span style="color: #000033;">$curl_handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  curl_close<span style="color: #009900;">&#40;</span><span style="color: #000033;">$curl_handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$curlinfo</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'http_code'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">400</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$curlinfo</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'http_code'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000033;">$buffer</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>The above code would be included in a simple page such as:</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p64413');">[<span
id="p64413_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p644code13'); return false;">View Code</a> PHP</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p64413"><td
class="code" id="p644code13"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'the_function_above'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// you really should check for and handle errors</span>
  <a href="http://www.php.net/print"><span style="color: #990000;">print</span></a><span style="color: #009900;">&#40;</span>remote_load<span style="color: #009900;">&#40;</span><span style="color: #000033;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uri'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div><p>If the above page is called proxy.php on your site, then you can now make Ajax requests that look like:</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p64414');">[<span
id="p64414_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p644code14'); return false;">View Code</a> JAVASCRIPT</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p64414"><td
class="code" id="p644code14"><pre class="javascript">jQuery.<span style="color: #006600;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http://www.example.com/proxy.php?uri=http://www.google.com/'</span><span style="color: #339933;">,</span>
  <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    do_something_with_result<span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p>There are several considerations to make when developing a content proxy that haven&#8217;t been shown here. The first two that come to mind are security and caching. Requests to third-party services should be cached locally on your server to ensure that your application doesn&#8217;t make too many requests and potentially become blacklisted by the third-party service. Additionally, if you build a content proxy, you should ensure that only requests from your approved callers are satisfied. You don&#8217;t want other sites making proxy requests through your servers for many reasons.</p><p>The proxy shown above may be used as a starting point for a more secure and robust content proxy, though, and will be the basis for some of my upcoming Ajax articles.</p> ]]></content:encoded> <wfw:commentRss>http://www.thewhyandthehow.com/building-an-ajax-content-proxy-in-php/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>jQuery tabs</title><link>http://www.thewhyandthehow.com/jquery-tabs/</link> <comments>http://www.thewhyandthehow.com/jquery-tabs/#comments</comments> <pubDate>Thu, 30 Apr 2009 23:49:02 +0000</pubDate> <dc:creator>Blake Schwendiman</dc:creator> <category><![CDATA[Javascript]]></category> <category><![CDATA[Tutorial]]></category> <category><![CDATA[jQuery]]></category> <category><![CDATA[jquery ui]]></category> <category><![CDATA[tabs]]></category><guid
isPermaLink="false">http://www.thewhyandthehow.com/?p=632</guid> <description><![CDATA[I&#8217;ve finally had a chance to implement a solution with jQuery tabs and I was very pleased with the simplicity of creating a very simple tabbed interface. I&#8217;ve used several tab solutions before the jQuery UI solution, and I definitely prefer the jQuery solution (yet again).
To set up a basic tabbed layout, start with an [...]]]></description> <content:encoded><![CDATA[<p>I&#8217;ve finally had a chance to implement a solution with jQuery tabs and I was very pleased with the simplicity of creating a very simple tabbed interface. I&#8217;ve used several tab solutions before the jQuery UI solution, and I definitely prefer the jQuery solution (yet again).</p><p>To set up a basic tabbed layout, start with an enclosing container followed immediately by an unordered list containing the tabs and then the containers for the actual tab content. For example:</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p63216');">[<span
id="p63216_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p632code16'); return false;">View Code</a> JAVASCRIPT</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p63216"><td
class="code" id="p632code16"><pre class="javascript"><span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;example_tabs&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;#example_tab_1&quot;</span><span style="color: #339933;">&gt;</span>Tab <span style="color: #CC0000;">1</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;#example_tab_2&quot;</span><span style="color: #339933;">&gt;</span>Tab <span style="color: #CC0000;">1</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;#example_tab_3&quot;</span><span style="color: #339933;">&gt;</span>Tab <span style="color: #CC0000;">1</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;example_tab_1&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>Content <span style="color: #000066; font-weight: bold;">for</span> tab <span style="color: #CC0000;">1</span><span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;example_tab_2&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>Content <span style="color: #000066; font-weight: bold;">for</span> tab <span style="color: #CC0000;">2</span><span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;example_tab_3&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>Content <span style="color: #000066; font-weight: bold;">for</span> tab <span style="color: #CC0000;">3</span><span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<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>
  jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #006600;">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>
    jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#example_tabs'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">tabs</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div><p>And here&#8217;s the result:</p><div
id="example_tabs"><ul><li><a
href="#example_tab_1">Tab 1</a></li><li><a
href="#example_tab_2">Tab 2</a></li><li><a
href="#example_tab_3">Tab 3</a></li></ul><div
id="example_tab_1"><p>Content for tab 1</p></p></div><div
id="example_tab_2"><p>Content for tab 2</p></p></div><div
id="example_tab_3"><p>Content for tab 3</p></p></div></div><p><script type="text/javascript">jQuery(document).ready(function(){jQuery('#example_tabs').tabs();});</script><br
/> Obvious styling issues notwithstanding, I can&#8217;t imagine an easier way to build a tabbed interface for a web page. As always, <a
href="http://docs.jquery.com/UI/Tabs">read the manual</a> for more information and enjoy!</p> ]]></content:encoded> <wfw:commentRss>http://www.thewhyandthehow.com/jquery-tabs/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>jQuery autocomplete</title><link>http://www.thewhyandthehow.com/jquery-autocomplete/</link> <comments>http://www.thewhyandthehow.com/jquery-autocomplete/#comments</comments> <pubDate>Wed, 29 Apr 2009 00:44:12 +0000</pubDate> <dc:creator>Blake Schwendiman</dc:creator> <category><![CDATA[Javascript]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Technology]]></category> <category><![CDATA[Tutorial]]></category> <category><![CDATA[jQuery]]></category> <category><![CDATA[Ajax]]></category><guid
isPermaLink="false">http://www.thewhyandthehow.com/?p=603</guid> <description><![CDATA[Autocompletion of web-based forms has become so common that it has reached the point of being an expectation. Fortunately (again), there is a jQuery solution that makes this a simple addition to any form.
The first step is to download the jQuery Autocomplete plugin and check out the documentation. Make sure that you include the script [...]]]></description> <content:encoded><![CDATA[<link
rel="stylesheet" href="/wp-content/themes/thesis/custom/css/jquery.autocomplete.css" /><script type="text/javascript" src="/wp-content/themes/thesis/custom/js/jquery.autocomplete.min.js"></script><p>Autocompletion of web-based forms has become so common that it has reached the point of being an expectation. Fortunately (again), there is a jQuery solution that makes this a simple addition to any form.</p><p>The first step is to download the <a
href="http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/">jQuery Autocomplete plugin</a> and check out the <a
href="http://docs.jquery.com/Plugins/Autocomplete">documentation</a>. Make sure that you include the script and the CSS file, then build your form as normal and add a single call to the autocomplete plugin.</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p60320');">[<span
id="p60320_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p603code20'); return false;">View Code</a> JAVASCRIPT</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p60320"><td
class="code" id="p603code20"><pre class="javascript"><span style="color: #339933;">&lt;</span>form<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>fieldset<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>label<span style="color: #339933;">&gt;</span>Type <span style="color: #000066; font-weight: bold;">in</span> your favorite color<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;color_input&quot;</span> <span style="color: #339933;">/&gt;&lt;/</span>label<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>fieldset<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span>
<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>
  jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#color_input'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">autocomplete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'black'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'white'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'red'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'green'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'blue'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'yellow'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'purple'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'brown'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'silver'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'magenta'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'cyan'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'gold'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'goldenrod'</span><span style="color: #009900;">&#93;</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></td></tr></table></div><p>As you can see, the example above uses a simple Javascript array as the input to the <strong>autocomplete</strong> method. This implies a local, static list of lookup items. Here is the example:</p><form
class="example"><fieldset><label>Type in your favorite color:<br
/> <input
type="text" id="color_input" /></label></fieldset></form><p><script type="text/javascript">jQuery('#color_input').autocomplete(['black','white','red','green','blue','yellow','purple','brown','silver','magenta','cyan','gold','goldenrod']);</script></p><p>Now, if you want to make things really interesting, take the above idea and tie it together with an Ajax lookup. That way, the suggested items can be any list that pertains to the specific user and action related to the input. For example, the input box may be a list of counties for a specific state in the US. It may be a list of friends from a social network. It may be a book title or author from Amazon.</p><p>The basic code for an Ajax-based lookup is the same, but the parameter to the <strong>autocomplete</strong> function is a string URL instead of an array of items.</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p60321');">[<span
id="p60321_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p603code21'); return false;">View Code</a> JAVASCRIPT</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p60321"><td
class="code" id="p603code21"><pre class="javascript"><span style="color: #339933;">&lt;</span>form<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>fieldset<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>label<span style="color: #339933;">&gt;</span>Type <span style="color: #000066; font-weight: bold;">in</span> your favorite color<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text&quot;</span> id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;color_input&quot;</span> <span style="color: #339933;">/&gt;&lt;/</span>label<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>fieldset<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span>
<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>
  jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#color_input'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">autocomplete</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/relative/path/to/script'</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></td></tr></table></div><p>The path to the script file can be either a relative or absolute URL. When the autocomplete function calls your custom script, it passes two parameters, <strong>q</strong> and <strong>limit</strong>. The <strong>q</strong> parameter is the value that the end-user has typed into the box and the <strong>limit</strong> is the number of items to return. See the <a
href="http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions">jQuery autocomplete documentation</a> for more details.</p><p>For this example, I have written a very simple PHP script that shows how this might work on a typical web site. In the example, I use the <a
href="http://www.geonames.org/export/geonames-search.html">Geonames service</a> to lookup place names based on what is being typed. The PHP script looks like this:</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p60322');">[<span
id="p60322_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p603code22'); return false;">View Code</a> PHP</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p60322"><td
class="code" id="p603code22"><pre class="php">  <span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'custom_functions.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000033;">$query</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_key_exists"><span style="color: #990000;">array_key_exists</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'q'</span><span style="color: #339933;">,</span> <span style="color: #000033;">$_GET</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000033;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'q'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000033;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://ws.geonames.org/search?q='</span> <span style="color: #339933;">.</span> <span style="color: #000033;">$query</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&amp;maxRows=10&amp;type=json'</span><span style="color: #339933;">;</span>
  <span style="color: #000033;">$result</span> <span style="color: #339933;">=</span> Helpers<span style="color: #339933;">::</span><span style="color: #004000;">remoteLoad</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$url</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'geoname-'</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/md5"><span style="color: #990000;">md5</span></a><span style="color: #009900;">&#40;</span><span style="color: #000033;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000033;">$result</span> <span style="color: #339933;">=</span> json_decode<span style="color: #009900;">&#40;</span><span style="color: #000033;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000033;">$output_items</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">geonames</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000033;">$output_items</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <a href="http://www.php.net/print"><span style="color: #990000;">print</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/implode"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000033;">$output_items</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p>I haven&#8217;t included the full include file source, but obviously there is a class which does remote URL loading. This portion is not important to the example. The relevant information is that the script looks up a list of place names by calling out to the remote service and then prints a carriage-return delimited list of names.</p><p>The autocomplete plug-in takes care of all the rest. Here is the resulting form. For example, type in <strong>London</strong> or <strong>New York</strong> to see how it works:</p><form
class="example"><fieldset><label>Enter a place name:<br
/> <input
type="text" id="place_input" /></label></fieldset></form><p><script type="text/javascript">jQuery('#place_input').autocomplete('/wp-content/themes/thesis/custom/ajax/places.php');</script></p><p>There are many other options to the autocomplete plugin that you should explore on the <a
href="http://docs.jquery.com/Plugins/Autocomplete">documentation page</a>, however you can see how quickly and simply a basic autocomplete field can be created using the defaults.</p> ]]></content:encoded> <wfw:commentRss>http://www.thewhyandthehow.com/jquery-autocomplete/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>jQuery parent(s)</title><link>http://www.thewhyandthehow.com/jquery-parents/</link> <comments>http://www.thewhyandthehow.com/jquery-parents/#comments</comments> <pubDate>Mon, 27 Apr 2009 17:08:56 +0000</pubDate> <dc:creator>Blake Schwendiman</dc:creator> <category><![CDATA[Javascript]]></category> <category><![CDATA[Tutorial]]></category> <category><![CDATA[jQuery]]></category> <category><![CDATA[dom]]></category><guid
isPermaLink="false">http://www.thewhyandthehow.com/?p=597</guid> <description><![CDATA[I was recently working on an unordered list in which each item is displayed with a link that does two things: first, the metadata for the item is stored in a database via Ajax; second, the item is removed from the on-screen list so it can&#8217;t be chosen again.
For example, you might display a list [...]]]></description> <content:encoded><![CDATA[<p>I was recently working on an unordered list in which each item is displayed with a link that does two things: first, the metadata for the item is stored in a database via Ajax; second, the item is removed from the on-screen list so it can&#8217;t be chosen again.</p><p>For example, you might display a list of items in a shopping cart with a <strong>delete</strong> link for each. On delete, the item is removed from the current session&#8217;s cart (the Ajax portion) and the item is also removed from the display. In some cases the approach might be to perform the Ajax data update and return the entire list as the result of the call, thus performing the entire two-step process at once. For my recent implementation, I didn&#8217;t want to regenerate the list on the <a
href="http://www.webhostingsearch.com/dedicated-server.php">hosting server</a> each time, so I opted for the two-step process.</p><p>In the process, I learned a new jQuery method and selector option that are so cool that I had to share. The HTML for my list looked something like the following:</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p59726');">[<span
id="p59726_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p597code26'); return false;">View Code</a> JAVASCRIPT</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p59726"><td
class="code" id="p597code26"><pre class="javascript"><span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>img ...<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>dl<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>dt<span style="color: #339933;">&gt;</span>Title<span style="color: #339933;">&lt;/</span>dt<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>dd<span style="color: #339933;">&gt;</span>Some additional meta data<span style="color: #339933;">&lt;/</span>dd<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>dd<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;updateDB(); ???; return false;&quot;</span><span style="color: #339933;">&gt;</span>delete<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>dd<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>dl<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>img ...<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>dl<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>dt<span style="color: #339933;">&gt;</span>Title<span style="color: #339933;">&lt;/</span>dt<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>dd<span style="color: #339933;">&gt;</span>Some additional meta data<span style="color: #339933;">&lt;/</span>dd<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>dd<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;updateDB(); ???; return false;&quot;</span><span style="color: #339933;">&gt;</span>delete<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>dd<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>dl<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;!--</span> more list items <span style="color: #000066; font-weight: bold;">in</span> the same format <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span></pre></td></tr></table></div><p>The containers inside the list items provide all of the layout (via CSS), so they&#8217;re necessary to make everything look right. The click handler for the <strong>delete</strong> link calls <em>updateDB</em> which is the Ajax function that performs the actual server update. In place of the <em>???</em> I added the following code which removes the entire list item from the view:</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p59727');">[<span
id="p59727_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p597code27'); return false;">View Code</a> JAVASCRIPT</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p59727"><td
class="code" id="p597code27"><pre class="javascript">  jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">parents</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li:eq(0)'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p>The <strong><a
href="http://docs.jquery.com/API/1.2/Traversing/parents">parents()</a></strong> method returns all of the parents of an element that match the selector. Simply calling <strong>parents(&#8217;li&#8217;)</strong> for the anchor tag in the example above may have worked, but if the list happened to be embedded within another list, it&#8217;s possible that I would have inadvertently removed a series of parent items instead of just the immediate parent that I wanted. To ensure that I only acted on the immediate parent, I included the <a
href="http://docs.jquery.com/Selectors">basic filter</a>, <strong><a
href="http://docs.jquery.com/Selectors/eq#index">eq(0)</a></strong> to the selector to ensure that the subsequent calls (<em><a
href="http://docs.jquery.com/Effects/fadeOut#speedcallback">fadeOut</a></em>) only act on the first item found.</p><p>The final code looks like this:</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p59728');">[<span
id="p59728_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p597code28'); return false;">View Code</a> JAVASCRIPT</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p59728"><td
class="code" id="p597code28"><pre class="javascript"><span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>img ...<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>dl<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>dt<span style="color: #339933;">&gt;</span>Title<span style="color: #339933;">&lt;/</span>dt<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>dd<span style="color: #339933;">&gt;</span>Some additional meta data<span style="color: #339933;">&lt;/</span>dd<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>dd<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;updateDB(); jQuery(this).parents('li:eq(0)').fadeOut(); return false;&quot;</span><span style="color: #339933;">&gt;</span>delete<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>dd<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>dl<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;!--</span> more list items <span style="color: #000066; font-weight: bold;">in</span> the same format <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span></pre></td></tr></table></div><p>jQuery again solves the cumbersome process of traversing the DOM searching for parent elements manually by providing rich electors, methods and filters.</p> ]]></content:encoded> <wfw:commentRss>http://www.thewhyandthehow.com/jquery-parents/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Get latitude/longitude of Google map</title><link>http://www.thewhyandthehow.com/get-latitudelongitude-of-google-map/</link> <comments>http://www.thewhyandthehow.com/get-latitudelongitude-of-google-map/#comments</comments> <pubDate>Fri, 10 Apr 2009 23:51:18 +0000</pubDate> <dc:creator>Blake Schwendiman</dc:creator> <category><![CDATA[General]]></category> <category><![CDATA[Javascript]]></category> <category><![CDATA[geolocation]]></category> <category><![CDATA[google map]]></category> <category><![CDATA[latitude]]></category> <category><![CDATA[longitude]]></category><guid
isPermaLink="false">http://www.thewhyandthehow.com/?p=486</guid> <description><![CDATA[I do a fair amount of work where I need to quickly get the latitude and longitude of a given address. Most of the time I just head over to maps.google.com and type in the address, then use the link button and copy/paste just the latitude and longitude. I find this method to be workable [...]]]></description> <content:encoded><![CDATA[<p>I do a fair amount of work where I need to quickly get the latitude and longitude of a given address. Most of the time I just head over to <a
href="http://maps.google.com">maps.google.com</a> and type in the address, then use the link button and copy/paste just the latitude and longitude. I find this method to be workable in most cases, but I found a little <a
href="http://www.tech-recipes.com/rx/2403/google_maps_get_latitude_longitude_values/">Javascript snippet</a> that works well to display exactly what I need in a prompt.</p><p><a
href="http://www.thewhyandthehow.com/wp-content/uploads/2009/04/menu.png"><img
src="http://www.thewhyandthehow.com/wp-content/uploads/2009/04/menu.png" alt="menu" title="menu" width="144" height="270" class="alignleft size-full wp-image-487" /></a>The Javascript method works well, but it&#8217;s a little inconvenient when I&#8217;m copying and pasting several addresses, the JS code and the resulting geolocations in one session. Today I was doing this again and it struck me how convenient it would be to have a browser button that would do what the Javascript does. So I set that up.</p><p>In Firefox, I simply added a custom bookmark to my Bookmarks toolbar (View, Toolbars, Bookmark Toolbars). To do this, assuming the Bookmark toolbar is visible, right click (or control click for Mac users) on it and select New Bookmark. In the properties box, enter this Javascript into the location field:</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p48630');">[<span
id="p48630_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p486code30'); return false;">View Code</a> JAVASCRIPT</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p48630"><td
class="code" id="p486code30"><pre class="javascript">javascript<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">void</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">prompt</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #339933;">,</span>gApplication.<span style="color: #006600;">getMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">getCenter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p><a
href="http://www.thewhyandthehow.com/wp-content/uploads/2009/04/properties.png"><img
src="http://www.thewhyandthehow.com/wp-content/uploads/2009/04/properties-300x200.png" alt="properties" title="properties" width="300" height="200" class="alignright size-medium wp-image-488" /></a>Now you have a button that will display the latitude and longitude of the current map (on the Google maps site) in a convenient Javascript prompt box. Like I said, this isn&#8217;t going to change your life, but it might save you a few cycles &#8230; and that&#8217;s worth something.</p> ]]></content:encoded> <wfw:commentRss>http://www.thewhyandthehow.com/get-latitudelongitude-of-google-map/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>jQuery modal dialog</title><link>http://www.thewhyandthehow.com/jquery-modal-dialog/</link> <comments>http://www.thewhyandthehow.com/jquery-modal-dialog/#comments</comments> <pubDate>Thu, 09 Apr 2009 20:41:51 +0000</pubDate> <dc:creator>Blake Schwendiman</dc:creator> <category><![CDATA[Javascript]]></category> <category><![CDATA[Tutorial]]></category> <category><![CDATA[jQuery]]></category> <category><![CDATA[dialog]]></category> <category><![CDATA[jquery ui]]></category> <category><![CDATA[tooltip]]></category><guid
isPermaLink="false">http://www.thewhyandthehow.com/?p=473</guid> <description><![CDATA[Coming from the world of Windows software development, the concept of a modal dialog box is an absolute expectation. Most often such dialogs are used to gather required information from the user while ensuring that nothing is clicked or altered in the main application window. When moving to the web world, it&#8217;s a bit jarring [...]]]></description> <content:encoded><![CDATA[<p>Coming from the world of Windows software development, the concept of a modal dialog box is an absolute expectation. Most often such dialogs are used to gather required information from the user while ensuring that nothing is clicked or altered in the main application window. When moving to the web world, it&#8217;s a bit jarring to discover that there is no ubiquitous built-in modal dialog*.</p><p>jQuery and jQuery UI provide an overlay-based approach to solving this problem that is both visually appealing and functionally complete. And, as with all things jQuery, it is very simple to implement.</p><p><a
href="#" onclick="jQuery('#dialog').dialog('open'); return false">Click here to see the popup</a> (unless you&#8217;re reading this in a news reader, then you&#8217;ll need to <a
href="http://www.thewhyandthehow.com/jquery-modal-dialog/">view the post on site</a>).</p><p>This sample is very simple to code in HTML and jQuery. Basically I provide a hidden DIV element containing the message to display and then initiate the dialog on page load. The link for the popup is also shown:</p><div
id="wp_codebox_msgheader"><span
class="right"><a
href="javascript:;" onclick="toggle_collapse('p47332');">[<span
id="p47332_symbol">-</span>]</a><sup><a
href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span
style="color: #99cc00">?</span></a></sup></span><span
class="left"><a
href="javascript:;" onclick="javascript:showCodeTxt('p473code32'); return false;">View Code</a> JAVASCRIPT</span><div
class="codebox_clear"></div></div><div
id="wp_codebox"><table
width="100%" ><tr
id="p47332"><td
class="code" id="p473code32"><pre class="javascript"><span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;dialog&quot;</span> title<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Example dialog&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>Some text that you want to display to the user.<span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<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>
  jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #006600;">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>
    jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#dialog&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
      bgiframe<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> autoOpen<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> modal<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</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>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;#&quot;</span> onclick<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;jQuery('#dialog').dialog('open'); return false&quot;</span><span style="color: #339933;">&gt;</span>Click here to see the popup<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span></pre></td></tr></table></div><p>There are many more options describe on the <a
href="http://jqueryui.com/demos/dialog/">jQuery UI dialog</a> pages. One of the reasons that I prefer the jQuery UI dialog to any of the other overlay dialog/tooltip solutions is that the jQuery UI version is so easy to implement and has very good defaults. By default the dialog is draggable, sizeable and includes the appropriate control element for closing the dialog. Surprisingly many other solutions don&#8217;t have these features by default.</p><div
id="dialog" title="Example dialog"><p>Some text that you want to display to the user.</p></div><p
class="note"><em>*Footnote: Yes, IE does have a modal popup window option, I know. It&#8217;s extremely non-standard and only supported in IE, so it&#8217;s not a reasonable solution in most cases.</em></p><p><script type="text/javascript">jQuery(document).ready(function(){jQuery("#dialog").dialog({bgiframe:true,autoOpen:false,height:100,modal:true});});</script></p> ]]></content:encoded> <wfw:commentRss>http://www.thewhyandthehow.com/jquery-modal-dialog/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss><!--
This site's performance optimized by W3 Total Cache:

W3 Total Cache improves the user experience of your blog by caching
frequent operations, reducing the weight of various files and providing
transparent content delivery network integration.

Learn more about our WordPress Plugins: http://www.w3-edge.com/wordpress-plugins/

Minified using memcached
Page Caching using memcached
Database Caching 10/17 queries in 0.011 seconds using memcached

Served from: a2.c0.354a.static.theplanet.com @ 2010-09-10 04:05:56 -->