<?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>Query7 &#187; jQuery</title>
	<atom:link href="http://query7.com/tag/jquery/feed" rel="self" type="application/rss+xml" />
	<link>http://query7.com</link>
	<description>PHP, Javascript, Python and Web Development</description>
	<lastBuildDate>Sat, 25 Jun 2011 21:29:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Creating an inline contact form</title>
		<link>http://query7.com/creating-an-inline-contact-form</link>
		<comments>http://query7.com/creating-an-inline-contact-form#comments</comments>
		<pubDate>Mon, 07 Sep 2009 10:41:50 +0000</pubDate>
		<dc:creator>Kishore Nallan</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.webdevelopmentbits.com/?p=507</guid>
		<description><![CDATA[<p>Customer feedback is arguably one of the most critical factors determining the success of a product over a period of time. Hence, it&#8217;s not surprising that many websites have some form of contact form or another to encourage users to write back to them. Most of these &#8220;contact&#8221; pages tend to be on a separate page which is usually part of the navigation or is linked to with phrases like &#8220;we encourage your feedback&#8221; or &#8220;do get in touch with us&#8221;, etc.</p>
<p>However, I have often noticed that many people (unless they genuinely needed help), either back away from submitting their  comments (when they are confronted by a large  textarea on the contact page), or simply move away from the site as the contact page loads.</p>
<p>Today, let&#8217;s spice things up a bit by using jQuery to load a lightweight inline contact form. So, instead of loading a separate contact form page when the &#8220;contact us&#8221; link is clicked, we will just have a small textarea opens up dynamically right next to  the link &#8211; so that the user can type his feedback and it can be  submitted rightaway using AJAX. Our main aim here is to encourage more users&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>Customer feedback is arguably one of the most critical factors determining the success of a product over a period of time. Hence, it&#8217;s not surprising that many websites have some form of contact form or another to encourage users to write back to them. Most of these &#8220;contact&#8221; pages tend to be on a separate page which is usually part of the navigation or is linked to with phrases like &#8220;we encourage your feedback&#8221; or &#8220;do get in touch with us&#8221;, etc.</p>
<p>However, I have often noticed that many people (unless they genuinely needed help), either back away from submitting their  comments (when they are confronted by a large  textarea on the contact page), or simply move away from the site as the contact page loads.</p>
<p>Today, let&#8217;s spice things up a bit by using jQuery to load a lightweight inline contact form. So, instead of loading a separate contact form page when the &#8220;contact us&#8221; link is clicked, we will just have a small textarea opens up dynamically right next to  the link &#8211; so that the user can type his feedback and it can be  submitted rightaway using AJAX. Our main aim here is to encourage more users to provide feedback and although I don&#8217;t have actual data to prove whether an inline form will be any more helpful, I have used it in a  couple of places, and it did appear to bring in more casual feedback  about the site, features etc. Ok, let&#8217;s get started. Here is the <strong><a href="http://www.webdevelopmentbits.com/temp/inline_form_demo.html">demo</a></strong> of how it will all look in the end.</p>
<p>Okay, first let&#8217;s have a hypothetical page in which we want to make use of the inline contact form. The call of action, is obviously the &#8220;We would love to hear your feedback&#8221; link, which has the id &#8220;a_inline_form&#8221;. Initially, this hyperlink will simply point to the default contact form page. We will have the inline form in a DIV with id &#8220;inline_form&#8221;, which will be hidden initially.</p>
<pre>#inline_form {
		/* keep the inline form hidden initially */
		display: none;

		position: relative;
		padding: 10px;
		width: 260px;
	}</pre>
<p>We will now use jQuery to &#8220;hijack&#8221; this link and fire our inline contact form into action:</p>
<pre> // toggle visibility of inline form when link is clicked
$("#a_inline_form").click( function() {

	$("#inline_form").toggle(200);

	$("#inline_form").css( "left", $("#a_inline_form").position().left +
                                                $("#a_inline_form").width() );

	$("#inline_form").css("top", "-40px");

	// get cursor to texarea, ready to receive keystrokes!
	$("#inlinecmt").focus();

	// return false to prevent default link behaviour
	return false;
});</pre>
<p>The above code is fairly self explanatory &#8211; I just want to describe how we are positioning the inline form. jQuery allows us to very easily retrieve the left offset position of an element (even when it&#8217;s not absolutely or relatively positioned!) using &#8220;$(elem).position().left&#8221; property.</p>
<p>In this example, I wanted to place the inline form right next to the comment link. To do that, we can simply set the &#8220;left&#8221; CSS property of the inline form to a value that&#8217;s the sum of the left offset of the feedback link and the width of the comment link &#8211; this will of course place the inline form right next to the feedback link. In addition, we also set the cursor focus to the inline form so that the textarea is ready to receive input right away.</p>
<p>We have a simple &#8220;ok&#8221; button underneath the textarea which will be used to submit the form. We will once again &#8220;hijack&#8221; the form submit event using jQuery and perform the form submission using AJAX. I have omitted the actual form submission and processing here.</p>
<pre>$("#inline_form").submit( function() {

	// make your ajax POST call here...

	$("#a_inline_form").after('&lt;span class="msg"&gt;Thanks for the comment!' +
                               '&lt;/span&gt;');
	$("#inline_form").hide(200);

	setTimeout( '$(".msg").hide()', 2000);

	// return false to prevent default form submission behavior
	return false;
});</pre>
<p>Now, once the AJAX call is successful, we need to notify that to the user and thank him for the feedback. I have chosen to do that by inserting a message right after the feedback link and hiding it (using setTimeout() ) after 2 seconds. Don&#8217;t forget to hide the inline form too. With that, our inline form is up and ready to go!</p>
<p>This is by no means a replacement for the traditional contact form, but it will be handy in places where one requires casual feedback from users. And as you would have already noticed, we have used JavaScript here unobtrusively &#8211; so in case the user has JavaScript disabled, the feedback link merely loads the <a href="http://www.webdevelopmentbits.com/temp/inline_form_contact.html">default contact page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://query7.com/creating-an-inline-contact-form/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Avoiding long polling</title>
		<link>http://query7.com/avoiding-long-polling</link>
		<comments>http://query7.com/avoiding-long-polling#comments</comments>
		<pubDate>Sun, 26 Jul 2009 15:04:02 +0000</pubDate>
		<dc:creator>Kishore Nallan</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[COMET]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[long polling]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[polling]]></category>
		<category><![CDATA[real time]]></category>

		<guid isPermaLink="false">http://www.webdevelopmentbits.com/?p=500</guid>
		<description><![CDATA[Right from Twitter to Google Wave, real time information streaming via the browser seems to be the most "happening" thing on the web arena currently. However, feeding real time information as and when it is available to an user using your web application is not as straight forward as it is on a desktop environment.]]></description>
			<content:encoded><![CDATA[<p>Right from Twitter to Google Wave, real time information streaming via the browser seems to be the most &quot;happening&quot; thing on the web arena currently. However, feeding real time information as and when it is available to an user using your web application is not as straight forward as it is on a desktop environment. HTTP is a stateless protocol which does not currently support a two way communication link between the server and the user&#8217;s computer via the browser. </p>
<p>The simplest way to simulate  real time streaming of information is to poll the server every N seconds, and to display the information (if any). The number N will determine how real time the resulting stream of updates will look to a viewer. This model is simply called, polling. Now, polling is bad because it repeatedly connects and disconnects to the server, which when scaled across thousands of users, cause severe strain on your server. This problem becomes even more apparent in cases when the poller returns with empty data most of the times. </p>
<p>The jQuery snippet below polls the server every 3 seconds for new information and displays it in a simple alert box.</p>
<pre>
function callComplete(response) {
	alert("Response received is: "+response);
};

function connect() {
	// when the call completes, callComplete() will be called along with
	// the response returned
	$.post('/path/to/script.php', {}, callComplete, 'json');
};

$(document).ready( function() {
	setInterval(connect,3000);
}
</pre>
</p>
<p><strong>Long polling</strong></p>
<p>This technique is an optimisation of the traditional polling method. In long polling, once the client makes a connection with the server (via JavaScript), the server waits for the data requested by the client to become available. If it&#8217;s not immediately available, the server (being a PHP script for example) loops and sleeps. When the data is available, the server returns the data to the client and the connection closes. When this happens, the client again reconnects with the server (in the callback function of your original AJAX request). </p>
<p>This method is definitely an improvement over mindless polling, as we will avoid repeatedly opening and closing connections when data is not available for long periods of time. However, it is important to be aware of the server side technical limitations of long polling. Since connections might be kept alive for long durations of time, your web server must be capable of handling such large numbers of simultaneous connections. We talked about how certain light weight webservers outperformed Apache in this particular area in the last article. </p>
<p>Let&#8217;s look at a very simple jQuery/PHP long polling example:</p>
<pre>function callComplete(response) {
	alert("Response received is: "+response);
	// reconnect to the server
	connect();
};

function connect() {
	// when the call completes, callComplete() will be called along with
	// the response returned
	$.post('/path/to/script.php', {}, callComplete, 'json');
};

$(document).ready( function() {
	connect();
}
</pre>
</p>
<p>In the PHP end, we do:</p>
<pre>
while(1) {

	$data = &quot;some data fetched from the DB&quot;;

	// if we have new data, return it to the client
	if(!empty($data)) {
		echo json_encode($data);
		break;
	}

	sleep(3000);	// we sleep for 3s and check again for data
}
</pre>
<p><strong>COMET streaming </strong></p>
<p>COMET is a generic term that refers to the use a persistent HTTP connection for the web server to &quot;push&quot; data to the client as and when it arrives. If you have ever wondered how Gmail is able to notify you of a new email message seconds after it is sent, this is what they use to push the new email notifications to the front end. </p>
<p>However, as often is the case with JavaScript, the cross browser compatibility issues mean that a host of methods (targeting different browsers) are used to implement such an interaction. The upside is that these methods rely purely on JavaScript, and hence not dependent on any specific client side plugin. A detailed exploration of these different methods and their caveats is beyond the scope of this article, but let&#8217;s get a quick overview of these methods.</p>
<p><strong>Hidden iframe technique:</strong> A hidden iframe html element is embedded onto the page, which points to the URL on the server which handles the persistent connection. The server will then push the data as it arrives to the iframe, using &lt;script&gt; tags. This content is then channeled to the parent window using JavaScript. This method is based on the fact that many browsers evaluate JavaScript chunks as they are received, even if the complete page has not finished loading yet. However this method is prone to cause the &quot;loading&quot; icon or status bar message to appear all the time (since we are using a persistent HTTP connection, the browsers tend to believe that the page is yet to finish loading fully). </p>
<p><strong>XMLHttpRequest using Interactive readyState:</strong> This method is targetted towards Firefox, Safari and Chrome, which support the interactive readyState property of the XHR object, and fires events every time a chunk of data is received from the server. </p>
<p><strong>Server-Sent events: </strong>This is a feature (from the <a href="http://whatwg.org">WHATWG</a> Web Applications 1.0 specification) supported by Opera since v9, using which we can push events from the server directly to the visitor&#8217;s browser. Refer to <a href="http://my.opera.com/WebApplications/blog/show.dml/438711">this article</a> on how it&#8217;s used. </p>
<p>As you can see, it&#8217;s quite a mouthful, and hence implementing COMET streaming which works uniformly across all major browsers is not a task for the faint hearted (not forgetting that the webserver must also be able to handle a large number of persistent connection). The good news is that quite a lot of the hard work has already been done for you. There are various opensource COMET servers and client libraries which you can use to deploy a COMET application pretty safely. Here are a few of them for your exploration: </p>
<p><strong><a href="http://orbited.org">Orbited</a>:</strong> &quot;Orbited is an HTTP daemon that is optimized for long-lasting comet  connections. Orbited allows you to write real-time web  applications, such as a chat room or instant messaging client, without  using any external plugins like Flash or Java. &quot;</p>
<p><a href="http://cometdproject.dojotoolkit.org/"><strong>Cometd:</strong></a> &quot;Cometd is a scalable HTTP-based event routing bus that uses a Ajax Push technology pattern known as Comet.&quot; </p>
<p><strong><a href="http://meteorserver.org/">Meteor</a>:</strong> &quot;Meteor is an open source HTTP server, designed to offer developers a simple means of integrating streaming data into web applications without the need for page refreshes.&quot;</p>
<p><a href="http://pi.kodfabrik.com/documentation/plugin/pi.comet/"><strong>Comet Pi:</strong></a> This is a standalone JavaScript library which you can use to handle the various cross browser issues that COMET causes. You can use pi.comet with any serverside language. </p>
]]></content:encoded>
			<wfw:commentRss>http://query7.com/avoiding-long-polling/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>No IE6 support on new W3C website?</title>
		<link>http://query7.com/no-ie6-support-on-new-w3c-website</link>
		<comments>http://query7.com/no-ie6-support-on-new-w3c-website#comments</comments>
		<pubDate>Fri, 27 Mar 2009 14:00:59 +0000</pubDate>
		<dc:creator>Dilip Shukla</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[IE6]]></category>
		<category><![CDATA[W3]]></category>
		<category><![CDATA[Web Standards]]></category>

		<guid isPermaLink="false">http://www.webdevelopmentbits.com/?p=445</guid>
		<description><![CDATA[<p><a title="World Wide Web Consortium" href="http://www.w3.org" target="_blank">W3.org</a> is overhauling its website in order to make it more user-friendly and quiet people who used to wonder (including me) why the administrator body of web have such dull, flat, unorganized and old fashioned website?</p>
<p>The new website looks more attractive and organized. 10 minutes tour of new website <a title="10 Minutes Tour of http://beta.w3.org/" href="http://dotsub.com/view/41e149bd-8b98-4103-a9f8-c96787497211" target="_blank">can be found here</a>. In contrast with the previous version, this new website uses rich content presentation, including JavaScript.</p>
<p>The website uses <a title="Download jQuery 1.3.2 from Google Code" href="http://code.google.com/p/jqueryjs/downloads/detail?name=jquery-1.3.2.min.js" target="_blank">jQuery 1.3.2</a>, a very known and popular JavaScript library, instead of pure JavaScript. This is very good news and a big leap towards streamlining the use of JavaScript frameworks, specially in case of jQuery. In addition of core jQuery framework, <a title="Beta World Wide Web Consortium" href="http://beta.w3.org" target="_blank">beta.w3.org</a> also uses jQuery plugins (e.g.: <a title="jQuery Cycle Plugin" href="http://malsup.com/jquery/cycle/" target="_blank">http://malsup.com/jquery/cycle/</a>), to enhance the user experience.</p>
<p>The biggest news of the day is w3.org beta website doesn&#8217;t render correctly in IE6. It&#8217;s supposed to be a strong argument for web developers in order to inspire and take initiative towards to stop exclusive coding to render their websites correctly in IE6.</p>
<p style="text-align: center;"><img class="size-full wp-image-451 aligncenter" src="http://query7.com/wp-content/uploads/ie6.jpg" alt="" width="500" height="428" /></p>
]]></description>
			<content:encoded><![CDATA[<p><a title="World Wide Web Consortium" href="http://www.w3.org" target="_blank">W3.org</a> is overhauling its website in order to make it more user-friendly and quiet people who used to wonder (including me) why the administrator body of web have such dull, flat, unorganized and old fashioned website?</p>
<p>The new website looks more attractive and organized. 10 minutes tour of new website <a title="10 Minutes Tour of http://beta.w3.org/" href="http://dotsub.com/view/41e149bd-8b98-4103-a9f8-c96787497211" target="_blank">can be found here</a>. In contrast with the previous version, this new website uses rich content presentation, including JavaScript.</p>
<p>The website uses <a title="Download jQuery 1.3.2 from Google Code" href="http://code.google.com/p/jqueryjs/downloads/detail?name=jquery-1.3.2.min.js" target="_blank">jQuery 1.3.2</a>, a very known and popular JavaScript library, instead of pure JavaScript. This is very good news and a big leap towards streamlining the use of JavaScript frameworks, specially in case of jQuery. In addition of core jQuery framework, <a title="Beta World Wide Web Consortium" href="http://beta.w3.org" target="_blank">beta.w3.org</a> also uses jQuery plugins (e.g.: <a title="jQuery Cycle Plugin" href="http://malsup.com/jquery/cycle/" target="_blank">http://malsup.com/jquery/cycle/</a>), to enhance the user experience.</p>
<p>The biggest news of the day is w3.org beta website doesn&#8217;t render correctly in IE6. It&#8217;s supposed to be a strong argument for web developers in order to inspire and take initiative towards to stop exclusive coding to render their websites correctly in IE6.</p>
<p style="text-align: center;"><img class="size-full wp-image-451 aligncenter" src="http://query7.com/wp-content/uploads/ie6.jpg" alt="" width="500" height="428" /></p>
]]></content:encoded>
			<wfw:commentRss>http://query7.com/no-ie6-support-on-new-w3c-website/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

