Saturday 15 July 2023

Javascript Ajax Code


Simple Ajax code
Ajax Call onkeyup Event in jsp


<!--  Simple Ajax code  -->

<script language="javascript" type="text/javascript">
 var xmlHttp;
 function modifyData(Str) {

   if(document.getElementById("td1").style.display=="none")
         {document.getElementById("td1").style.display="block";}
         else{document.getElementById("td1").style.display="none";}

  var versionIds = [ "Msxml2.XMLHTTP", "Microsoft.XMLHTTP",
    "Microsoft.XMLDOM", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0",
    "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.3.0",
    "Msxml2.XMLHTTP.2.6", "Microsoft.XMLHTTP.1.0",
    "Microsoft.XMLHTTP.1" ];
  if (typeof XMLHttpRequest != "undefined") {
   xmlHttp = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
   for ( var i = 0; i < versionIds.length; i++) {
    try {

     return new ActiveXObject(versionIds[i]);

    } catch (e) {
     alert("Browser does not support XMLHTTP Request");
    }
   }
   new XDomainRequest();
  }
  if (xmlHttp == null) {
   alert("Browser does not support XMLHTTP Request");
   return;
  }

  var url = "outPut.jsp?";
  url += "id=" + Str;
  url += "&sid=" + Math.random();
  xmlHttp.onreadystatechange = stateChange;
  xmlHttp.open("POST", url, true);
  xmlHttp.send(null);

  function stateChange() {
   if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
     document.getElementById("td1").innerHTML=xmlHttp.responseText;
   }
  }
 }

</script>

    <table align="center" >
       <tr>
            <td><input type="text" size="30" maxlength="50" name="search1" value=""/><input type="button"     name="searchbtn" value="Search" onclick='modifyData(this.value);'/></td>
       </tr>
       <tr>
            <td id="td1"></td>
       </tr>
    </table>

No comments:

Post a Comment