Ajax : Introduction

AJAX stands for Asynchronous JavaScript And XML.
AJAX is a type of programming made popular in 2005 by Google (with Google Suggest).
AJAX is not a new programming language, but a new way to use existing standards.
With AJAX you can create better, faster, and more user-friendly web applications.

AJAX is based on the following web standards:

  • JavaScript
  • XML
  • HTML
  • CSS

The XMLHttpRequest Object

By using the XMLHttpRequest object, a web developer can update a page with data from the server after the page has loaded!

The onreadystatechange Property

After a request to the server, we need a function that can receive the data that is returned by the server.
The onreadystatechange property stores your function that will process the response from a server. This is not a method, the function is stored in the property to be called automatically. The following code sets the onreadystatechange property and stores an empty function inside it.

The readyState Property

The readyState property holds the status of the server’s response. Each time the readyState changes, the onreadystatechange function will be executed.

Here are the possible values for the readyState property:

State Description
0 The request is not initialized
1 The request has been set up
2 The request has been sent
3 The request is in process
4 The request is complete

The responseText Property

The data sent back from the server can be retrieved with the responseText property.

In our code, we will set the value of our “time” input field equal to responseText:

xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value=xmlHttp.responseText;
}
}

The below are the example code to illustrate the Ajax functionalities,

Name: Time:

<html>
<body>

<script type=”text/javascript”>
function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
catch (e)
{
alert(“Your browser does not support AJAX!”);
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value=xmlHttp.responseText;
}
}
xmlHttp.open(“GET”,”test.php”,true);
xmlHttp.send(null);
}
</script>

<form name=”myForm”>
Name: <input type=”text”
onkeyup=”ajaxFunction();” name=”username” />
Time: <input type=”text” name=”time” />
</form>

</body>
</html>

Test.php

<?php
echo date("H:i:s");
?>

Google: Transliteration with Custom Control

With the AJAX Language API for Transliteration, you can enable transliteration on any textfield or textarea in you webpage. This will help your website users to type in any language using an English keyboard.

What is transliteration? Transliteration is the process of phonetically converting a word written in one script into another. Transliteration should not be confused with translation, which involves a change in language while preserving meaning. With transliteration, it is the sound of the words that are converted from one alphabet to the other.

This is an advanced example which shows how to create your own custom control to control the transliteration. It uses a checkbox for toggling between English and Hindi typing modes and a dropdown to change the destination language. It also registers event handlers for various events that can be raised by the Transliteration Control. See the API class reference for details on the possible events.

<html>

<head>

<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″/>

<script type=”text/javascript” src=”http://www.google.com/jsapi”></script&gt;

<script type=”text/javascript”>

// Load the Google Transliteration API

google.load(“elements”, “1″, {

packages: “transliteration”

});

var transliterationControl;

function onLoad() {

var options = {

sourceLanguage: ‘en’,

destinationLanguage: ['ar','hi','kn','ml','ta','te'],

transliterationEnabled: true,

shortcutKey: ‘ctrl+g’

};

// Create an instance on TransliterationControl with the required

// options.

transliterationControl =

new google.elements.transliteration.TransliterationControl(options);

// Enable transliteration in the textfields with the given ids.

var ids = [ "transl1", "transl2" ];

transliterationControl.makeTransliteratable(ids);

// Add the STATE_CHANGED event handler to correcly maintain the state

// of the checkbox.

transliterationControl.addEventListener(

google.elements.transliteration.TransliterationControl.EventType.STATE_CHANGED,

transliterateStateChangeHandler);

// Add the SERVER_UNREACHABLE event handler to display an error message

// if unable to reach the server.

transliterationControl.addEventListener(

google.elements.transliteration.TransliterationControl.EventType.SERVER_UNREACHABLE,

serverUnreachableHandler);

// Add the SERVER_REACHABLE event handler to remove the error message

// once the server becomes reachable.

transliterationControl.addEventListener(

google.elements.transliteration.TransliterationControl.EventType.SERVER_REACHABLE,

serverReachableHandler);

// Set the checkbox to the correct state.

document.getElementById(‘checkboxId’).checked =

transliterationControl.isTransliterationEnabled();

// Populate the language dropdown

var destinationLanguage =

transliterationControl.getLanguagePair().destinationLanguage;

var languageSelect = document.getElementById(‘languageDropDown’);

var supportedDestinationLanguages =

google.elements.transliteration.getDestinationLanguages(

google.elements.transliteration.LanguageCode.ENGLISH);

for (var lang in supportedDestinationLanguages) {

var opt = document.createElement(‘option’);

opt.text = lang;

opt.value = supportedDestinationLanguages[lang];

if (destinationLanguage == opt.value) {

opt.selected = true;

}

try {

languageSelect.add(opt, null);

} catch (ex) {

languageSelect.add(opt);

}

}

}

// Handler for STATE_CHANGED event which makes sure checkbox status

// reflects the transliteration enabled or disabled status.

function transliterateStateChangeHandler(e) {

document.getElementById(‘checkboxId’).checked = e.transliterationEnabled;

}

// Handler for checkbox’s click event.  Calls toggleTransliteration to toggle

// the transliteration state.

function checkboxClickHandler() {

transliterationControl.toggleTransliteration();

}

// Handler for dropdown option change event.  Calls setLanguagePair to

// set the new language.

function languageChangeHandler() {

var dropdown = document.getElementById(‘languageDropDown’);

transliterationControl.setLanguagePair(

google.elements.transliteration.LanguageCode.ENGLISH,

dropdown.options[dropdown.selectedIndex].value);

}

// SERVER_UNREACHABLE event handler which displays the error message.

function serverUnreachableHandler(e) {

document.getElementById(“errorDiv”).innerHTML =

“Transliteration Server unreachable”;

}

// SERVER_UNREACHABLE event handler which clears the error message.

function serverReachableHandler(e) {

document.getElementById(“errorDiv”).innerHTML = “”;

}

google.setOnLoadCallback(onLoad);

</script>

</head>

<body>


<center>Type in Indian languages (Press Ctrl+g to toggle between English and Hindi)</center>

<div id=’translControl’>

<input type=”checkbox” id=”checkboxId” onclick=”javascript:checkboxClickHandler()”></input>

Type in <select id=”languageDropDown” onchange=”javascript:languageChangeHandler()”></select>

</div>

<br>Title : <input type=’textbox’ id=”transl1″/>

<br>Body<br><textarea id=”transl2″ style=”width:600px;height:200px”></textarea>

<br><div id=”errorDiv”></div>

</body>

</html>

The demo on the Custom Transliteration can view on the below URL,


http://code.google.com/intl/ru/apis/ajaxlanguage/documentation/customtransliteration.html

Follow

Get every new post delivered to your Inbox.