Showing posts with label named callback functions. Show all posts
Showing posts with label named callback functions. Show all posts

Programming in Ajax - Named Callback Functions

// 2. Named Callback Functions

<!DOCTYPE html>

<html>

<head>

<script>

     function loadDoc(fncall)

     {

          var xhttp=new XMLHttpRequest();

 

          xhttp.onreadystatechange = function()

          {

               if (this.readyState == 4 && this.status == 200)

              {

                   fncall(this);

              }

          };

          xhttp.open("GET", "Hello.txt", true);

          xhttp.send();

     }

    

     function CallBkFn(xhttp)

     {

          document.getElementById("demo").innerHTML =  xhttp.responseText;

     }

</script>

</head>

<body>

<div id="demo">

<h2>Ajax callback functions </h2>

<button type="button" onclick="loadDoc(CallBkFn)"> Change Content </button>

</div>

</body>

</html>

 

// Input file – Hello.txt

<h2>Hello and Welcome to Ajax</h2>

Programming in Ajax - Anonymous Callback Functions

// 1. Anonymous Callback Functions

<!DOCTYPE html>

<html>

<head>

<script>

function loadDoc()

{

     var xhttp = new XMLHttpRequest();

 

     // Callback using Anonymous Function

     xhttp.onreadystatechange = function()

     {

          if (this.readyState == 4 && this.status == 200)

          {

              document.getElementById("id1").innerHTML = this.responseText;

          }

     };

     xhttp.open("GET", "Hello.txt", true);

     xhttp.send();

}

</script>

</head>

<body>

<div id="id1">

<h2>The XMLHttpRequest Object</h2>

<h2>Ajax – Anonymous Callback Functions <h2>

<button type="button" onclick="loadDoc()">Change Content</button>

</div>

</body>

</html>

 

 

// Input file - Hello.txt

<h2>Hello and Welcome to Ajax</h2>