views:

54

answers:

1

I have the following code:

<script type="text/javascript" language="javascript">
<!--
function getXMLHTTP() { //fuction to return the xml http object
        var xmlhttp=false;    
        try{
            xmlhttp=new XMLHttpRequest();
        }
        catch(e)    {        
            try{            
                xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){
                try{
                req = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(e1){
                    xmlhttp=false;
                }
            }
        }

        return xmlhttp;
    }

    function wait1()
    {
        document.getElementById('comment').innerHTML="Please wait...";    
    }

    function getComment(strURL) {        

        var req = getXMLHTTP();

        if (req) {

            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    // only if "OK"
                    if (req.status == 200) {                        
                        document.getElementById('comment').innerHTML=req.responseText;                        
                    } else {
                        alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                    }
                }                
            }            
            req.open("GET", "comment_form.php", true);
            req.send(null);
        }

    }
//-->
</script>

    <div id="comment">
<form  action="javascript:get(document.getElementById('comment'));wait1()" method="post" enctype="multipart/form-data" >
<input type="submit" name="Submit" value="Post Your Comment" />
</form>
</div>

I am sure I used the same in past running smoothly, but now it doesn't seem to be working. I think something is messed up there, but not able to figure out.

I would be thankful if I get the solution.

A: 

The one bug in the above code, that I found, was: The getComment(strURL) function takes an argument, which is never used. The "comment_form.php" should be replaced with the function's argument instead. And, since software's soft, I renamed strURL to the easier-to-read-and-spell "url".

(That the DIV is presented as opened, but not closed, is a formatting oversight, I take it. The wait1 function is unused here, too.)

There's no need to add the deprecated "language" attribute to the SCRIPT tag, nor to wrap any JS code in HTML comments.

function getXMLHTTP() {
    var x = false;
    try {
        x = new XMLHttpRequest();
    }
    catch(e) {
        try {
            x = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(ex) {
            try {
                req = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e1) {
                x = false;
            }
        }
    }
    return x;
}

/* TODO: Where is this ever used? */
function wait1() {
    document.getElementById('comment').innerHTML = "Please wait...";
}
function getComment(url) {
    var req = getXMLHTTP();
    if (!req) {
        // Complain early, instead of nesting deeply
        alert('Unable to set up the XHR object.'); 
        return;
    }
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            // only if "OK"
            if (req.status == 200) {
                document.getElementById('comment').innerHTML = req.responseText;
            } else {
                alert("There was a problem while using XMLHTTP:\n" + req.statusText);
            }
        }
    };
    req.open("GET", url, true); // the "true" stands for "async", when is this not default?
    req.send(null); // do not add any content (null); when is this not default?
}

I added some questions to the code.

olleolleolle
thanks for the useful informations. and about the unclosed div and the wait1() function, I m sorry as I did not post it properly... but I have edited it now.
Anjali