0

I have index.html file having code like this:

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script>
            function ajaxObj(str){
                var xmlhttp;
                if(window.ActiveXObject){
                    try{
                        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                    }catch(e){
                        xmlhttp=false;
                    }

                }
                else{
                    try{
                        xmlhttp=new XMLHttpRequest();
                    }
                    catch(e){
                        xmlhttp=false;
                    }
                }
                if(!xmlhttp)
                    alert("cant create the xmlHttp object");
                else
                    //alert("objet created");

                xmlhttp.onreadystatechange=function(){

                    if(xmlhttp.readyState==4 && xmlhttp.status==200){
                        //  alert("in ready state");
                        var resp=xmlhttp.responseText;
                        document.getElementById("div1").innerHTML=resp;
                        if(resp=="pal"){               
                            document.getElementById("div2").innerHTML="text is pal";
                        }
                        else{
                            document.getElementById("div2").innerHTML="text is something else";
                        }
                    }
                }
                xmlhttp.open("GET","mainJsp.jsp?q="+str,true);
                xmlhttp.send();
            }
        </script>
    </head>

    <body>

        <table>
            <tr>
                <td>
                    <input type="text" name="userInput" onblur="ajaxObj(this.value)"/>
                </td>
            </tr>
            <tr>
                <td>
                    <div id="div1"></div>
                </td>
            </tr>
            <tr>
                <td>
                    <div id="div2"></div>
                </td>
            </tr>

        </table>
    </body>
</html>

and mainJsp.jsp having code like this:

<%
    String textValue=request.getParameter("q");

    if(textValue.equals("pal")){
        out.println(textValue);
    }
    /*if(textValue.equals("mohit")){
        out.println(textValue);
    }*/
    else{
        out.println("else");
    }
%>

whether i enter 'pal' or something else in text box in index.html. only else statement 'text is something else' in javascript function get executed. if statement never get exexuted. Please help

Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85
mohit pal
  • 1
  • 2
  • Did you try to display in the log "xmlhttp.responseText" to know what is the value ? – LOLKFC May 02 '14 at 16:43
  • Yes i have tried.....it will print 'pal' if 'pal' is entered in text field and 'else' if something else is entered – mohit pal May 02 '14 at 16:53

1 Answers1

2

Here is a fiddle with this working. The formatting around your code was a little confusing to me, sorry about that. I took a couple liberties. Let me know if you have a problem with it!

if(textValue === "pal"){
    alert("If");
}
else{
    alert("else");
}

Fiddle

StackOverflow about '===' operator

Community
  • 1
  • 1
TheNorthWes
  • 2,661
  • 19
  • 35
  • I have done the formatting again. Please see. Example in Fiddle link is simple javascript but i here using XMLHttpRequest. May be there is some issue in xmlhttp.requestText. – mohit pal May 02 '14 at 17:00
  • Are you getting any errors in Firebug? It shouldn't matter too much, you said text prints as pal correctly? But the comparison fails? – TheNorthWes May 02 '14 at 17:15