<?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>Webitations &#187; jQuery</title>
	<atom:link href="http://ivorycity.com/blog/category/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://ivorycity.com/blog</link>
	<description>Web development tips, tricks, and topics</description>
	<lastBuildDate>Thu, 07 Jul 2011 17:43:45 +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>jQuery Lazy Loader &#8211; Lazy Load Images and HTML On Scroll</title>
		<link>http://ivorycity.com/blog/2011/04/19/jquery-lazy-loader-load-html-and-images-on-scroll/</link>
		<comments>http://ivorycity.com/blog/2011/04/19/jquery-lazy-loader-load-html-and-images-on-scroll/#comments</comments>
		<pubDate>Tue, 19 Apr 2011 18:35:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://ivorycity.com/blog/?p=139</guid>
		<description><![CDATA[What is Lazy Loading?
Guess what? Server-side render time is probably not the bottleneck of your page performance&#8230; In most cases, the front-end is the most significant contributor to page load time. Things like img tags and css images are one of the most frequent contributors to load because these assets are loaded by the browser, [...]]]></description>
			<content:encoded><![CDATA[<h3>What is Lazy Loading?</h3>
<p>Guess what? Server-side render time is probably not the bottleneck of your page performance&#8230; In most cases, the front-end is the most significant contributor to page load time. Things like img tags and css images are one of the most frequent contributors to load because these assets are loaded by the browser, even if they&#8217;re not visible to the user.</p>
<p><em>But you have all this content and images on your page, you say? What are you to do?</em></p>
<p>So one technique is to use lazy loading of images and even entire portions of the DOM as users scroll down the page. By lazy loading, you free up resources such as bandwidth, available http connections, and client-cpu to render the content within the browser viewport quickly. Then, worry about additional content as the user scrolls down the page. </p>
<p>Maybe there&#8217;s a little delay to load the images, but better to render them later than keep your users waiting on initial load, right? Who knows? Maybe many of your users don&#8217;t even scroll down on every page load? In those cases, they will never see the content anyway&#8230; why waste resources?</p>
<p>Perhaps you&#8217;ll also cut your server bandwidth and free up more apache threads or reduce requests to your static content server by using lazy loading and save yourself some Dinero$.</p>
<h3>How To Implement Lazy Loading with jQuery</h3>
<p>So I wrote this little jQuery plugin that I hope you&#8217;ll find useful to help in your endeavor to get lazy. The premise is to use a placeholder tag with an Html Comment inside to hide content from the rendering engine. If the browser can&#8217;t see your img tags or html elements with css images, it obviously wont render them.</p>
<p>The trick then is to tell the browser which tags to render while the page is loading.</p>
<h3>Here&#8217;s a quick example:</h3>
<blockquote>
<p>&lt;pre class=&#8221;i-am-lazy&#8221; &gt;&lt;!&#8211;<br />
&lt;img src=&#8221;some.png&#8221; /&gt;<br />
&#8211;&gt;&lt;/pre&gt;</p>
<p>&lt;pre class=&#8221;i-am-lazy&#8221; &gt;&lt;!&#8211;<br />
&lt;div&gt;Any, html css img background, whatever. &lt;img src=&#8221;some.png&#8221; /&gt;  &lt;/div&gt;<br />
&#8211;&gt;&lt;/pre&gt;</p>
<p>&lt;script type=&#8221;text/javascript&#8221; src=&#8221;jquery.lazyloader.js&#8221; &gt;&lt;/script&gt;<br />
&lt;script type=&#8221;text/javascript&#8221; &gt;<br />
$(document).ready( function()<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;$(&#8217;pre.i-am-lazy&#8217;).lazyLoad();<br />
});<br />
&lt;/script&gt;
</p></blockquote>
<p><em>The Lazy Loader </em><em><strong>replaces</strong></em><em> the placeholder &#8216;pre&#8217; tags with the text inside the HTML Comment, so the pre tags disappear as the hidden content is rendered into the DOM.</em> This way, you don&#8217;t get a bunch of crufty markup cluttering up the DOM once the content is rendered.</p>
<p>If the content appears withing the browser viewport on initial load, the content in the pre tags is automatically rendered right away. Otherwise the html content within the pre tags is rendered as the user scrolls down the page.</p>
<p>I like to use pre tags for placeholders when I&#8217;m lazy loading because they render with 0 dimension when there&#8217;s only an html comment inside. <em>You can use any tag you like though with your html comment inside.</em> You can use div, li, ul, etc&#8230; not just pre tags. Be sure though not to include any white space between the tags:</p>
<blockquote><p>&lt;div class=&#8221;i-am-lazy&#8221;&gt;&lt;!&#8211; stuff &#8211;&gt;&lt;/div&gt;</p></blockquote>
<p>not:</p>
<blockquote><p>&lt;div class=&#8221;i-am-lazy&#8221;&gt;                   &lt;!&#8211; stuff &#8211;&gt;               &lt;/div&gt;</p></blockquote>
<h3>A real Lazy Loader example &#8211; Facebook Friend List:</h3>
<p><iframe src="http://ivorycity.com/ls.php" width="400" height="200" ></iframe></p>
<h3>Where to get it? Downloads and Links:</h3>
<h4><a href="http://plugins.jquery.com/project/LazyLoadOnScroll">jQuery Plugin Project Page</a></h4>
<h4><a href="http://ivorycity.com/jquery.lazyloader.zip">jquery.lazyloader.zip</a></h4>
]]></content:encoded>
			<wfw:commentRss>http://ivorycity.com/blog/2011/04/19/jquery-lazy-loader-load-html-and-images-on-scroll/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Twitter App with jQuery Templates and JSONP</title>
		<link>http://ivorycity.com/blog/2009/08/24/simple-twitter-app-with-jquery-templates-and-jsonp/</link>
		<comments>http://ivorycity.com/blog/2009/08/24/simple-twitter-app-with-jquery-templates-and-jsonp/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 16:41:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://ivorycity.com/blog/?p=63</guid>
		<description><![CDATA[I&#8217;ve been trying to come up with some cool applications for the jQuery Templates Plugin. So Twitter hosts this cool jsonp interface that enables you to pull down all kinds of twitter data to your client application &#8211; jsonp enables us to get around XSS, so the application doesn&#8217;t need a server-side component at all!!!
Check [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been trying to come up with some cool applications for the jQuery Templates Plugin. So Twitter hosts this cool jsonp interface that enables you to pull down all kinds of twitter data to your client application &#8211; jsonp enables us to get around XSS, so the application doesn&#8217;t need a server-side component at all!!!</p>
<p>Check out the code:</p>
<pre>&lt;form id="twitter_search" &gt;
   &lt;label for="twitter_search" &gt;Search Twitter Feed:&lt;/label&gt;
   &lt;input type="text" name="q" value="GUICenter" /&gt;
   &lt;input type="submit" name="submit" value="Submit" /&gt;
 &lt;/form&gt;
 &lt;div id="twitter_results_tmpl" style="display:none" &gt;
   &lt;h4&gt;Tweets from {name}&lt;/h4&gt;
     &lt;ul&gt;
       &lt;li style="display:none" &gt;{text} &lt;br/ &gt;Created At: {created_at}&lt;/li&gt;
    &lt;/ul&gt;
 &lt;/div&gt;

Twitter = {

			init: function()
			{
				$('#twitter_search').submit(Twitter.search);
			},

			search: function()
			{
				$.ajax({
					'url': 'http://twitter.com/statuses/user_timeline/'+$(this.q).val()+'.json',
					'data': { count: 5 },
					'success': Twitter.onSearchResult,
					'dataType': 'jsonp'
				});

				return false;

			},

			onSearchResult: function(res)
			{
				$('#twitter_results_tmpl')
					.render(res[0]['user'],{clone:true})
					.removeAttr('id')
					.find('.twitter_tmpl')
					.render( res, {clone:true} )
				  .show().end().end().show();
			}

		};

		$(document).ready( Twitter.init );</pre>
<p>Try it out&#8230; </p>
<p><script src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js" type="text/javascript"></script> <script src="/tests/jquery/template/templates.js" type="text/javascript"></script><br />
<script src="/tests/jquery/template/examples/twitter.js" type="text/javascript"></script></p>
<form id="twitter_search"> <label for="twitter_search">Search Twitter Feed:</label></p>
<input name="q" type="text" value="GUICenter" />
<input name="submit" type="submit" value="Submit" /> </form>
<div id="twitter_results_tmpl" class="twitter_result" style="display:none">
<h4>Tweets from {name}</h4>
<ul>
<li class="twitter_tmpl" style="display:none">{text}<br />
Created At: {created_at}</li>
</ul>
</div>
<p>Cool, eh? <img src='http://ivorycity.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://ivorycity.com/blog/2009/08/24/simple-twitter-app-with-jquery-templates-and-jsonp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Templates-1.0.0 released!</title>
		<link>http://ivorycity.com/blog/2009/08/20/jquery-templates-1-0-0-released/</link>
		<comments>http://ivorycity.com/blog/2009/08/20/jquery-templates-1-0-0-released/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 17:49:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://ivorycity.com/blog/?p=52</guid>
		<description><![CDATA[Just released a new jQuery plugin!
Here is the excerpt:
The jQuery templates plugin enables you to use jQuery and HTML as a template engine. Why would you want to use JavaScript as a template engine you might ask? One of the primary reasons is to perform easy token replacements inside the DOM when you receive data [...]]]></description>
			<content:encoded><![CDATA[<p>Just released a new jQuery plugin!</p>
<p><strong>Here is the excerpt:</strong></p>
<p>The jQuery templates plugin enables you to use jQuery and HTML as a template engine. Why would you want to use JavaScript as a template engine you might ask? One of the primary reasons is to perform easy token replacements inside the DOM when you receive data back from an AJAX call. Typically (especially when using frameworks like jQuery) you won’t want to put your html inside a JS file, but rather use the code to manipulate the html already inside the DOM. The jQuery templates function allows you to apply template token replacements to any section of markup in the DOM, and even clone templates when you have an array of new values. </p>
<p><strong>On plugins.jquery.com: <a href="http://plugins.jquery.com/project/Templates" onclick=" window.open( $(this).attr('href'),'_blank'); return false;" >http://plugins.jquery.com/project/Templates</a></strong></p>
<p><strong>Full Documentation <a href="http://ivorycity.com/blog/?page_id=31">Here</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://ivorycity.com/blog/2009/08/20/jquery-templates-1-0-0-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery outerHtml()</title>
		<link>http://ivorycity.com/blog/2009/08/18/3/</link>
		<comments>http://ivorycity.com/blog/2009/08/18/3/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 16:44:45 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://ivorycity.com/blog/?p=3</guid>
		<description><![CDATA[Today I&#8217;ve been working on some template stuff using jquery and I realized I need a way to get the entire html string for a particular selector, not just the innerHtml contents. Hmm, that sounds like something called outerHtml&#8230;
Okay, better google for outerHtml: yes&#8230; no&#8230; grrrrr&#8230; there is an outerHtml property, but only for IE. [...]]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;ve been working on some template stuff using jquery and I realized I need a way to get the entire html string for a particular selector, not just the innerHtml contents. Hmm, that sounds like something called outerHtml&#8230;</p>
<p>Okay, better google for outerHtml: yes&#8230; no&#8230; grrrrr&#8230; there is an outerHtml property, but only for <a href="http://msdn.microsoft.com/en-us/library/ms534310(VS.85).aspx">IE</a>. So lets see if we can roll our own cross-browser solution using jquery:</p>
<pre>jQuery.fn.extend( {

  outerHtml: function( replacement )
  {
    // We just want to replace the entire node and contents with
    // some new html value
    if (replacement)
    {
      return this.each(function (){ $(this).replaceWith(replacement); });
    }

    /*
     * Now, clone the node, we want a duplicate so we don't remove
     * the contents from the DOM. Then append the cloned node to
     * an anonymous div.
     * Once you have the anonymous div, you can get the innerHtml,
     * which includes the original tag.
     */
    var tmp_node = $("&lt;div&gt;&lt;/div&gt;").append( $(this).clone() );
    var markup = tmp_node.html();

    // Don't forget to clean up or we will leak memory.
    tmp_node.remove();
    return markup;
  }
});</pre>
<p>So that basically gets us an outerHtml() function! The method also lets you do outerHtml replacements, which is basically just a wrapper around replaceWith(). Pretty fun!</p>
<p>So now, how do we use it?</p>
<pre>&lt;div class="outer" &gt;Fun Stuff&lt;/div&gt;

// get the entire html for the &lt;div class="outer" &gt; tag
var x = $('.outer').outerHtml();

// replace all div tags with &lt;div class="outer" &gt;Fun Stuff&lt;/div&gt;
$('div').outerHtml(x);</pre>
]]></content:encoded>
			<wfw:commentRss>http://ivorycity.com/blog/2009/08/18/3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Efficient jQuery Selectors</title>
		<link>http://ivorycity.com/blog/2009/03/23/hello-world/</link>
		<comments>http://ivorycity.com/blog/2009/03/23/hello-world/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 00:12:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://ivorycity.com/blog/?p=1</guid>
		<description><![CDATA[jQuery is seriously awesome, but if we don&#8217;t use it carefully we can put a pretty serious load on the users&#8217; CPU.  This can mean a perceptible delay when the $(document).ready() function runs if there are loads of un-optimized selectors running. Considering that most users spend 4 seconds on a site, 1 or 2 seconds [...]]]></description>
			<content:encoded><![CDATA[<p>jQuery is seriously awesome, but if we don&#8217;t use it carefully we can put a pretty serious load on the users&#8217; CPU.  This can mean a perceptible delay when the $(document).ready() function runs if there are loads of un-optimized selectors running. Considering that most users spend 4 seconds on a site, 1 or 2 seconds of delay can mean losing your user.  What kinds of jQuery selectors are fastest? Which ones are slower, and which ones must we avoid at all costs to ensure a responsive experience?</p>
<h4>Which is fastest?</h4>
<p>$(&#8217;.box_text&#8217;).html(&#8217;test1&#8242;);<br />
$(&#8217;span.box_text&#8217;).html(&#8217;test2&#8242;);<br />
$(&#8217;#box_text&#8217;).html(&#8217;test3&#8242;);</p>
<p>Which is fastest? Have a look for yourself on this test page, <a title="test0" href="http://www.ivorycity.com/tests/jquery/selectors/test0.php" target="_blank">http://www.ivorycity.com/tests/jquery/selectors/test0.php</a>.</p>
<p>If you&#8217;re using FF3, they should all be about the same, but if you&#8217;re using IE6,7, or FF2, $(&#8217;#box_text&#8217;) is up to 10x faster than $(&#8217;.box_text&#8217;)!</p>
<h4>Why is this?</h4>
<p>In order to answer these questions, we need to know a little about how jQuery finds DOM nodes.</p>
<p>Internally, jQuery uses document.getElementById() to find elements based on id. Browsers index id&#8217;s internally, so the lookup is instantaneous.  Meanwhile,  using $(&#8217;.someclass&#8217;) in IE causes jQuery to iterate over every element in the DOM to find one with a particular class &#8211; that&#8217;s crazy slow!</p>
<p>By prefixing &#8217;span&#8217; to the query, we get $(&#8217;span.box_text&#8217;) &#8211; this is a little better because internally, jQuery calls document.getElementsByTagName and iterates the results. Since spans are only a sub-set of all DOM nodes, the query is a lot faster. So if you&#8217;re performing lots of &#8216;naked&#8217; class selectors, you&#8217;re going to consume a lot of user cpu.</p>
<p>In Firefox 3 however,  it&#8217;s no longer the case that $(&#8217;.someclass&#8217;) causes an iteration over the DOM; document.getElementsByClassName() was added in FF3 to index element class names inside the browser memory space, which is much, much faster. That&#8217;s why we don&#8217;t see the same performance hit as in IE.</p>
<p>Now, it may not always be appropriate to access elements by id. If you&#8217;re trying to find many instances, say 100+, with a particular class name, it&#8217;s actually faster to use $(&#8217;.someclass&#8217;) than to loop through a set of id&#8217;s.</p>
<h4>The moral of the story&#8230;</h4>
<p>1) Use id selectors to reference a &#8216;needle in a haystack&#8217;</p>
<p>2) Use class selectors prudently, and only when referencing large numbers of DOM nodes</p>
<p>3) You can also prevent iteration over the entire dom by doing something like $(&#8217;#myid .myclass&#8217;) which only searches sub-nodes of #myid.</p>
]]></content:encoded>
			<wfw:commentRss>http://ivorycity.com/blog/2009/03/23/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

