SlideShare ist ein Scribd-Unternehmen logo
1 von 6
Downloaden Sie, um offline zu lesen
Chap 4. Ajax in Depth         1



                                      4. Ajax in Depth
 1.   Write the Ajax code to illustrate how JavaScript code can be returned from the server by a PHP
      script.
      A PHP script can be used to send a JavaScript back from the server to the client browser. In the client
      browser, this JavaScript can be evaluated by using the eval() function of JavaScript.

      The PHP file JAVASCRIPT.PHP is as follows:
      <?php
           echo 'myalert()';
      ?>

      The HTML file JAVASCRIPT.HTML is as follows:
      <html>
      <head>
      <title>Returning JavaScript</title>
      <script language = "javascript">
      var xhr = false;

      if (window.XMLHttpRequest)
      {
           xhr = new XMLHttpRequest();
      }
      else if (window.ActiveXObject)
      {
           xhr = new ActiveXObject("Microsoft.XMLHTTP");
      }

      function getData(dataSource)
      {
           if(xhr)
           {
                 xhr.open("GET", dataSource);

                      xhr.onreadystatechange = function()
                      {
                           if (xhr.readyState == 4 && xhr.status == 200)
                           {
                                 eval(xhr.responseText);
                           }
                      }

                      xhr.send(null);
              }
      }

      function myalert()
      {
           var targetDiv = document.getElementById("targetDiv");

           targetDiv.innerHTML = "Got the JavaScript OK.";
      }
      </script>
      </head>


mukeshtekwani@hotmail.com                                                       Prof. Mukesh N. Tekwani
2    Ajax in Depth


          <body>
               <H1>Returning JavaScript</H1>

                  <form>
                        <input type = "button" value = "Fetch JavaScript"
                                  onclick = "getData('javascript.php')">
                  </form>

               <div id="targetDiv">
                    <p>The fetched data will go here.</p>
               </div>
          </body>
          </html>

     2.   Write the Ajax code to illustrate how to return a JavaScript object from the server.
          <html>
          <head>
          <title>Converting text to a JavaScript object</title>
          <script>
          var text = "{method: 'adder', operand1: 2, operand2: 3};";
          var jSObject;

          eval('jSObject = '+ text);

          eval(jSObject.method + '(' + jSObject.operand1 + ',' +
          jSObject.operand2 + ');');
          function adder(op1, op2)
          {
               var sum = op1 + op2;
               alert(op1 + " + " + op2 + " = " + sum);
          }
          </script>
          </head>

          <body>
          <h1>Converting text to a JavaScript object</h1>
          </body>
          </html>

     3.   Write Ajax code to get the following header information from the server: data about the server,
          current date and time on server, date of last modification of a file type of document being
          accessed.
          To get the header information, we use the HEAD method with the XMLHttpRequest object open().
          This is instead of the GET method.

          The data transferred from the server are the values of the Http headers returned by the server when an
          Ajax script it tries to read a file on the server. If we send a GET request, we get the text file back from
          the server. But if we send HEAD, we get data about the file or metadata.

          Why is header information important? We can se the information in HTTP header before we try to
          download the resource from the server. The HTTP header will contain information about resource size,
          type, last-modified date, etc. This allows us to decide whether to download a resource. Also if the user
          tries to download a resource that does not exist, we can inform the user.




    Prof. Mukesh N Tekwani                                                      mukeshtekwani@hotmail.com
Chap 4. Ajax in Depth    3


     File HEAD.HTML

     <html>
     <head>
     <title>Getting header information</title>
     <script language = "javascript">
     var xhr = false;
     if (window.XMLHttpRequest)
     {
          xhr = new XMLHttpRequest();
     }
     else if (window.ActiveXObject)
     {
          xhr = new ActiveXObject("Microsoft.XMLHTTP");
     }

     function getData(dataSource, divID)
     {
          if(xhr)
          {
                var obj = document.getElementById(divID);
                xhr.open("HEAD", dataSource); //observe that we have used
                                              // HEAD instead of GET.

                    xhr.onreadystatechange = function()
                    {
                         if (xhr.readyState == 4 && xhr.status == 200)
                         {
                               obj.innerHTML = xhr.getAllResponseHeaders();
                         }
                    }
                    xhr.send(null);
          }
     }
     </script>
     </head>

     <body>
     <H1>Getting header information</H1>
     <form>
           <input type = "button" value = "Display Message"
                           onclick = "getData('data.txt', 'targetDiv')">
     </form>

     <div id="targetDiv">
           <p>The fetched data will go here.</p>
     </div>
     </body>
     </html>

     The output of this, on my computer, was:

     Server: Microsoft-IIS/5.1 X-Powered-By: ASP.NET Date: Tue, 09 Nov 2010 17:54:30 GMT
     Content-Type: text/plain Accept-Ranges: bytes Last-Modified: Thu, 04 Nov 2010 08:15:17
     GMT Etag: "f27876af87bcb1:b44" Content-Length: 43

mukeshtekwani@hotmail.com                                         Prof. Mukesh N. Tekwani
4    Ajax in Depth



     4.   Modify the above program to obtain only the last modified date from the Http header.
          Instead of using getAllResponseHeaders method, we will use the getResponseHeader
          method to get only data for specific header, like this:

          File : DATE.HTML

          <html>
          <head>
          <title>Getting date information</title>
          <script language = "javascript">
          var xhr = false;

          if (window.XMLHttpRequest)
          {
               xhr = new XMLHttpRequest();
          }
          else if (window.ActiveXObject)
          {
               xhr = new ActiveXObject("Microsoft.XMLHTTP");
          }

          function getData(dataSource, divID)
          {
               if(xhr)
               {
                     var obj = document.getElementById(divID);
                     xhr.open("HEAD", dataSource);

                         xhr.onreadystatechange = function()
                         {
                              if (xhr.readyState == 4 && xhr.status == 200)
                              {
                                    obj.innerHTML = "data.txt was last modified on
                                    " + xhr.getResponseHeader("Last-Modified");
                              }
                         }
                         xhr.send(null);
               }
          }
          </script>
          </head>

          <body>
          <H1>Getting date information</H1>
          <form>
          <input type = "button" value = "Display Date" onclick =
                                     "getData('data.txt', 'targetDiv')">
          </form>

          <div id="targetDiv">
               <p>The fetched data will go here.</p>
          </div>
          </body>
          </html>

    Prof. Mukesh N Tekwani                                          mukeshtekwani@hotmail.com
Chap 4. Ajax in Depth      5



 5.   Modify the above program so that it displays the following parts of a date: date, month, year,
      hours, minutes, seconds.
      The modified part is shown below:

      if (xhr.readyState == 4 && xhr.status == 200)
      {
           var date = new Date(xhr.getResponseHeader("Last-Modified"));
           var details;
           details = "Date: " + date.getDate() + '<br>';
           details = details + "Month: " + date.getMonth() + '<br>';
           details = details + "Year: " + date.getFullYear() + '<br>';
           details = details + "Hours: " + date.getHours() + '<br>';
           details = details + "Minutes: " + date.getMinutes() + '<br>';
           details = details + "Seconds: " + date.getSeconds() + '<br>';
           obj.innerHTML =   details
      }

 6.   Write Ajax code to check whether a URL exisis.
      <html>
      <head>
      <title>Returning JavaScript</title>
      <script language = "javascript">
      var xhr = false;
      if (window.XMLHttpRequest)
      {
             xhr = new XMLHttpRequest();
      }
      else if (window.ActiveXObject)
      {
             xhr = new ActiveXObject("Microsoft.XMLHTTP");
      }
      function getData(dataSource, divId)
      {
             if(xhr)
             {
                    var obj = document.getElementById(divId);
                    xhr.open("HEAD", dataSource);
                    xhr.onreadystatechange = function()
                    {
                            if (xhr.readyState == 4 && xhr.status == 200)
                            {
                                   obj.innerHTML = "URL exists";
                            }
                            else
                            {
                                   obj.innerHTML = "URL does not exist";
                    }
             }
             xhr.send(null);
      }
      }
      </script>
      </head>



mukeshtekwani@hotmail.com                                                Prof. Mukesh N. Tekwani
6    Ajax in Depth


         <body>
         <H1>Returning JavaScript</H1>
         <form>
         <input type = "button" value = "Fetch JavaScript"
                    onclick = "getData('data22.txt', 'targetDiv')">
         </form>

         <div id="targetDiv">
              <p>The fetched data will go here.</p>
         </div>
         </body>
         </html>




    Prof. Mukesh N Tekwani                            mukeshtekwani@hotmail.com

Weitere ähnliche Inhalte

Was ist angesagt?

Spring data presentation
Spring data presentationSpring data presentation
Spring data presentationOleksii Usyk
 
Reactive Access to MongoDB from Java 8
Reactive Access to MongoDB from Java 8Reactive Access to MongoDB from Java 8
Reactive Access to MongoDB from Java 8Hermann Hueck
 
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDBJava Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDBMongoDB
 
An introduction into Spring Data
An introduction into Spring DataAn introduction into Spring Data
An introduction into Spring DataOliver Gierke
 
MongoDB + Java - Everything you need to know
MongoDB + Java - Everything you need to know MongoDB + Java - Everything you need to know
MongoDB + Java - Everything you need to know Norberto Leite
 
Web Integration Patterns in the Era of HTML5
Web Integration Patterns in the Era of HTML5Web Integration Patterns in the Era of HTML5
Web Integration Patterns in the Era of HTML5johnwilander
 
Paintfree Object-Document Mapping for MongoDB by Philipp Krenn
Paintfree Object-Document Mapping for MongoDB by Philipp KrennPaintfree Object-Document Mapping for MongoDB by Philipp Krenn
Paintfree Object-Document Mapping for MongoDB by Philipp KrennJavaDayUA
 
JDBC - JPA - Spring Data
JDBC - JPA - Spring DataJDBC - JPA - Spring Data
JDBC - JPA - Spring DataArturs Drozdovs
 
Windows 8 metro applications
Windows 8 metro applicationsWindows 8 metro applications
Windows 8 metro applicationsAlex Golesh
 
Indexing & Query Optimization
Indexing & Query OptimizationIndexing & Query Optimization
Indexing & Query OptimizationMongoDB
 
Advanced java practical semester 6_computer science
Advanced java practical semester 6_computer scienceAdvanced java practical semester 6_computer science
Advanced java practical semester 6_computer scienceNiraj Bharambe
 
Data access 2.0? Please welcome: Spring Data!
Data access 2.0? Please welcome: Spring Data!Data access 2.0? Please welcome: Spring Data!
Data access 2.0? Please welcome: Spring Data!Oliver Gierke
 
Indexing and Query Optimization
Indexing and Query OptimizationIndexing and Query Optimization
Indexing and Query OptimizationMongoDB
 
Modern Android app library stack
Modern Android app library stackModern Android app library stack
Modern Android app library stackTomáš Kypta
 
Java development with MongoDB
Java development with MongoDBJava development with MongoDB
Java development with MongoDBJames Williams
 
Data Management 3: Bulletproof Data Management
Data Management 3: Bulletproof Data ManagementData Management 3: Bulletproof Data Management
Data Management 3: Bulletproof Data ManagementMongoDB
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the TrenchesJonathan Wage
 

Was ist angesagt? (18)

Spring data presentation
Spring data presentationSpring data presentation
Spring data presentation
 
Reactive Access to MongoDB from Java 8
Reactive Access to MongoDB from Java 8Reactive Access to MongoDB from Java 8
Reactive Access to MongoDB from Java 8
 
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDBJava Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
 
An introduction into Spring Data
An introduction into Spring DataAn introduction into Spring Data
An introduction into Spring Data
 
MongoDB + Java - Everything you need to know
MongoDB + Java - Everything you need to know MongoDB + Java - Everything you need to know
MongoDB + Java - Everything you need to know
 
Web Integration Patterns in the Era of HTML5
Web Integration Patterns in the Era of HTML5Web Integration Patterns in the Era of HTML5
Web Integration Patterns in the Era of HTML5
 
Paintfree Object-Document Mapping for MongoDB by Philipp Krenn
Paintfree Object-Document Mapping for MongoDB by Philipp KrennPaintfree Object-Document Mapping for MongoDB by Philipp Krenn
Paintfree Object-Document Mapping for MongoDB by Philipp Krenn
 
JDBC - JPA - Spring Data
JDBC - JPA - Spring DataJDBC - JPA - Spring Data
JDBC - JPA - Spring Data
 
Windows 8 metro applications
Windows 8 metro applicationsWindows 8 metro applications
Windows 8 metro applications
 
Indexing & Query Optimization
Indexing & Query OptimizationIndexing & Query Optimization
Indexing & Query Optimization
 
Advanced java practical semester 6_computer science
Advanced java practical semester 6_computer scienceAdvanced java practical semester 6_computer science
Advanced java practical semester 6_computer science
 
Data access 2.0? Please welcome: Spring Data!
Data access 2.0? Please welcome: Spring Data!Data access 2.0? Please welcome: Spring Data!
Data access 2.0? Please welcome: Spring Data!
 
Indexing and Query Optimization
Indexing and Query OptimizationIndexing and Query Optimization
Indexing and Query Optimization
 
Modern Android app library stack
Modern Android app library stackModern Android app library stack
Modern Android app library stack
 
Java development with MongoDB
Java development with MongoDBJava development with MongoDB
Java development with MongoDB
 
Data Management 3: Bulletproof Data Management
Data Management 3: Bulletproof Data ManagementData Management 3: Bulletproof Data Management
Data Management 3: Bulletproof Data Management
 
Ajax Part II
Ajax Part IIAjax Part II
Ajax Part II
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the Trenches
 

Andere mochten auch

Coding Ajax
Coding AjaxCoding Ajax
Coding AjaxTed Husted
 
Ajax 101 Workshop
Ajax 101 WorkshopAjax 101 Workshop
Ajax 101 WorkshopBill Scott
 
Python reading and writing files
Python reading and writing filesPython reading and writing files
Python reading and writing filesMukesh Tekwani
 
Dom
DomDom
Domsoumya
 
S.E. DBMS-Paper Analysis
S.E. DBMS-Paper AnalysisS.E. DBMS-Paper Analysis
S.E. DBMS-Paper AnalysisMukesh Tekwani
 
OSI Model - Every Detail Explained
OSI Model - Every Detail ExplainedOSI Model - Every Detail Explained
OSI Model - Every Detail ExplainedAshish Malik
 
TCP-IP Reference Model
TCP-IP Reference ModelTCP-IP Reference Model
TCP-IP Reference ModelMukesh Tekwani
 
OSI Model of Networking
OSI Model of NetworkingOSI Model of Networking
OSI Model of NetworkingMukesh Tekwani
 

Andere mochten auch (12)

Coding Ajax
Coding AjaxCoding Ajax
Coding Ajax
 
Ajax 101 Workshop
Ajax 101 WorkshopAjax 101 Workshop
Ajax 101 Workshop
 
Python reading and writing files
Python reading and writing filesPython reading and writing files
Python reading and writing files
 
Ajax part i
Ajax part iAjax part i
Ajax part i
 
Dom
DomDom
Dom
 
Perl Chapter 1
Perl Chapter 1Perl Chapter 1
Perl Chapter 1
 
S.E. DBMS-Paper Analysis
S.E. DBMS-Paper AnalysisS.E. DBMS-Paper Analysis
S.E. DBMS-Paper Analysis
 
AJAX
AJAXAJAX
AJAX
 
XML
XMLXML
XML
 
OSI Model - Every Detail Explained
OSI Model - Every Detail ExplainedOSI Model - Every Detail Explained
OSI Model - Every Detail Explained
 
TCP-IP Reference Model
TCP-IP Reference ModelTCP-IP Reference Model
TCP-IP Reference Model
 
OSI Model of Networking
OSI Model of NetworkingOSI Model of Networking
OSI Model of Networking
 

Ă„hnlich wie Ajax chap 4

Ă„hnlich wie Ajax chap 4 (20)

jQuery - Chapter 5 - Ajax
jQuery - Chapter 5 -  AjaxjQuery - Chapter 5 -  Ajax
jQuery - Chapter 5 - Ajax
 
Ajax for dummies, and not only.
Ajax for dummies, and not only.Ajax for dummies, and not only.
Ajax for dummies, and not only.
 
Xml http request
Xml http requestXml http request
Xml http request
 
Lec 7
Lec 7Lec 7
Lec 7
 
AJAX.pptx
AJAX.pptxAJAX.pptx
AJAX.pptx
 
Web 11 | AJAX + JSON + PHP
Web 11 | AJAX + JSON + PHPWeb 11 | AJAX + JSON + PHP
Web 11 | AJAX + JSON + PHP
 
J query 01.07.2013.html
J query 01.07.2013.htmlJ query 01.07.2013.html
J query 01.07.2013.html
 
J query 01.07.2013
J query 01.07.2013J query 01.07.2013
J query 01.07.2013
 
Ajax
AjaxAjax
Ajax
 
JavaScript Lessons 2023
JavaScript Lessons 2023JavaScript Lessons 2023
JavaScript Lessons 2023
 
Unit-5.pptx
Unit-5.pptxUnit-5.pptx
Unit-5.pptx
 
Ajax.ppt
Ajax.pptAjax.ppt
Ajax.ppt
 
Building Applications Using Ajax
Building Applications Using AjaxBuilding Applications Using Ajax
Building Applications Using Ajax
 
Ajaxtechnicalworkshopshortversion 1224845432835700-8
Ajaxtechnicalworkshopshortversion 1224845432835700-8Ajaxtechnicalworkshopshortversion 1224845432835700-8
Ajaxtechnicalworkshopshortversion 1224845432835700-8
 
Javascript - Beyond-jQuery
Javascript - Beyond-jQueryJavascript - Beyond-jQuery
Javascript - Beyond-jQuery
 
The Principle of Hybrid App.
The Principle of Hybrid App.The Principle of Hybrid App.
The Principle of Hybrid App.
 
16 18
16 1816 18
16 18
 
Ajax - a quick introduction
Ajax - a quick introductionAjax - a quick introduction
Ajax - a quick introduction
 
Softshake - Offline applications
Softshake - Offline applicationsSoftshake - Offline applications
Softshake - Offline applications
 
Html5 For Jjugccc2009fall
Html5 For Jjugccc2009fallHtml5 For Jjugccc2009fall
Html5 For Jjugccc2009fall
 

Mehr von Mukesh Tekwani

ISCE-Class 12-Question Bank - Electrostatics - Physics
ISCE-Class 12-Question Bank - Electrostatics  -  PhysicsISCE-Class 12-Question Bank - Electrostatics  -  Physics
ISCE-Class 12-Question Bank - Electrostatics - PhysicsMukesh Tekwani
 
Hexadecimal to binary conversion
Hexadecimal to binary conversion Hexadecimal to binary conversion
Hexadecimal to binary conversion Mukesh Tekwani
 
Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Mukesh Tekwani
 
Hexadecimal to octal conversion
Hexadecimal to octal conversionHexadecimal to octal conversion
Hexadecimal to octal conversionMukesh Tekwani
 
Gray code to binary conversion
Gray code to binary conversion Gray code to binary conversion
Gray code to binary conversion Mukesh Tekwani
 
What is Gray Code?
What is Gray Code? What is Gray Code?
What is Gray Code? Mukesh Tekwani
 
Decimal to Binary conversion
Decimal to Binary conversionDecimal to Binary conversion
Decimal to Binary conversionMukesh Tekwani
 
Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21Mukesh Tekwani
 
Refraction and dispersion of light through a prism
Refraction and dispersion of light through a prismRefraction and dispersion of light through a prism
Refraction and dispersion of light through a prismMukesh Tekwani
 
Refraction of light at a plane surface
Refraction of light at a plane surfaceRefraction of light at a plane surface
Refraction of light at a plane surfaceMukesh Tekwani
 
Spherical mirrors
Spherical mirrorsSpherical mirrors
Spherical mirrorsMukesh Tekwani
 
Atom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atomAtom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atomMukesh Tekwani
 
Refraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lensesRefraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lensesMukesh Tekwani
 
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGEISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGEMukesh Tekwani
 
TCP-IP Reference Model
TCP-IP Reference ModelTCP-IP Reference Model
TCP-IP Reference ModelMukesh Tekwani
 

Mehr von Mukesh Tekwani (20)

Circular motion
Circular motionCircular motion
Circular motion
 
Gravitation
GravitationGravitation
Gravitation
 
ISCE-Class 12-Question Bank - Electrostatics - Physics
ISCE-Class 12-Question Bank - Electrostatics  -  PhysicsISCE-Class 12-Question Bank - Electrostatics  -  Physics
ISCE-Class 12-Question Bank - Electrostatics - Physics
 
Hexadecimal to binary conversion
Hexadecimal to binary conversion Hexadecimal to binary conversion
Hexadecimal to binary conversion
 
Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Hexadecimal to decimal conversion
Hexadecimal to decimal conversion
 
Hexadecimal to octal conversion
Hexadecimal to octal conversionHexadecimal to octal conversion
Hexadecimal to octal conversion
 
Gray code to binary conversion
Gray code to binary conversion Gray code to binary conversion
Gray code to binary conversion
 
What is Gray Code?
What is Gray Code? What is Gray Code?
What is Gray Code?
 
Decimal to Binary conversion
Decimal to Binary conversionDecimal to Binary conversion
Decimal to Binary conversion
 
Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21
 
Refraction and dispersion of light through a prism
Refraction and dispersion of light through a prismRefraction and dispersion of light through a prism
Refraction and dispersion of light through a prism
 
Refraction of light at a plane surface
Refraction of light at a plane surfaceRefraction of light at a plane surface
Refraction of light at a plane surface
 
Spherical mirrors
Spherical mirrorsSpherical mirrors
Spherical mirrors
 
Atom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atomAtom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atom
 
Refraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lensesRefraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lenses
 
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGEISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
 
Cyber Laws
Cyber LawsCyber Laws
Cyber Laws
 
XML
XMLXML
XML
 
Social media
Social mediaSocial media
Social media
 
TCP-IP Reference Model
TCP-IP Reference ModelTCP-IP Reference Model
TCP-IP Reference Model
 

KĂĽrzlich hochgeladen

AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinojohnmickonozaleda
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 

KĂĽrzlich hochgeladen (20)

AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipino
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 

Ajax chap 4

  • 1. Chap 4. Ajax in Depth 1 4. Ajax in Depth 1. Write the Ajax code to illustrate how JavaScript code can be returned from the server by a PHP script. A PHP script can be used to send a JavaScript back from the server to the client browser. In the client browser, this JavaScript can be evaluated by using the eval() function of JavaScript. The PHP file JAVASCRIPT.PHP is as follows: <?php echo 'myalert()'; ?> The HTML file JAVASCRIPT.HTML is as follows: <html> <head> <title>Returning JavaScript</title> <script language = "javascript"> var xhr = false; if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else if (window.ActiveXObject) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } function getData(dataSource) { if(xhr) { xhr.open("GET", dataSource); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { eval(xhr.responseText); } } xhr.send(null); } } function myalert() { var targetDiv = document.getElementById("targetDiv"); targetDiv.innerHTML = "Got the JavaScript OK."; } </script> </head> mukeshtekwani@hotmail.com Prof. Mukesh N. Tekwani
  • 2. 2 Ajax in Depth <body> <H1>Returning JavaScript</H1> <form> <input type = "button" value = "Fetch JavaScript" onclick = "getData('javascript.php')"> </form> <div id="targetDiv"> <p>The fetched data will go here.</p> </div> </body> </html> 2. Write the Ajax code to illustrate how to return a JavaScript object from the server. <html> <head> <title>Converting text to a JavaScript object</title> <script> var text = "{method: 'adder', operand1: 2, operand2: 3};"; var jSObject; eval('jSObject = '+ text); eval(jSObject.method + '(' + jSObject.operand1 + ',' + jSObject.operand2 + ');'); function adder(op1, op2) { var sum = op1 + op2; alert(op1 + " + " + op2 + " = " + sum); } </script> </head> <body> <h1>Converting text to a JavaScript object</h1> </body> </html> 3. Write Ajax code to get the following header information from the server: data about the server, current date and time on server, date of last modification of a file type of document being accessed. To get the header information, we use the HEAD method with the XMLHttpRequest object open(). This is instead of the GET method. The data transferred from the server are the values of the Http headers returned by the server when an Ajax script it tries to read a file on the server. If we send a GET request, we get the text file back from the server. But if we send HEAD, we get data about the file or metadata. Why is header information important? We can se the information in HTTP header before we try to download the resource from the server. The HTTP header will contain information about resource size, type, last-modified date, etc. This allows us to decide whether to download a resource. Also if the user tries to download a resource that does not exist, we can inform the user. Prof. Mukesh N Tekwani mukeshtekwani@hotmail.com
  • 3. Chap 4. Ajax in Depth 3 File HEAD.HTML <html> <head> <title>Getting header information</title> <script language = "javascript"> var xhr = false; if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else if (window.ActiveXObject) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } function getData(dataSource, divID) { if(xhr) { var obj = document.getElementById(divID); xhr.open("HEAD", dataSource); //observe that we have used // HEAD instead of GET. xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { obj.innerHTML = xhr.getAllResponseHeaders(); } } xhr.send(null); } } </script> </head> <body> <H1>Getting header information</H1> <form> <input type = "button" value = "Display Message" onclick = "getData('data.txt', 'targetDiv')"> </form> <div id="targetDiv"> <p>The fetched data will go here.</p> </div> </body> </html> The output of this, on my computer, was: Server: Microsoft-IIS/5.1 X-Powered-By: ASP.NET Date: Tue, 09 Nov 2010 17:54:30 GMT Content-Type: text/plain Accept-Ranges: bytes Last-Modified: Thu, 04 Nov 2010 08:15:17 GMT Etag: "f27876af87bcb1:b44" Content-Length: 43 mukeshtekwani@hotmail.com Prof. Mukesh N. Tekwani
  • 4. 4 Ajax in Depth 4. Modify the above program to obtain only the last modified date from the Http header. Instead of using getAllResponseHeaders method, we will use the getResponseHeader method to get only data for specific header, like this: File : DATE.HTML <html> <head> <title>Getting date information</title> <script language = "javascript"> var xhr = false; if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else if (window.ActiveXObject) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } function getData(dataSource, divID) { if(xhr) { var obj = document.getElementById(divID); xhr.open("HEAD", dataSource); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { obj.innerHTML = "data.txt was last modified on " + xhr.getResponseHeader("Last-Modified"); } } xhr.send(null); } } </script> </head> <body> <H1>Getting date information</H1> <form> <input type = "button" value = "Display Date" onclick = "getData('data.txt', 'targetDiv')"> </form> <div id="targetDiv"> <p>The fetched data will go here.</p> </div> </body> </html> Prof. Mukesh N Tekwani mukeshtekwani@hotmail.com
  • 5. Chap 4. Ajax in Depth 5 5. Modify the above program so that it displays the following parts of a date: date, month, year, hours, minutes, seconds. The modified part is shown below: if (xhr.readyState == 4 && xhr.status == 200) { var date = new Date(xhr.getResponseHeader("Last-Modified")); var details; details = "Date: " + date.getDate() + '<br>'; details = details + "Month: " + date.getMonth() + '<br>'; details = details + "Year: " + date.getFullYear() + '<br>'; details = details + "Hours: " + date.getHours() + '<br>'; details = details + "Minutes: " + date.getMinutes() + '<br>'; details = details + "Seconds: " + date.getSeconds() + '<br>'; obj.innerHTML = details } 6. Write Ajax code to check whether a URL exisis. <html> <head> <title>Returning JavaScript</title> <script language = "javascript"> var xhr = false; if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else if (window.ActiveXObject) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } function getData(dataSource, divId) { if(xhr) { var obj = document.getElementById(divId); xhr.open("HEAD", dataSource); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { obj.innerHTML = "URL exists"; } else { obj.innerHTML = "URL does not exist"; } } xhr.send(null); } } </script> </head> mukeshtekwani@hotmail.com Prof. Mukesh N. Tekwani
  • 6. 6 Ajax in Depth <body> <H1>Returning JavaScript</H1> <form> <input type = "button" value = "Fetch JavaScript" onclick = "getData('data22.txt', 'targetDiv')"> </form> <div id="targetDiv"> <p>The fetched data will go here.</p> </div> </body> </html> Prof. Mukesh N Tekwani mukeshtekwani@hotmail.com