SlideShare a Scribd company logo
Ajax
Ajax is a catchy name for a type of programming made popular in 2005 by
Google and other big web developers. Ajax loosely stands for
Asynchronous Javascript And XML, but that just sounds like techno jargon
to many people.
Pretty much. By using the programming practice termed "Ajax" you will be
able to trade data, with a web server, without having to load a new page.
Instead of Ajax being seen as "The New Way to Develop Websites", it
should instead be seen as another weapon to add to your programming
arsenal.
This tutorial will introduce you to the basics of Ajax and show you how to
send and receive data from a server without using a "Submit" button
approach.
Ajax - Creating an HTML Form
•

Before we can start getting to the exciting new stuff, we must first make a
standard HTML form (no submit button though!). This form will be spiced
up in later with a hint of Ajax, but for now let's just make a solid, basic
HTML form with a couple inputs.
<html>
<body>
<form name='myForm'>
Name: <input type='text' name='username' /> <br />
Time: <input type='text' name='time' />
</form>
</body>
</html>
Ajax - Where's the Submit Button?
• That's the great thing about Ajax, you do not
need a form submit button to send the user's
data to the server. We are going to be using
our "Javascript on Steroids" to get and submit
data with the server.
• Now that we have our HTML form, we can
dive deeper into the Ajax jungle and try to
discover what we're facing.
Ajax - Browser Support
• This lesson includes one of the largest hurdles
for aspiring Ajax programmers: browser
support. It would be nice if all the web
browsers required the same Javascript code to
use Ajax, but life isn't fair and you've got your
work cut out for you!
Ajax - Try/Catch Blocks of Code
• To create this important Ajax object, you are going to have
to use a special programming technique known as "try and
catch". Basically it attempts to "try" a piece of code and if
that piece causes an error it "catches" the error and keeps
going. Normally when an error occurs the code will stop
running, however, the "catch" eats up the error and lets
the code continue.
• In the following code we are going to "try" three different
ways to make a new XMLHttpRequest object. Every time
we fail and get an error, we will catch the error and try the
next a different command.
• Note: If our "try" is successful then the "catch" code will
not be run because it is only used when there is an error.
Catch & try
<script language="javascript" type="text/javascript">
<!-- //Browser Support Code
function ajaxFunction()
{
var ajaxRequest; // The variable that makes Ajax possible!
try{ajaxRequest = new XMLHttpRequest(); // Opera 8.0+, Firefox, Safari
}
catch (e)
{// Internet Explorer Browsers
try{ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try{ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");}
catch (e){// Something went wrong{alert("Your browser
broke!");return false;}
}
}
}
</script>
Catch & try
• In the above Javascript code, we try three times to make our
XMLHttpRequest object. Our first attempt:
• ajaxRequest = new XMLHttpRequest();
• is for the Opera 8.0+, Firefox and Safari browsers. If that fails we try
two more times to make the correct object for an Internet Explorer
browser with:
• ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
• ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");>
• If that doesn't work, then they are using a very outdated browser
that doesn't support XMLHttpRequest, which also means it doesn't
support Ajax.
• Most likely though, our variable ajaxRequest will now be set to
whatever XMLHttpRequest standard the browser uses and we can
start sending data to the server.
Ajax - onreadystatechange Property
• Before we even think about sending data to the server, we
must first write a function that will be able to receive
information. This function will be used to catch the data that
is returned by the server.
• The XMLHttpRequest object has a special property called
onreadystatechange. onreadystatechange stores the function
that will process the response from the server. The following
code defines an empty function and sets the
onreadystatechange property at the same time!
• We will be filling this function in throughout the lesson, as
you learn more about the XMLHttpRequest object.
• Javascript Code:
• // Create a function that will receive data sent from the
server ajaxRequest.onreadystatechange = function()
{ // We still need to write some code here }
Ajax - readyState Property

• The XMLHttpRequest object has another property called readyState. This is
where the status of our server's response is stored. The response can be
processing, downloading or completed. Each time the readyState changes
then our onreadystatechange function executes.
• The only readyState that we are going to care about in this lesson is when
our response is complete and we can get our hands on that information. So
let's add an If Statement to our function to check if the response is
complete.
• Note: When the property readyState is 4 that means the response is
complete and we can get our data.
• Javascript Code:
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function()
{
if(ajaxRequest.readyState == 4)
{ // Get the data from the server's response } }
• Now that we know how to check if the response is complete, we can access
the property that stores the server's response, responseText.
Ajax - responseText Property

• For simple Ajax applications, like this tutorial describes, you can retrieve the
server's response by using the responseText property. Using a little bit of
Javascript and HTML forms we can change our text box to equal
responseText.
• In case you forgot, this tutorial is using Ajax to set an HTML text box to the
server's time. The HTML input we want to change is the "time" text box.
Here's a little refresher on how to access form inputs with Javascript:
• document.FormName.InputName.value
• Our form's name is "myForm" and the text box is "time". Below is the code
that will set our "time" text box to the server's time.
• Javascript Code:
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function()
{
if(ajaxRequest.readyState == 4)
{
document.myForm.time.value = ajaxRequest.responseText;
}
}
•
•
•
•

•
•
•

Ajax - Sending a Request for Information

Now that our onreadystatechange property has a proper response-handling
function, we can send our request. Sending a request is comprised of two steps:
Specify the URL of server-side script that will be used in our Ajax application.
Use the send function to send off the request.
Our simple PHP script, that we have yet to write, will be called "serverTime.php",
so we can already do step 1. The URL is set using the open method, which takes
three arguments. The second argument is the important one, as it is the URL of our
PHP script.
Assuming that the HTML and PHP files are in the same directory, the code would
be:
Javascript Code:
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function()
{

if(ajaxRequest.readyState == 4)
{
document.myForm.time.value = ajaxRequest.responseText;
}

}
ajaxRequest.open("GET", “page.php", true);
ajaxRequest.send(null);
Ajax - Finishing up "order.html"
• Before we plug in our freshly written Javascript code
into the "order.html" file, we need some way for the
visitor to run our Ajax function. It might be kinda cool
if our Ajax did its magic while the user was doing
something on our webpage, so let's have our function
run after the user enters their name.
• Using the onChange attribute, we can make it so our
function is called whenever the user makes a change
to the "username" text box.
• Javascript Code:
<input type='text' onChange="ajaxFunction();"
name='username' /> <br />
• OK, now we are ready to completely update our
"order.html" file to be 100% Ajax ready.
• <html> <body>
<script language="javascript" type="text/javascript">
<!-- //Browser Support Code
function ajaxFunction()
{
var ajaxRequest; // The variable that makes Ajax possible!
try{ // Opera 8.0+, Firefox, Safari ajaxRequest = new
XMLHttpRequest(); }
catch (e){ // Internet Explorer Browsers
try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch
(e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e){ // Something went wrong alert("Your browser broke!");
return false; } } } // Create a function that will receive data sent from
the server ajaxRequest.onreadystatechange = function()
{ if(ajaxRequest.readyState == 4){ document.myForm.time.value =
ajaxRequest.responseText; } } ajaxRequest.open("GET",
"serverTime.php", true); ajaxRequest.send(null); } //--> </script>
<form name='myForm'> Name: <input type='text'
onChange="ajaxFunction();" name='username' /> <br /> Time:
<input type='text' name='time' /> </form> </body> </html>
Ajax
Ajax

More Related Content

What's hot

Ajax
AjaxAjax
Ajax ppt - 32 slides
Ajax ppt - 32 slidesAjax ppt - 32 slides
Ajax ppt - 32 slides
Smithss25
 
AJAX
AJAXAJAX
AJAX
ARJUN
 
25250716 seminar-on-ajax text
25250716 seminar-on-ajax text25250716 seminar-on-ajax text
25250716 seminar-on-ajax text
Kamleshh Chandnani
 
Ajax
AjaxAjax
Learn Ajax here
Learn Ajax hereLearn Ajax here
Learn Ajax here
jarnail
 
Ajax
AjaxAjax
Ajax
AjaxAjax
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
Venkat Pinagadi
 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
Raja V
 
Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)
Adnan Sohail
 
Ajax Overview by Bally Chohan
Ajax Overview by Bally ChohanAjax Overview by Bally Chohan
Ajax Overview by Bally Chohan
WebVineet
 
Ajax control tool kit
Ajax control tool kitAjax control tool kit
Ajax control tool kit
Vidhi Patel
 
Ajax Ppt 1
Ajax Ppt 1Ajax Ppt 1
Ajax Ppt 1
JayaPrakash.m
 
Html web workers
Html web workersHtml web workers
Html web workers
AbhishekMondal42
 
Ajax
AjaxAjax
Introduction To Asp.Net Ajax
Introduction To Asp.Net AjaxIntroduction To Asp.Net Ajax
Introduction To Asp.Net Ajax
Jeff Blankenburg
 
Ajax Presentation
Ajax PresentationAjax Presentation
Ajax Presentation
jrdoane
 
Esri Dev Summit 2009 Rest and Mvc Final
Esri Dev Summit 2009 Rest and Mvc FinalEsri Dev Summit 2009 Rest and Mvc Final
Esri Dev Summit 2009 Rest and Mvc Final
guestcd4688
 
Ajax3
Ajax3Ajax3

What's hot (20)

Ajax
AjaxAjax
Ajax
 
Ajax ppt - 32 slides
Ajax ppt - 32 slidesAjax ppt - 32 slides
Ajax ppt - 32 slides
 
AJAX
AJAXAJAX
AJAX
 
25250716 seminar-on-ajax text
25250716 seminar-on-ajax text25250716 seminar-on-ajax text
25250716 seminar-on-ajax text
 
Ajax
AjaxAjax
Ajax
 
Learn Ajax here
Learn Ajax hereLearn Ajax here
Learn Ajax here
 
Ajax
AjaxAjax
Ajax
 
Ajax
AjaxAjax
Ajax
 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
 
Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)
 
Ajax Overview by Bally Chohan
Ajax Overview by Bally ChohanAjax Overview by Bally Chohan
Ajax Overview by Bally Chohan
 
Ajax control tool kit
Ajax control tool kitAjax control tool kit
Ajax control tool kit
 
Ajax Ppt 1
Ajax Ppt 1Ajax Ppt 1
Ajax Ppt 1
 
Html web workers
Html web workersHtml web workers
Html web workers
 
Ajax
AjaxAjax
Ajax
 
Introduction To Asp.Net Ajax
Introduction To Asp.Net AjaxIntroduction To Asp.Net Ajax
Introduction To Asp.Net Ajax
 
Ajax Presentation
Ajax PresentationAjax Presentation
Ajax Presentation
 
Esri Dev Summit 2009 Rest and Mvc Final
Esri Dev Summit 2009 Rest and Mvc FinalEsri Dev Summit 2009 Rest and Mvc Final
Esri Dev Summit 2009 Rest and Mvc Final
 
Ajax3
Ajax3Ajax3
Ajax3
 

Viewers also liked

寻找未见的澳大利亚
寻找未见的澳大利亚寻找未见的澳大利亚
寻找未见的澳大利亚
OZOZ01
 
Discapacitado
DiscapacitadoDiscapacitado
Discapacitado
COMPU-EPP S.C.
 
Menu general junio 2015 - cocinas - sin cerdo
Menu general   junio 2015 - cocinas - sin cerdoMenu general   junio 2015 - cocinas - sin cerdo
Menu general junio 2015 - cocinas - sin cerdo
lolosan10
 
The Polar Continental Shelf Project (PCSP) or Polar shelf
The Polar Continental Shelf Project (PCSP) or Polar shelfThe Polar Continental Shelf Project (PCSP) or Polar shelf
The Polar Continental Shelf Project (PCSP) or Polar shelf
dstongeoc
 
Regale
RegaleRegale
Raices firmes en el amor de Dios
Raices firmes en el amor de DiosRaices firmes en el amor de Dios
Raices firmes en el amor de Dios
Estado
 
Mitología japonesa
Mitología japonesaMitología japonesa
Mitología japonesa
8837
 
Textual analysis 3
Textual analysis 3Textual analysis 3
Textual analysis 3
bryonywoolley
 
Equipment list
Equipment list Equipment list
Equipment list
JulianaMariz
 
Marketing Medical Billing Services to Physician Practices
Marketing Medical Billing Services to Physician PracticesMarketing Medical Billing Services to Physician Practices
Marketing Medical Billing Services to Physician Practices
John Mazza
 
Delivering Projects the Pivotal Way
Delivering Projects the Pivotal WayDelivering Projects the Pivotal Way
Delivering Projects the Pivotal Way
Aaron Severs
 
1.a pratica circuitos lógicos com fpga
1.a pratica circuitos lógicos com fpga1.a pratica circuitos lógicos com fpga
1.a pratica circuitos lógicos com fpga
Leonardo Borges
 
Fgxpress apresentação fácil de entender - copia (7)
Fgxpress   apresentação fácil de entender - copia (7)Fgxpress   apresentação fácil de entender - copia (7)
Fgxpress apresentação fácil de entender - copia (7)
Bdrag Wake
 

Viewers also liked (13)

寻找未见的澳大利亚
寻找未见的澳大利亚寻找未见的澳大利亚
寻找未见的澳大利亚
 
Discapacitado
DiscapacitadoDiscapacitado
Discapacitado
 
Menu general junio 2015 - cocinas - sin cerdo
Menu general   junio 2015 - cocinas - sin cerdoMenu general   junio 2015 - cocinas - sin cerdo
Menu general junio 2015 - cocinas - sin cerdo
 
The Polar Continental Shelf Project (PCSP) or Polar shelf
The Polar Continental Shelf Project (PCSP) or Polar shelfThe Polar Continental Shelf Project (PCSP) or Polar shelf
The Polar Continental Shelf Project (PCSP) or Polar shelf
 
Regale
RegaleRegale
Regale
 
Raices firmes en el amor de Dios
Raices firmes en el amor de DiosRaices firmes en el amor de Dios
Raices firmes en el amor de Dios
 
Mitología japonesa
Mitología japonesaMitología japonesa
Mitología japonesa
 
Textual analysis 3
Textual analysis 3Textual analysis 3
Textual analysis 3
 
Equipment list
Equipment list Equipment list
Equipment list
 
Marketing Medical Billing Services to Physician Practices
Marketing Medical Billing Services to Physician PracticesMarketing Medical Billing Services to Physician Practices
Marketing Medical Billing Services to Physician Practices
 
Delivering Projects the Pivotal Way
Delivering Projects the Pivotal WayDelivering Projects the Pivotal Way
Delivering Projects the Pivotal Way
 
1.a pratica circuitos lógicos com fpga
1.a pratica circuitos lógicos com fpga1.a pratica circuitos lógicos com fpga
1.a pratica circuitos lógicos com fpga
 
Fgxpress apresentação fácil de entender - copia (7)
Fgxpress   apresentação fácil de entender - copia (7)Fgxpress   apresentação fácil de entender - copia (7)
Fgxpress apresentação fácil de entender - copia (7)
 

Similar to Ajax

Ajax
AjaxAjax
M Ramya
M RamyaM Ramya
Ajax
AjaxAjax
Ajax
AjaxAjax
Ajax
AjaxAjax
Ajax
AjaxAjax
ajax_pdf
ajax_pdfajax_pdf
ajax_pdf
tutorialsruby
 
ajax_pdf
ajax_pdfajax_pdf
ajax_pdf
tutorialsruby
 
Ajax
AjaxAjax
Copy of ajax tutorial
Copy of ajax tutorialCopy of ajax tutorial
Copy of ajax tutorial
Abhishek Kesharwani
 
How to Use AJAX in PHP and jQuery.pdf
How to Use AJAX in PHP and jQuery.pdfHow to Use AJAX in PHP and jQuery.pdf
How to Use AJAX in PHP and jQuery.pdf
semsem20021
 
AJAX.pptx
AJAX.pptxAJAX.pptx
AJAX.pptx
Ganesh Chavan
 
Ajax
AjaxAjax
Ajax.ppt
Ajax.pptAjax.ppt
Ajax presentation
Ajax presentationAjax presentation
Ajax presentation
engcs2008
 
mukesh
mukeshmukesh
mukesh
guest06dc4b2
 
Ajax
AjaxAjax
Ajax
AjaxAjax
Ajax Introduction
Ajax IntroductionAjax Introduction
Ajax Introduction
Oliver Cai
 
Ajax tutorial by bally chohan
Ajax tutorial by bally chohanAjax tutorial by bally chohan
Ajax tutorial by bally chohan
WebVineet
 

Similar to Ajax (20)

Ajax
AjaxAjax
Ajax
 
M Ramya
M RamyaM Ramya
M Ramya
 
Ajax
AjaxAjax
Ajax
 
Ajax
AjaxAjax
Ajax
 
Ajax
AjaxAjax
Ajax
 
Ajax
AjaxAjax
Ajax
 
ajax_pdf
ajax_pdfajax_pdf
ajax_pdf
 
ajax_pdf
ajax_pdfajax_pdf
ajax_pdf
 
Ajax
AjaxAjax
Ajax
 
Copy of ajax tutorial
Copy of ajax tutorialCopy of ajax tutorial
Copy of ajax tutorial
 
How to Use AJAX in PHP and jQuery.pdf
How to Use AJAX in PHP and jQuery.pdfHow to Use AJAX in PHP and jQuery.pdf
How to Use AJAX in PHP and jQuery.pdf
 
AJAX.pptx
AJAX.pptxAJAX.pptx
AJAX.pptx
 
Ajax
AjaxAjax
Ajax
 
Ajax.ppt
Ajax.pptAjax.ppt
Ajax.ppt
 
Ajax presentation
Ajax presentationAjax presentation
Ajax presentation
 
mukesh
mukeshmukesh
mukesh
 
Ajax
AjaxAjax
Ajax
 
Ajax
AjaxAjax
Ajax
 
Ajax Introduction
Ajax IntroductionAjax Introduction
Ajax Introduction
 
Ajax tutorial by bally chohan
Ajax tutorial by bally chohanAjax tutorial by bally chohan
Ajax tutorial by bally chohan
 

Recently uploaded

20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 

Recently uploaded (20)

20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 

Ajax

  • 1. Ajax Ajax is a catchy name for a type of programming made popular in 2005 by Google and other big web developers. Ajax loosely stands for Asynchronous Javascript And XML, but that just sounds like techno jargon to many people. Pretty much. By using the programming practice termed "Ajax" you will be able to trade data, with a web server, without having to load a new page. Instead of Ajax being seen as "The New Way to Develop Websites", it should instead be seen as another weapon to add to your programming arsenal. This tutorial will introduce you to the basics of Ajax and show you how to send and receive data from a server without using a "Submit" button approach.
  • 2. Ajax - Creating an HTML Form • Before we can start getting to the exciting new stuff, we must first make a standard HTML form (no submit button though!). This form will be spiced up in later with a hint of Ajax, but for now let's just make a solid, basic HTML form with a couple inputs. <html> <body> <form name='myForm'> Name: <input type='text' name='username' /> <br /> Time: <input type='text' name='time' /> </form> </body> </html>
  • 3. Ajax - Where's the Submit Button? • That's the great thing about Ajax, you do not need a form submit button to send the user's data to the server. We are going to be using our "Javascript on Steroids" to get and submit data with the server. • Now that we have our HTML form, we can dive deeper into the Ajax jungle and try to discover what we're facing.
  • 4. Ajax - Browser Support • This lesson includes one of the largest hurdles for aspiring Ajax programmers: browser support. It would be nice if all the web browsers required the same Javascript code to use Ajax, but life isn't fair and you've got your work cut out for you!
  • 5. Ajax - Try/Catch Blocks of Code • To create this important Ajax object, you are going to have to use a special programming technique known as "try and catch". Basically it attempts to "try" a piece of code and if that piece causes an error it "catches" the error and keeps going. Normally when an error occurs the code will stop running, however, the "catch" eats up the error and lets the code continue. • In the following code we are going to "try" three different ways to make a new XMLHttpRequest object. Every time we fail and get an error, we will catch the error and try the next a different command. • Note: If our "try" is successful then the "catch" code will not be run because it is only used when there is an error.
  • 6. Catch & try <script language="javascript" type="text/javascript"> <!-- //Browser Support Code function ajaxFunction() { var ajaxRequest; // The variable that makes Ajax possible! try{ajaxRequest = new XMLHttpRequest(); // Opera 8.0+, Firefox, Safari } catch (e) {// Internet Explorer Browsers try{ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");} catch (e){// Something went wrong{alert("Your browser broke!");return false;} } } } </script>
  • 7. Catch & try • In the above Javascript code, we try three times to make our XMLHttpRequest object. Our first attempt: • ajaxRequest = new XMLHttpRequest(); • is for the Opera 8.0+, Firefox and Safari browsers. If that fails we try two more times to make the correct object for an Internet Explorer browser with: • ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); • ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");> • If that doesn't work, then they are using a very outdated browser that doesn't support XMLHttpRequest, which also means it doesn't support Ajax. • Most likely though, our variable ajaxRequest will now be set to whatever XMLHttpRequest standard the browser uses and we can start sending data to the server.
  • 8. Ajax - onreadystatechange Property • Before we even think about sending data to the server, we must first write a function that will be able to receive information. This function will be used to catch the data that is returned by the server. • The XMLHttpRequest object has a special property called onreadystatechange. onreadystatechange stores the function that will process the response from the server. The following code defines an empty function and sets the onreadystatechange property at the same time! • We will be filling this function in throughout the lesson, as you learn more about the XMLHttpRequest object. • Javascript Code: • // Create a function that will receive data sent from the server ajaxRequest.onreadystatechange = function() { // We still need to write some code here }
  • 9. Ajax - readyState Property • The XMLHttpRequest object has another property called readyState. This is where the status of our server's response is stored. The response can be processing, downloading or completed. Each time the readyState changes then our onreadystatechange function executes. • The only readyState that we are going to care about in this lesson is when our response is complete and we can get our hands on that information. So let's add an If Statement to our function to check if the response is complete. • Note: When the property readyState is 4 that means the response is complete and we can get our data. • Javascript Code: // Create a function that will receive data sent from the server ajaxRequest.onreadystatechange = function() { if(ajaxRequest.readyState == 4) { // Get the data from the server's response } } • Now that we know how to check if the response is complete, we can access the property that stores the server's response, responseText.
  • 10. Ajax - responseText Property • For simple Ajax applications, like this tutorial describes, you can retrieve the server's response by using the responseText property. Using a little bit of Javascript and HTML forms we can change our text box to equal responseText. • In case you forgot, this tutorial is using Ajax to set an HTML text box to the server's time. The HTML input we want to change is the "time" text box. Here's a little refresher on how to access form inputs with Javascript: • document.FormName.InputName.value • Our form's name is "myForm" and the text box is "time". Below is the code that will set our "time" text box to the server's time. • Javascript Code: // Create a function that will receive data sent from the server ajaxRequest.onreadystatechange = function() { if(ajaxRequest.readyState == 4) { document.myForm.time.value = ajaxRequest.responseText; } }
  • 11. • • • • • • • Ajax - Sending a Request for Information Now that our onreadystatechange property has a proper response-handling function, we can send our request. Sending a request is comprised of two steps: Specify the URL of server-side script that will be used in our Ajax application. Use the send function to send off the request. Our simple PHP script, that we have yet to write, will be called "serverTime.php", so we can already do step 1. The URL is set using the open method, which takes three arguments. The second argument is the important one, as it is the URL of our PHP script. Assuming that the HTML and PHP files are in the same directory, the code would be: Javascript Code: // Create a function that will receive data sent from the server ajaxRequest.onreadystatechange = function() { if(ajaxRequest.readyState == 4) { document.myForm.time.value = ajaxRequest.responseText; } } ajaxRequest.open("GET", “page.php", true); ajaxRequest.send(null);
  • 12. Ajax - Finishing up "order.html" • Before we plug in our freshly written Javascript code into the "order.html" file, we need some way for the visitor to run our Ajax function. It might be kinda cool if our Ajax did its magic while the user was doing something on our webpage, so let's have our function run after the user enters their name. • Using the onChange attribute, we can make it so our function is called whenever the user makes a change to the "username" text box. • Javascript Code: <input type='text' onChange="ajaxFunction();" name='username' /> <br /> • OK, now we are ready to completely update our "order.html" file to be 100% Ajax ready.
  • 13. • <html> <body> <script language="javascript" type="text/javascript"> <!-- //Browser Support Code function ajaxFunction() { var ajaxRequest; // The variable that makes Ajax possible! try{ // Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest(); } catch (e){ // Internet Explorer Browsers try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ // Something went wrong alert("Your browser broke!"); return false; } } } // Create a function that will receive data sent from the server ajaxRequest.onreadystatechange = function() { if(ajaxRequest.readyState == 4){ document.myForm.time.value = ajaxRequest.responseText; } } ajaxRequest.open("GET", "serverTime.php", true); ajaxRequest.send(null); } //--> </script> <form name='myForm'> Name: <input type='text' onChange="ajaxFunction();" name='username' /> <br /> Time: <input type='text' name='time' /> </form> </body> </html>