<?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>jonathan stegall: creative tension &#187; JavaScript</title>
	<atom:link href="http://jonathanstegall.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonathanstegall.com</link>
	<description>culture, design, spirituality</description>
	<lastBuildDate>Fri, 04 May 2012 02:34:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>★ jQuery Auto Suggest with MySQL</title>
		<link>http://jonathanstegall.com/2009/09/28/jquery-auto-suggest-with-mysql/</link>
		<comments>http://jonathanstegall.com/2009/09/28/jquery-auto-suggest-with-mysql/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 04:29:55 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[design]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jquery auto suggest]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://jonathanstegall.com/?p=2024</guid>
		<description><![CDATA[Recently, I was building a site at work that benefited from an auto suggest feature in some of the form fields. This, of course, is the kind of thing that you see when you do Google searches. I've done some similar things in the past, but I've never had a solid, consistent solution that I could use for this kind of thing.

In knowing this, I spent some time looking at a large number of <a href="http://jquery.com/">jQuery</a> plugins that do this. For some time, I couldn't find any that fit the specific need I had. There was one plugin that I thought would work, but I couldn't get it to work with a MySQL database, as it was designed to work with plain text lists in PHP. It may seem pathetic, but I couldn't translate it.]]></description>
			<content:encoded><![CDATA[<p>Recently, I was building a site at work that benefited from an auto suggest feature in some of the form fields. This, of course, is the kind of thing that you see when you do Google searches. I&#8217;ve done some similar things in the past, but I&#8217;ve never had a solid, consistent solution that I could use for this kind of thing.</p>
<p>In knowing this, I spent some time looking at a large number of <a href="http://jquery.com/">jQuery</a> plugins that do this. For some time, I couldn&#8217;t find any that fit the specific need I had. There was one plugin that I thought would work, but I couldn&#8217;t get it to work with a MySQL database, as it was designed to work with plain text lists in PHP. It may seem pathetic, but I couldn&#8217;t translate it.</p>
<p>So finally, I found the specific solution I wanted, and also manipulated the code examples enough to get it to work with MySQL. In light of this, and in light of my difficulty finding good examples for this, I want to give an example here, for myself and any others with the issue.</p>
<h2>Steps</h2>
<ol>
<li>Download <a href="http://jquery.com/">the latest jQuery</a>.</li>
<li>Download the latest <a href="http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/">Autocomplete Plugin</a> from <a href="http://bassistance.de">bassistance.de</a>.</li>
<li>Include the <a href="http://plugins.jquery.com/project/bgiframe">bgiframe plugin</a> for the sake of Internet Explorer users, in case this appears within a larger form. You can include this with a <a href="http://www.quirksmode.org/css/condcom.html">conditional comment</a> if you wish.</li>
<li>Create a MySQL database that contains the items from which you&#8217;ll be suggesting. There are many great tutorials out there for this kind of thing.</li>
<li>Include your JavaScript files in a page with the following HTML, additional JavaScript, PHP, and CSS.</li>
</ol>
<h2>HTML form markup</h2>
<p>So let&#8217;s say we have the following XHTML. I&#8217;m not concerned with specific areas of content at the moment, just a basic auto-suggest form that does not submit to anything in particular.</p>
<ol class="code xhtml">
<li><code>&lt;div id="wrapper"&gt;</code></li>
<li class="tab1"><code>&lt;form id="suggest-form"&gt;</code></li>
<li class="tab2"><code>&lt;div&gt;</code></li>
<li class="tab3"><code>&lt;label for="field"&gt;Search for something&lt;/label&gt;</code></li>
<li class="tab3"><code>&lt;input type="text" name="field" id="field" /&gt;</code></li>
<li class="tab2"><code>&lt;/div&gt;</code></li>
<li class="tab1"><code>&lt;/form&gt;</code></li>
<li><code>&lt;/div&gt;</code></li>
</ol>
<h2>jQuery Auto Suggest code</h2>
<p>The jQuery plugin for this technique works in such a way that it examines the contents of the text field as users type in it, and as it does this it sends requests to PHP to see what contents in the database (or other PHP content) match what is being typed. Each time a letter is pressed or deleted, this occurs again. jQuery then creates HTML that displays the results to the user.</p>
<p>After the plugin is loaded, the following JavaScript is loaded. For myself, I normally put this into a main.js file, though for the purposes of this example I have kept it inside the source, and you can view it there if you wish.</p>
<ol class="code javascript">
<li><code>function suggestValues() {</code></li>
<li class="tab1"><code>	$("#field").autocomplete("http://design.jonathanstegall.com/autosuggest/suggestions.php", {</code></li>
<li class="tab2"><code>width: 260,</code></li>
<li class="tab2"><code>selectFirst: false</code></li>
<li class="tab1"><code>});</code></li>
<li><code>}</code></li>
<li><code>$(document).ready(function(){</code></li>
<li class="tab1"><code>suggestValues();</code></li>
<li><code>});</code></li>
</ol>
<h2>PHP search code (suggestions.php)</h2>
<p>In this particular example, we&#8217;ll use PHP to connect to a MySQL database and get search results. I have a specific test database here, with keywords that are used in this blog. Obviously, you&#8217;ll need to modify the code for your own purposes, and there is no reason to use suggestions.php; you&#8217;ll just need to take note of the filename in order to include it in the JavaScript code above.</p>
<ol class="code php">
<li class="comment">First, you will need to have an active connection to your database. I can&#8217;t possibly predict all the ways that this can happen, so I will assume that you are already connected to it and can run queries on it.</li>
<li><code>function autosuggest() {</code></li>
<li class="tab1"><code>$q = strtolower($_GET["q"]);</code></li>
<li class="tab1"><code>if (!$q) return;</code></li>
<li class="tab1"><code>$query = "SELECT name FROM keywords";</code></li>
<li class="tab1"><code>$results = mysql_query($query);</code></li>
<li class="tab1"><code>while($result = mysql_fetch_array($results)) {</code></li>
<li class="tab2"><code>$name = $result['name'];</code></li>
<li class="tab2"><code>if (strpos(strtolower($name), $q) !== false) {</code></li>
<li class="tab3"><code>echo "$name\n";</code></li>
<li class="tab2"><code>};</code></li>
<li class="tab1"><code>};</code></li>
<li><code>}</code></li>
<li><code>autosuggest();</code></li>
</ol>
<h2>CSS example</h2>
<p>There is also some CSS that needs to be present to account for the default behavior of the plugin. The JavaScript, as I said, creates HTML on the fly, and this HTML can be styled very easily. Here is an example of what I have used. You can also see this in the source, if you view it, though again it would normally be inside a CSS file.</p>
<ol class="code css">
<li><code>.ac_results {</code></li>
<li class="tab1"><code>padding: 0;</code></li>
<li class="tab1"><code>border: 1px solid #333;</code></li>
<li class="tab1"><code>background-color: #fff;</code></li>
<li class="tab1"><code>overflow: hidden;</code></li>
<li class="tab1"><code>z-index: 99999;</code></li>
<li class="tab1"><code>text-align: left;</code></li>
<li><code>}</code></li>
<li><code>.ac_results ul {</code></li>
<li class="tab1"><code>width: 100%;</code></li>
<li class="tab1"><code>list-style-position: outside;</code></li>
<li class="tab1"><code>list-style: none;</code></li>
<li class="tab1"><code>padding: 0;</code></li>
<li class="tab1"><code>margin: 0;</code></li>
<li><code>}</code></li>
<li><code>.ac_results li {</code></li>
<li class="tab1"><code>margin: 0;</code></li>
<li class="tab1"><code>padding: 2px 5px;</code></li>
<li class="tab1"><code>cursor: default;</code></li>
<li class="tab1"><code>display: block;</code></li>
<li class="tab1"><code>font: menu;</code></li>
<li class="tab1"><code>font-size: 10px;</code></li>
<li class="tab1"><code>line-height: 16px;</code></li>
<li class="tab1"><code>overflow: hidden;</code></li>
<li><code>}</code></li>
<li><code>.ac_results {</code></li>
<li class="tab1"><code>background-color: #eee;</code></li>
<li><code>}</code></li>
<li><code>.ac_over {</code></li>
<li class="tab1"><code>background-color: #0e2221;</code></li>
<li class="tab1"><code>color: #fff;</code></li>
<li><code>}</code></li>
</ol>
<h2>Put it all together</h2>
<p>I have created a file at <a href="http://design.jonathanstegall.com/jquery/autosuggest/">http://design.jonathanstegall.com/jquery/autosuggest/</a> that demonstrates the jQuery auto suggest technique that I have discussed. This is a very foundational concept, and the plugin does come with some configuration options, but I feel that this is important since most of us will be using this plugin with a database of some kind.</p>
<p>Feel free to leave any additional thoughts or questions in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonathanstegall.com/2009/09/28/jquery-auto-suggest-with-mysql/feed/</wfw:commentRss>
		<slash:comments>36</slash:comments>
		</item>
		<item>
		<title>★ JavaScript functions on page load</title>
		<link>http://jonathanstegall.com/2008/04/17/javascript-functions-on-page-load/</link>
		<comments>http://jonathanstegall.com/2008/04/17/javascript-functions-on-page-load/#comments</comments>
		<pubDate>Thu, 17 Apr 2008 16:30:15 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ie6]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://jonathanstegall.com/?p=186</guid>
		<description><![CDATA[One of the biggest annoyances (for me) about JavaScript is that the famous window.onload = functionname; only works once on a given page. Over the years, many people have written various solutions to go around this, and cause the page to load multiple functions. Typically, my favorite is this one from Simon Wilson. It&#8217;s easy, [...]]]></description>
			<content:encoded><![CDATA[<p>One of the biggest annoyances (for me) about JavaScript is that the famous <code>window.onload = functionname;</code> only works once on a given page.</p>
<p>Over the years, many people have written various solutions to go around this, and cause the page to load multiple functions. Typically, my favorite is <a href="http://simonwillison.net/2004/May/26/addLoadEvent/">this one</a> from <a href="http://simonwillison.net/">Simon Wilson</a>. It&#8217;s easy, concise, and works in a myriad of different situations.</p>
<p>The other day, though, I ran into a situation where it didn&#8217;t work. I&#8217;m still not sure what the issue was, but Internet Explorer 6 (of course) refused to load a couple of the functions I was trying to use. Firefox, IE7, Safari, etc. all loaded them very nicely, but IE6 remained stubborn. I tried everything: rewriting the functions, removing everything else in the page, changing random things that were unrelated, and so on, all to no avail.</p>
<p>Finally, I decided to try <a href="http://jquery.com">jQuery</a>&#8216;s load solution:</p>
<ol class="code javascript">
<li><code>$(document).ready(function() {</code></li>
<li class="tab1"><code>functionname();</code></li>
<li><code>});</code></li>
</ol>
<p>This worked perfectly, in every browser. It didn&#8217;t occur to me to use this, mainly because the offending functions are not jQuery functions, and that particular page in the site didn&#8217;t have any jQuery on it at all. Now, it does.</p>
<p>File this away under: &#8220;I&#8217;m already using jQuery on a site, and I need to load some non-jQuery functions.&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://jonathanstegall.com/2008/04/17/javascript-functions-on-page-load/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>★ Reasons for IE8&#8242;s Default Behavior</title>
		<link>http://jonathanstegall.com/2008/03/04/reasons-for-ie8s-default-behavior/</link>
		<comments>http://jonathanstegall.com/2008/03/04/reasons-for-ie8s-default-behavior/#comments</comments>
		<pubDate>Wed, 05 Mar 2008 00:57:50 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[web standards]]></category>
		<category><![CDATA[ie8]]></category>
		<category><![CDATA[internet explorer]]></category>

		<guid isPermaLink="false">http://jonathanstegall.com/2008/03/04/reasons-for-ie8s-default-behavior/</guid>
		<description><![CDATA[As I&#8217;ve been thinking about the new default behavior that Microsoft announced for IE8, it occurs to me that there are a lot of reasons for Microsoft&#8217;s decision. In light of these thoughts, I want to look at the reasons that I think are likely possibilities. Standards as a whole Microsoft, especially in the last [...]]]></description>
			<content:encoded><![CDATA[<p>As I&#8217;ve been thinking about the new default behavior that <a href="http://www.microsoft.com/presspass/press/2008/mar08/03-03WebStandards.mspx">Microsoft announced for IE8</a>, it occurs to me that there are a lot of reasons for Microsoft&#8217;s decision. In light of these thoughts, I want to look at the reasons that I think are likely possibilities.</p>
<h2>Standards as a whole</h2>
<p>Microsoft, especially in the last year or two, has made a lot of moves toward standards, web and otherwise. They announced the <a href="http://www.microsoft.com/presspass/presskits/interoperability/default.mspx">Interoperability Principles</a>. They announced the ASP.NET MVC Framework, which finally allows programmers to control the HTML generated by ASP.NET. IE8 <a href="http://blogs.msdn.com/ie/archive/2007/12/19/internet-explorer-8-and-acid2-a-milestone.aspx">will pass Acid2</a>, and any number of other things.</p>
<p>While I&#8217;m not optimistic that any of this will cause Microsoft to act in any unexpected ways with respect to its attempts to acquire Yahoo, it is clear that a shift is occurring, to whatever extent.</p>
<h2>Noise from the web standards community</h2>
<p>Advocates of web standards have made a lot of noise about the original decision, and it is clear that Microsoft listened. Many people made logical arguments for why IE8, especially from a long-term perspective, should not default to IE7 behavior, while understanding that the option for IE7 behavior was a good thing.</p>
<h2>Legal issues</h2>
<p>It&#8217;s entirely possible that this is partly related to Microsoft&#8217;s various legal troubles, and that they are trying to appear as a better business citizen to help their case.</p>
<h2>Future versions of IE</h2>
<p>I think one of the most significant benefits, both from the perspective of future versions of IE, as well as future versions of Firefox, Opera, Safari, etc., is that the need for version targeting will probably become much less because IE8 is the default behavior. People who create sites that break in IE8 will, certainly, have the option of targeting IE7, but in order to do that they&#8217;ll have to learn that they can.</p>
<p>Most people who will run into the issue will probably not have the interest (or the time, possibly) to find out this kind of solution. The ones who do find out about the solution will find out that Microsoft doesn&#8217;t see it as the ideal solution, and will probably feel at least some pressure to learn proper ways of doing things. Thus, in a few years, it is entirely possible that versions of Internet Explorer that follow version 8 will be met with a majority of sites that do not break.</p>
<p>That&#8217;s not to say that the majority of sites will follow web standards. I&#8217;ve seen innumerable websites that didn&#8217;t break when IE7 came out, because they had such disregard for web standards that none of the changes affected them. I suspect we&#8217;ll have to deal with this for a long time. But that&#8217;s fine with me, as the future of web browsers can continue to look forward.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonathanstegall.com/2008/03/04/reasons-for-ie8s-default-behavior/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>★ Microsoft Expands Support for Web Standards</title>
		<link>http://jonathanstegall.com/2008/03/03/microsoft-expands-support-for-web-standards/</link>
		<comments>http://jonathanstegall.com/2008/03/03/microsoft-expands-support-for-web-standards/#comments</comments>
		<pubDate>Tue, 04 Mar 2008 00:16:48 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[web standards]]></category>
		<category><![CDATA[ie8]]></category>

		<guid isPermaLink="false">http://jonathanstegall.com/2008/03/03/microsoft-expands-support-for-web-standards/</guid>
		<description><![CDATA[See <a href="http://www.microsoft.com/presspass/press/2008/mar08/03-03WebStandards.mspx">this press release</a>:]]></description>
			<content:encoded><![CDATA[<p>See <a href="http://www.microsoft.com/presspass/press/2008/mar08/03-03WebStandards.mspx">this press release</a>:</p>
<blockquote><p>IE8 has been significantly enhanced, and was designed with great support for current Internet standards. This is evidenced by the fact that even in its first beta, IE8 correctly renders the popular test known as &#39;Acid2,&#39; which was created by the Web community to promote real-world interoperability,&#8221; said Ray Ozzie, Microsoft chief software architect. &#8220;Our initial plan had been to use IE7-compatible behavior as the default setting for IE8, to minimize potential impact on the world&#39;s existing Web sites. We have now decided to make our most current standards-based mode the default in IE8.</p>
</blockquote>
<p>The web standards world has been abuzz for the last few weeks, following the announcement that Internet Explorer 8 (and, presumably, any number of future releases) would have the default behavior of rendering websites in the same way that Internet Explorer 7 does. Causing the browser&#8217;s behavior to advance would have required that designers and developers direct it to do so with a meta tag.</p>
<p>Many standards-aware designers and developers were very understanding of the decision to allow websites to target IE7, avoiding issues with badly-programmed websites that break when browsers move forward. However, many of these designers and developers made a lot of noise wishing that Microsoft would allow the default behavior of websites to advance as the browser does, and require those who want to target IE7 to read a little bit and find out about the meta tag.</p>
<p>Apparently, Microsoft listened to the noise. I&#8217;m amazed and pleased by this change in strategy. I hope they will be able to spread the word enough, so that websites that will break with IE8&#8242;s improved standards support will learn about their options (1. learn how to write standards-compliant code, 2. include a one-line meta tag). I feel pretty confident that this is unrealistic, but I hope that it will happen to the extent that Microsoft doesn&#8217;t have to go back on this decision.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonathanstegall.com/2008/03/03/microsoft-expands-support-for-web-standards/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>★ Internet Explorer 8 is Acid2 Compliant</title>
		<link>http://jonathanstegall.com/2007/12/20/internet-explorer-8-is-acid2-compliant/</link>
		<comments>http://jonathanstegall.com/2007/12/20/internet-explorer-8-is-acid2-compliant/#comments</comments>
		<pubDate>Thu, 20 Dec 2007 18:18:08 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[web standards]]></category>
		<category><![CDATA[acid2 compliance]]></category>
		<category><![CDATA[microsoft internet explorer 8]]></category>

		<guid isPermaLink="false">http://jonathanstegall.com/2007/12/20/internet-explorer-8-is-acid2-compliant/</guid>
		<description><![CDATA[From the <a href="http://blogs.msdn.com/ie/archive/2007/12/19/internet-explorer-8-and-acid2-a-milestone.aspx">Internet Explorer team blog</a>:]]></description>
			<content:encoded><![CDATA[<p>From the <a href="http://blogs.msdn.com/ie/archive/2007/12/19/internet-explorer-8-and-acid2-a-milestone.aspx">Internet Explorer team blog</a>:</p>
<blockquote><p>&#8230;I&#39;m delighted to tell you that on Wednesday, December 12, Internet Explorer correctly rendered the Acid2 page in IE8 standards mode. While supporting the features tested in Acid2 is important for many reasons, it is just one of several milestones for the interoperability, standards compliance, and backwards compatibility that we&#39;re committed to for this release. We will blog more on these topics.</p>
</blockquote>
<p>Wonderful news. Web designers and developers everywhere agree with me that IE6 is the bane of our existence. There&#8217;s no need to discuss this further. IE7 is a massive improvement, and in general I&#8217;m pleased with the direction that the team seems to be taking for the future.</p>
<p><a href="http://en.wikipedia.org/wiki/Acid2">Acid2 compliance</a> is, for me, an unexpected leap forward. Currently, Opera and Safari are the most widely used browsers that have achieved this milestone. Firefox 2.x does not reach this level, although Firefox 3 (which is in beta and, I&#8217;m sure, will be released well before IE 8 is) does.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonathanstegall.com/2007/12/20/internet-explorer-8-is-acid2-compliant/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

