mohit pal
mohit pal

Reputation: 1

compare responseText with another string

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

Upvotes: 0

Views: 1478

Answers (1)

TheNorthWes
TheNorthWes

Reputation: 2739

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

Upvotes: 2

Related Questions