I have tested following code and it works fine in IE but not Mozilla:

var ajax = false;

if (window.XMLHttpRequest) {
ajax = new XMLHttpRequest();
else if (window.ActiveXObject) {

try {
    ajax = new ActiveXObject("Msxml2.XMLHTTP");

catch (e1) {

try {
    ajax = new ActiveXObject("Microsoft.XMLHTTP");
catch (e2) {}

if (!ajax) {
alert ('Some page functionality is unavailable.');

Please tell me what is the solution?

Hi, save yourself time and code lines using a library. top developers has done the hard work sniffing browsers and such.

I can recomen jQuery, so your can use an ajax request that is crossbrowser with a single line of code:

$.ajax(url, data, callback)


What "doesn't work" about your code? I cut and pasted it in firebug and it runs just fine. I see the XMLHttpRequest object in the console window.

This is my code

php Syntax (Toggle Plain Text)

for ($i=0; $i<length; $i++)
    echo '<textarea type="text" name="username' . $i .
         '" cols="60" rows="4" onkeydown="check_username('. $i .')">' .
         $sentences_data[$i] . '</textarea> <span id="username_label' . $i .

javascript Syntax (Toggle Plain Text)

function check_username(index) {
    if (!ajax) {
        alert("ajax not created");
    var username = document.getElementById("username" + index);
    ajax.open("get", "testuser.php?id=" + username);
    ajax.onreadystatechange = function() { handle_check(index); };

// Passing index to this function allows access to correct username_label element.
function handle_check(index) {
    if (ajax.readyState == 4 && ajax.status == 200) {
        document.getElementById("username_label" + index).innerHTML = ajax.responseText;

This works fine in Internet Explorer but nothing happens on Mozilla. Please suggest me where is the mistake?