Using Ajax in JavaScript and jQuery


/ Published in: JavaScript
Save to your folder(s)

simple code of using Ajax by JavaScript or jQuery


Copy this code and paste it in your HTML
  1. <script>
  2. function createRequestObject(){
  3. var request_;
  4. var browser = navigator.appName;
  5. if(browser == "Microsoft Internet Explorer"){
  6. request_ = new ActiveXObject("Microsoft.XMLHTTP");
  7. }
  8. else{
  9. request_ = new XMLHttpRequest();
  10. }
  11. return request_;
  12. }
  13. //==============
  14.  
  15. var msgdiv_http = createRequestObject();
  16.  
  17. function msgdiv_getInfo(S1){
  18. t = new Date().getTime();
  19. URL = 'getmd5.php?c='+S1+"&t="+t;
  20. Obj = document.getElementById('msgdiv');
  21. Obj.innerHTML = '<img src="load.gif" border="0">Loading ......';
  22.  
  23. msgdiv_http.open("GET", URL,true);
  24. msgdiv_http.onreadystatechange = msgdiv_handleInfo;
  25. msgdiv_http.send(null);
  26. }
  27.  
  28. function msgdiv_handleInfo(){
  29. Obj = document.getElementById('msgdiv');
  30. if(msgdiv_http.readyState == 2){
  31. Obj.innerHTML ='<img src="load.gif" border="0">Loading ......';
  32. }
  33. if(msgdiv_http.readyState == 4){
  34. Obj.innerHTML = msgdiv_http.responseText;
  35. }
  36. }
  37. </script>
  38. <input type="text" name="T1" size="20">
  39. <input type="button" value="Encode String" id="B1" onclick ="msgdiv_getInfo(T1.value);">
  40. <BR><BR>
  41. <div id="msgdiv"></div>
  42.  
  43.  
  44.  
  45.  
  46. //==================== By jQuery ================
  47. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  48. <script>
  49. $(document).ready(function(){
  50. $("#B1").click(function(){
  51. $("#msgdiv").load("getmd5.php?c="+T1.value);
  52. });
  53. });
  54. </script>
  55. <input type="text" name="T1" size="20">
  56. <input type="button" value="Encode String" id="B1">
  57. <BR><BR>
  58. <div id="msgdiv"></div>

URL: http://function-code.blogspot.com/2013/04/using-ajax-in-javascript-and-jquery.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.