尊敬的 微信汇率:1円 ≈ 0.046078 元 支付宝汇率:1円 ≈ 0.046168元 [退出登录]
SlideShare a Scribd company logo
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

More Related Content

What's hot

Spring data presentation
Spring data presentationSpring data presentation
Spring data presentation
Oleksii 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 8
Hermann Hueck
 
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDBJava Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
MongoDB
 
An introduction into Spring Data
An introduction into Spring DataAn introduction into Spring Data
An introduction into Spring Data
Oliver 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 HTML5
johnwilander
 
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
JavaDayUA
 
JDBC - JPA - Spring Data
JDBC - JPA - Spring DataJDBC - JPA - Spring Data
JDBC - JPA - Spring Data
Arturs Drozdovs
 
Windows 8 metro applications
Windows 8 metro applicationsWindows 8 metro applications
Windows 8 metro applications
Alex Golesh
 
Indexing & Query Optimization
Indexing & Query OptimizationIndexing & Query Optimization
Indexing & Query Optimization
MongoDB
 
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
Niraj 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 Optimization
MongoDB
 
Modern Android app library stack
Modern Android app library stackModern Android app library stack
Modern Android app library stack
Tomáš Kypta
 
Java development with MongoDB
Java development with MongoDBJava development with MongoDB
Java development with MongoDB
James Williams
 
Data Management 3: Bulletproof Data Management
Data Management 3: Bulletproof Data ManagementData Management 3: Bulletproof Data Management
Data Management 3: Bulletproof Data Management
MongoDB
 
Ajax Part II
Ajax Part IIAjax Part II
Ajax Part II
Mukesh Tekwani
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the Trenches
Jonathan Wage
 

What's hot (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
 

Viewers also liked

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

Viewers also liked (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
 

Similar to Ajax chap 4

jQuery - Chapter 5 - Ajax
jQuery - Chapter 5 -  AjaxjQuery - Chapter 5 -  Ajax
jQuery - Chapter 5 - Ajax
WebStackAcademy
 
Ajax for dummies, and not only.
Ajax for dummies, and not only.Ajax for dummies, and not only.
Ajax for dummies, and not only.
Nerd Tzanetopoulos
 
Xml http request
Xml http requestXml http request
Xml http request
Jayalakshmi Ayyappan
 
Lec 7
Lec 7Lec 7
AJAX.pptx
AJAX.pptxAJAX.pptx
AJAX.pptx
ssuser0a07a1
 
Web 11 | AJAX + JSON + PHP
Web 11 | AJAX + JSON + PHPWeb 11 | AJAX + JSON + PHP
Web 11 | AJAX + JSON + PHP
Mohammad Imam Hossain
 
J query 01.07.2013
J query 01.07.2013J query 01.07.2013
J query 01.07.2013
Rajasekharreddy Kadasani
 
J query 01.07.2013.html
J query 01.07.2013.htmlJ query 01.07.2013.html
J query 01.07.2013.html
Rajasekharreddy Kadasani
 
Ajax
AjaxAjax
Ajax
Yoga Raja
 
JavaScript Lessons 2023
JavaScript Lessons 2023JavaScript Lessons 2023
JavaScript Lessons 2023
Laurence Svekis ✔
 
Unit-5.pptx
Unit-5.pptxUnit-5.pptx
Unit-5.pptx
itzkuu01
 
Ajax.ppt
Ajax.pptAjax.ppt
Ajax.ppt
ssuser9d62d6
 
Building Applications Using Ajax
Building Applications Using AjaxBuilding Applications Using Ajax
Building Applications Using Ajax
s_pradeep
 
Ajaxtechnicalworkshopshortversion 1224845432835700-8
Ajaxtechnicalworkshopshortversion 1224845432835700-8Ajaxtechnicalworkshopshortversion 1224845432835700-8
Ajaxtechnicalworkshopshortversion 1224845432835700-8
anuradha raheja
 
Javascript - Beyond-jQuery
Javascript - Beyond-jQueryJavascript - Beyond-jQuery
The Principle of Hybrid App.
The Principle of Hybrid App.The Principle of Hybrid App.
The Principle of Hybrid App.
musart Park
 
16 18
16 1816 18
Ajax - a quick introduction
Ajax - a quick introductionAjax - a quick introduction
Ajax - a quick introduction
Stefan Pettersson
 
Softshake - Offline applications
Softshake - Offline applicationsSoftshake - Offline applications
Softshake - Offline applications
jeromevdl
 
Html5 For Jjugccc2009fall
Html5 For Jjugccc2009fallHtml5 For Jjugccc2009fall
Html5 For Jjugccc2009fall
Shumpei Shiraishi
 

Similar to 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
J query 01.07.2013J query 01.07.2013
J query 01.07.2013
 
J query 01.07.2013.html
J query 01.07.2013.htmlJ query 01.07.2013.html
J query 01.07.2013.html
 
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
 

More from Mukesh Tekwani

Computer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube ChannelComputer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube Channel
Mukesh Tekwani
 
The Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdfThe Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdf
Mukesh Tekwani
 
Circular motion
Circular motionCircular motion
Circular motion
Mukesh Tekwani
 
Gravitation
GravitationGravitation
Gravitation
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 - Physics
Mukesh 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 conversion
Mukesh 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 conversion
Mukesh 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-21
Mukesh 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 prism
Mukesh 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 surface
Mukesh Tekwani
 
Spherical mirrors
Spherical mirrorsSpherical mirrors
Spherical mirrors
Mukesh 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 atom
Mukesh 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 lenses
Mukesh 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 - WEIGHTAGE
Mukesh Tekwani
 
Cyber Laws
Cyber LawsCyber Laws
Cyber Laws
Mukesh Tekwani
 
XML
XMLXML

More from Mukesh Tekwani (20)

Computer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube ChannelComputer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube Channel
 
The Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdfThe Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdf
 
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
 

Recently uploaded

220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science
Kalna College
 
managing Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptxmanaging Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptx
nabaegha
 
Angle-or,,,,,-Pull-of-Muscleexercise therapy.pptx
Angle-or,,,,,-Pull-of-Muscleexercise therapy.pptxAngle-or,,,,,-Pull-of-Muscleexercise therapy.pptx
Angle-or,,,,,-Pull-of-Muscleexercise therapy.pptx
siddhimeena3
 
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
yarusun
 
Keynote given on June 24 for MASSP at Grand Traverse City
Keynote given on June 24 for MASSP at Grand Traverse CityKeynote given on June 24 for MASSP at Grand Traverse City
Keynote given on June 24 for MASSP at Grand Traverse City
PJ Caposey
 
A Quiz on Drug Abuse Awareness by Quizzito
A Quiz on Drug Abuse Awareness by QuizzitoA Quiz on Drug Abuse Awareness by Quizzito
A Quiz on Drug Abuse Awareness by Quizzito
Quizzito The Quiz Society of Gargi College
 
Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024
khabri85
 
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
Kalna College
 
Diversity Quiz Prelims by Quiz Club, IIT Kanpur
Diversity Quiz Prelims by Quiz Club, IIT KanpurDiversity Quiz Prelims by Quiz Club, IIT Kanpur
Diversity Quiz Prelims by Quiz Club, IIT Kanpur
Quiz Club IIT Kanpur
 
Creativity for Innovation and Speechmaking
Creativity for Innovation and SpeechmakingCreativity for Innovation and Speechmaking
Creativity for Innovation and Speechmaking
MattVassar1
 
Hospital pharmacy and it's organization (1).pdf
Hospital pharmacy and it's organization (1).pdfHospital pharmacy and it's organization (1).pdf
Hospital pharmacy and it's organization (1).pdf
ShwetaGawande8
 
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH LỚP 9 - GLOBAL SUCCESS - FORM MỚI 2025 - C...
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH LỚP 9 - GLOBAL SUCCESS - FORM MỚI 2025 - C...BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH LỚP 9 - GLOBAL SUCCESS - FORM MỚI 2025 - C...
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH LỚP 9 - GLOBAL SUCCESS - FORM MỚI 2025 - C...
Nguyen Thanh Tu Collection
 
220711130097 Tulip Samanta Concept of Information and Communication Technology
220711130097 Tulip Samanta Concept of Information and Communication Technology220711130097 Tulip Samanta Concept of Information and Communication Technology
220711130097 Tulip Samanta Concept of Information and Communication Technology
Kalna College
 
How to Create User Notification in Odoo 17
How to Create User Notification in Odoo 17How to Create User Notification in Odoo 17
How to Create User Notification in Odoo 17
Celine George
 
Images as attribute values in the Odoo 17
Images as attribute values in the Odoo 17Images as attribute values in the Odoo 17
Images as attribute values in the Odoo 17
Celine George
 
Non-Verbal Communication for Tech Professionals
Non-Verbal Communication for Tech ProfessionalsNon-Verbal Communication for Tech Professionals
Non-Verbal Communication for Tech Professionals
MattVassar1
 
nutrition in plants chapter 1 class 7...
nutrition in plants chapter 1 class 7...nutrition in plants chapter 1 class 7...
nutrition in plants chapter 1 class 7...
chaudharyreet2244
 
Erasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES CroatiaErasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES Croatia
whatchangedhowreflec
 
Opportunity scholarships and the schools that receive them
Opportunity scholarships and the schools that receive themOpportunity scholarships and the schools that receive them
Opportunity scholarships and the schools that receive them
EducationNC
 
Music Business Model Presentation Full Sail University
Music Business Model Presentation Full Sail UniversityMusic Business Model Presentation Full Sail University
Music Business Model Presentation Full Sail University
camakaiclarkmusic
 

Recently uploaded (20)

220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science
 
managing Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptxmanaging Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptx
 
Angle-or,,,,,-Pull-of-Muscleexercise therapy.pptx
Angle-or,,,,,-Pull-of-Muscleexercise therapy.pptxAngle-or,,,,,-Pull-of-Muscleexercise therapy.pptx
Angle-or,,,,,-Pull-of-Muscleexercise therapy.pptx
 
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
 
Keynote given on June 24 for MASSP at Grand Traverse City
Keynote given on June 24 for MASSP at Grand Traverse CityKeynote given on June 24 for MASSP at Grand Traverse City
Keynote given on June 24 for MASSP at Grand Traverse City
 
A Quiz on Drug Abuse Awareness by Quizzito
A Quiz on Drug Abuse Awareness by QuizzitoA Quiz on Drug Abuse Awareness by Quizzito
A Quiz on Drug Abuse Awareness by Quizzito
 
Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024
 
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
 
Diversity Quiz Prelims by Quiz Club, IIT Kanpur
Diversity Quiz Prelims by Quiz Club, IIT KanpurDiversity Quiz Prelims by Quiz Club, IIT Kanpur
Diversity Quiz Prelims by Quiz Club, IIT Kanpur
 
Creativity for Innovation and Speechmaking
Creativity for Innovation and SpeechmakingCreativity for Innovation and Speechmaking
Creativity for Innovation and Speechmaking
 
Hospital pharmacy and it's organization (1).pdf
Hospital pharmacy and it's organization (1).pdfHospital pharmacy and it's organization (1).pdf
Hospital pharmacy and it's organization (1).pdf
 
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH LỚP 9 - GLOBAL SUCCESS - FORM MỚI 2025 - C...
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH LỚP 9 - GLOBAL SUCCESS - FORM MỚI 2025 - C...BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH LỚP 9 - GLOBAL SUCCESS - FORM MỚI 2025 - C...
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH LỚP 9 - GLOBAL SUCCESS - FORM MỚI 2025 - C...
 
220711130097 Tulip Samanta Concept of Information and Communication Technology
220711130097 Tulip Samanta Concept of Information and Communication Technology220711130097 Tulip Samanta Concept of Information and Communication Technology
220711130097 Tulip Samanta Concept of Information and Communication Technology
 
How to Create User Notification in Odoo 17
How to Create User Notification in Odoo 17How to Create User Notification in Odoo 17
How to Create User Notification in Odoo 17
 
Images as attribute values in the Odoo 17
Images as attribute values in the Odoo 17Images as attribute values in the Odoo 17
Images as attribute values in the Odoo 17
 
Non-Verbal Communication for Tech Professionals
Non-Verbal Communication for Tech ProfessionalsNon-Verbal Communication for Tech Professionals
Non-Verbal Communication for Tech Professionals
 
nutrition in plants chapter 1 class 7...
nutrition in plants chapter 1 class 7...nutrition in plants chapter 1 class 7...
nutrition in plants chapter 1 class 7...
 
Erasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES CroatiaErasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES Croatia
 
Opportunity scholarships and the schools that receive them
Opportunity scholarships and the schools that receive themOpportunity scholarships and the schools that receive them
Opportunity scholarships and the schools that receive them
 
Music Business Model Presentation Full Sail University
Music Business Model Presentation Full Sail UniversityMusic Business Model Presentation Full Sail University
Music Business Model Presentation Full Sail University
 

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
  翻译: