[Ilugc] [TIP] Ajax and jQuery

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Thu, 24 Nov 2011 09:40:09 +0530

jQuery and Ajax are both topics that need some experience and thinking.

Not something that can be covered in a tip or e-mail.

But still we will try.

jQuery we have already seen. And also Ajax in the previous tip.

But today we will see it another time in bit more detail.

Ajax is nothing but old wine in new bottle, CGI in a new incarnation with
 javascript. Our javascript library of choice is jQuery.

It is quite painful to do js programming directly.

Now that we are talking about javascript I have to mention something.

Javascript is not java.

It is a fantastic programming language; a language that has the magic of C
 with the convenience of perl and the elegance of python.

Okay enough evangelism.

It will take time for you to learn this since javascript works in
tandem with the DOM model we saw,
 and we need to learn jQuery alongside and HTML and CGI. So it is
attacking multiple problems
 at the same time. It is a little difficult but doable.

Let us look at one more example with which we can make a CGI call from
a jQuery code segment in
 a html file.

$ cat sample.html
<html>
<head>
   <script src="jquery.js" type="text/javascript"></script>
   <script type="text/javascript">
   $(function() {
          $('#ajax').load('/cgi-bin/testcgi');
   })
   </script>
</head>
<body>
       <div id='ajax' > </div>
</body>
</html>

Now let us look at the file /cgi-bin/testcgi

$ cat /var/www/cgi-bin/testcgi
#!/usr/bin/perl
use CGI;
$q = new CGI;
print $q->header;

print $q->start_html;
print "Printing from CGI";

$out = `ls /etc/`;

print "<pre>$out</pre>";

print $q->end_html;

Now you can see that you can load any portion of a web page with CGI
content using
 Ajax and jQuery.

But before that you should validate the CGI script independently.

And you can read up about how jQuery provides seamless Ajax
functionality using 3 functions.

$.get()

$.ajax()

and

$('selector).load();

That takes us deep into the jQuery world.

But suffice it to say that Ajax makes life really easy due to the
power of javascript and jQuery genius.

Now you might be able to guess how banking and other sites like
irctc.co.in run executable code and
 present in a web page seamlessly.

Javascript is also very useful for client side validations and several
things that need not be executed in the
 web server.

-Girish
-- 
G3 Tech
Networking appliance company
web: http://g3tech.in ?mail: girish at g3tech.in

Other related posts:

  • » [Ilugc] [TIP] Ajax and jQuery - Girish Venkatachalam