rss feed blog search engine
 
Search rss blog search engine
 
Ajax Tutorial  
Released:  8/24/2008 4:53:10 AM
RSS Link:  http://tutorialajax.com/feed
Last View 11/22/2008 1:29:34 AM
Last Refresh 11/22/2008 1:23:14 AM
Page Views 535
Comments:  Read user comments (0)
Save It Add to Technorati Add to Del.icio.us Add to Furl Add to Yahoo My Web 2.0 Add to My MSN Add to Google Add to My Yahoo! Ajax Tutorial



Description:



Prototype, jQuery and another Ajax tutorials


Contents:

Compress JavaScript with Gzip

The main problem when we are using ajax framework is because of their big size. Of course, it will extremely slow down the sites load time. For example, if we use the standard jQuery, it’s almost 100Kb. It’s even more if we use the complete Prototype and Scriptaculous that will cost around 150Kb.

This is really nightmare for people who still use GPRS or the old 56kbps Dial-Up internet connection. It will takes minutes to load the sites or maybe it will takes forever because the browser usually won’t download it. Unfortunately, it’s very easy to solve this problem. Do you know that we can compress those files and reduce the size by half or more?

There are several method to compress javascript and I think one of the most efficient method is using Gzip Compression.

Gzip

To implement the gzip compression for websites, first we need to gzip the Javascript files. For that purpose, we will need a gzip-compatible archiver / compressor. 7-zip is my favorite because it’s small, very fast and it’s completely free.

Gzip the JavaScript file, change the “gz” extension to “jgz” for safari compatibility and upload it to the same location together with the original script. Then next, we must ask apache to get the compressed version if there is available and finally tell the browser to un-compress the file before it can be used.

To do that, we add these following code in our .htaccess

RewriteEngine on
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.jgz -f
RewriteRule (.*).js$ $1.js.jgz [L]
AddType "text/javascript" .js.jgz
AddEncoding gzip .jgz

That’s all. Yeah.. And if you want to look for another method this is it.:




Font Zoomer JavaScript

This idea come from the Wordpress Zoom Box Plugin. It’s a widget used to change websites font automatically. This simple widget is easy to made and useful for blog / website. This is how to do it:

Adding the Zoom Box

First of all, add a <Div> to make a zoom-box right after <BODY>

<div id="zoom-box">
<p id="zoom-title"><a href="http://thesky.asia/zoom-box/">ZoomBox</a></p>
<p id="zoom-control">
<a href="#" id="zoom-in">-</a>
<span id="zoom-text">10</span>
<a href="#" id="zoom-out">+</a>
</p>
</div>

Floating and Styling the Zoom Box

Make the zoom-box floating as a layer. so, the it won’t interfere the existing layout. This step can be done by css. Setting the box position to absolute and then use the css to dress up the zoom box whatever you like. For example, this is my css code:


<style type="text/css">
#zoom-box {
position: absolute;
top: 10px;
right: 10px;
background-color:#aaa;
border: 1px solid #000;
}
#zoom-box * {
text-align:center;
font-size: 9pt;
margin: 0;
padding: 0;
text-decoration: none;
height: 20px;
}
#zoom-control * {
float: left;
display: block;
width: 25px;
background-color:#FFFFFF;
}
#zoom-control a {
background-color:#CCC;
}
#zoom-control a:hover {
background-color: #333333;
color: #FFFFFF;
}
</style>

Zoom Box Scripting

With above two steps, we’ve got a simple zoom-box widget. But it won’t work yet. To get our zoom-box works, we can use javascript like this:


<script language="javascript">
window.onload = function () {
var iFontSize = 1;
document.getElementById("zoom-in").onclick = function () {
iFontSize -= 0.1;
document.body.style.fontSize = iFontSize + "em";
document.getElementById("zoom-text").innerHTML = Math.round( iFontSize * 10 );
}
document.getElementById("zoom-out").onclick = function () {
iFontSize += 0.1;
document.body.style.fontSize = iFontSize + "em";
document.getElementById("zoom-text").innerHTML = Math.round( iFontSize * 10 );
}
}
</script>

That must works now. If it won’t work, please recheck your HTML / XHTML. Maybe it’s not valid or the page’s structure is broken. Please fix it and try again.




Using Prototype

To using prototype, first we must download it. It’s only 1 file named ‘prototype.js‘. It is about 120kb, a bit too large for a website. but don’t worry, because the 120kb will do more… ;) besides that, we can compress it using any packer. I won’t describe how to compress the file here. If you wanna compress the file, you can learn it on “Compressing JavaScript”.

To use the prototype, first refer to the ‘prototype.js’ as shown below
<script type="text/javascript" src="prototype.js">
</script>

Place it at the <head> tags of your html. Actually you can place it anywhere to use prototype, but It always be good to place it on the head because your pages will look cleaner.

And the main function in prototype is

document.observe('dom:loaded' , function() {
... -> script code here
});

it’s not necessary to include the function if we want to use prototype, but this function guarantee that all the DOM structure has been loaded before the script run. That is a good habid to always use this function.

The Hello World

It’s a good idea to start every programming with the ‘hello world’.

document.observe('dom:loaded' , function() {
$(document).update("Hello World!!");
});

note that we call the sexy function ‘$(document).update‘ to pop out the text. about further of this function, we will learn it together later… by now, this is only ‘Using Prototype




The Prototype-CSS $$() function

The dollar dollar function is Prototype’s CSS Selector Engine. The ‘$$()’ function returns all css match elements. It was used quite same as the CSS selector. For example, to get all tags with the class “myclass”, we use the following:

$$(".myclass")

This function will return the array of elements match the selector string. And We can make the returned elements bold like this:

$$(".myclass").invoke('setStyle', { fontWeight: bold; })

Easy as pie…




The Form $F() function

the $F() function will returns the value of form element.

$F("form_element_id")

Does it need a description anymore??




The Prototype $() function

The dollar function is one of the Prototype Utility Function. $() is the most frequently used prototype’s function. basically it was used to refer an element in the HTML page. The function is quickly shorthand for getElementById.

The usual function identifying an element is:
document.getElementById("element_id")

The $() function reduces the code to:
$("element_id")

For example, you can set the CSS text color with this code:
$("element_id").style.color = "#ffffff";

Or, the “Prototype way”:
$("id_of_element").setStyle({color: '#ffffff'});




Ajax Theme for Wordpress

This is another cool ajaxed wordpress theme. I’m just a newbie in wordpress and ajax. I try to unite ajax and wordpress and i make this theme. so, this is the result of my first work. This theme is available for free, and you can get the download link at the bottom of this post.

Feature of this theme:

  • - jQuery
  • - accordion
  • - font zoomer
  • - fade menubar, etc.
  • - Lightwight, only about 90kb / page.

The theme need this following plugin:

I recommend you to install the plugins first, so the theme will run smoothly. And as an addition, you can install WP PostRatings plugins.

Download Link: the sky themes




Start Using jQuery

jQuery main function is the ‘$(document).ready() function’. Although it’s many way to start jQuery, Always use this function to start using jQuery is good. example:

$(document).ready(function() {
$('.header-company').addClass('highlight');
});

This will add class named ‘highlight’ to element that have ‘header-company’ class. This is used generally for changing / adding class for CSS. So, when the document was loaded, the jQuery command will add the CSS Style.




Prototype

Prototype is the most popular Ajax framework. It used by many giant-website such : Apple, Gucci, etc… The Prototype JavaScript Framework is a JavaScript framework created by Sam Stephenson. Prorotype is implemented as a single file of JavaScript code “prototype.js”.

Prototype is also used as the low layer for many framework, for example: script.aculo.us, Rico, ExtJS, Moo.fx, etc. Prototype provides many functions for developing JavaScript web applications. The prorotype features programming shortcuts for accessing element, events, and major functions to deal with Ajax.

Prototype also feature library functions to support object oriented programming that use classes and ojects, something the JavaScript language hard to implement.

You can find and download prototype on their site.




jQuery feature

jQuery provides many general function to build a rich pages. jQuery feature:

  • Ajax
    jQuery is one of the ajax framework that used mostly by many web master.
  • Shortcut access to DOM
    instead of using bulk of code in accessing DOM, jQuery have the shortcut.
  • Manipulating the element content
    With jQuery, we can freely insert, update and delete every HTML element.
  • Easy event handling
    Event handling is no fear again in web pages, jQuery make the process simple.
  • Animation
    by default, jQuery already have ability to perform general animation on the pages.
  • Action Chaining
    Instead of using many temporary variable and repetition lines, we can easily unite several lines of code that have same characteristic to a line code with chaining.
  • CSS Compliance
    The jQuery selector is CSS Compliance, that make any designer that already have knowledge about CSS is able to learn jQuery easily.
  • Cross browser
    using jQuery, no more hack for IE… the framework has done it automatically.






Home  
 


Link to us




RSS Feed of new blogs                                                   Home        Feed Map        Submit Feed      Link to Us       Contact