SlideShare a Scribd company logo
Matthew Batchelder
Agenda JavaScript: What it is and isn't JavaScript: Uses What is the DOM? What is AJAX? jQuery FTW! Manipulating page elements (the DOM) in sweet ways Simplified AJAX Other Coolness Pluggability jQuery in myPlymouth
Before We Start! Important tools to have Use Firefox  Firebug JS Debugging FTW Web Developer Toolbar (handy) A sexy text editor (not notepad)
JS: What it is and isn’t NOT  Java despite its name ECMAScript More than form validation Client-Side  Scripting Language Dynamic Weakly Typed Object-Oriented (Prototype-Based) DOM Event Tool
JavaScript Sandbox Scripts run in a “sandbox” Can only perform web-related actions, not File-System actions Constrained by a “Same Origin Policy”
JS: Usage Drop this puppy in your page:  <html> <head> <title>Example JS Page</title> <script type=“text/javascript”> // javascript code goes here </script> </head> <body> … </body> </html>
JS: Literals Values (the stuff on the right of the equal sign) are literals. <script type=“text/javascript”> var myNumber = 123; var myString = ‘Bork!’; var myBoolean = true; var myFunction = function(){ return ‘hello’;} var myRegExp = /bork/gi; var myArray = [1, 2, 3]; var myCarObject = { color: ‘red’, tires: 4, windows: 6 } </script>
JS: Objects Everything in JS is an Object All literals are  object  literals . Those literals can be written: <script type=“text/javascript”> var myNumber = new Number(123); var myString = new String(‘Bork!’); var myBoolean = new Boolean(true); var myFunction = new Function(‘’, “return ‘hello’”);} var myRegExp = new RegExp(‘bork’); var myArray = new Array(); myArray[0] = 1; myArray[1] = 2; myArray[2] = 3; var myCarObject = new Object(); myCarObject.color = ‘red’; myCarObject.tires = 4; myCarObject.windows = 6; </script>
JS: Objects Objects values are accessed using dot (“.”) notation: example <html> <head> <title>Examples</title> <script type=&quot;text/javascript&quot;> var bork = 'Bork!'; var w00t = { hello: 'Greetings', yo: function(){ alert(bork + ' ' + this.hello); } }; var zomg = { nested: { iMeanReallyNested: { seriously: { out: function(){ alert('whee!'); } } } } }; w00t.yo(); zomg.nested.iMeanReallyNested.seriously.out(); </script> </head> <body> ... </body> </html>
JS: Control Structures if (bork) { //... }  else  { //... } while (bork) { //... } for (var i = 0; i< 10; i++) { //... } for (var element  in  array_of_elements) { //... } do  { //... }  while (bork); switch (bork) { case  1: // if bork == 1... case  'whee': // if bork == 'whee'... case  false: // if bork == false... default : // otherwise ... } try  { //... }  catch (err) { //... }
What is the DOM? DOM == Document Object Model The DOM is  hierarchical <html> <head> <title>Example JS Page</title> </head> <body> <form id=“some_form”> <input type=“text” name=“bork”/> <input type=“submit” value=“Go”/> </form> </body> </html>
Finding DOM Elements document.getElementById() returns a specific element document.getElementByTag() returns an array of elements
DOM Element Attributes nodeName nodeValue nodeType parentNode childNodes firstChild lastChild previousSibling nextSibling attributes ownerDocument 1 = an HTML element 2 = an element attribute 3 = text 8 = an HTML comment 9 = a document 10 = a document type definition DOM Attributes Node Types Here’s a  good article  that uses these.
Manipulating the DOM Dynamically creating and adding elements document.createElement appendChild example
innerHTML Why go through the trouble of creating Nodes? More efficient Easier example
Events Click Dblclick Mousedown Mouseup Mouseover Mousemove Mouseout Keypress Keydown Keyup Select Change Submit Reset Focus Blur Load Unload  Abort Error Resize Scroll Mouse Keyboard Frame/Object Form
Simple Alert Box <html> <head> <title>Example Message Box Page</title> <script type=“text/javascript”> alert(‘I like butter’); </script> </head> <body> … </body> </html>
Confirm Box Bound to an Event <html> <head> <title>Example Message Box Page</title> <script type=&quot;text/javascript&quot;> function doLoad() { document.getElementById('sweet-link').addEventListener(‘click’, confirmClick, false); }//end doLoad function confirmClick() { return confirm(‘Are you sure you wish to go to that link?’); }//end confirmClick window.addEventListener(‘load’, doLoad, false); </script> </head> <body> <a id=&quot;sweet-link&quot; href=&quot;http://borkweb.com&quot;>BorkWeb</a> </body> </html> example
Hiding/Displaying Elements Element visibility is a nice use of events and DOM manipulation example
AJAX AJAX  (Asychronous Javascript and XML) Gives you the ability to load content  dynamically ! Loading content on demand Possible usability Issues Possible performance  problems  and  benefits Limitation : No AJAX calls beyond the sandbox. Note: The way around this is with  XSS  (Cross Site Scripting)…which can be dangerous if done poorly.
Ajax: XMLHttpRequest Loading content on demand: <script type=&quot;text/javascript&quot;> function ajax(url, vars, callbackFunction){  var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject(&quot;MSXML2.XMLHTTP.3.0&quot;);  request.open(&quot;GET&quot;, url, true); request.setRequestHeader(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);  request.onreadystatechange = function(){ if (request.readyState == 4 && request.status == 200){ if (request.responseText){ callbackFunction(request.responseText); } } }; request.send(vars); }//end ajax function out(text){ document.getElementById('content').innerHTML = text; }//end out function ajaxCall(){ ajax('http://borkweb.com/examples/js_workshop/dynamic_content1.html','',out);return false; }//end ajaxCall  function doLoad(){ document.getElementById('sweet-link').addEventListener('click', ajaxCall, false); }//doLoad  window.addEventListener('load', doLoad, false);  </script> example
Things can actually be a bit easier. How much?  Well most of the above.
jQuery.  That’s what we use on campus.  It is awesome.
What is jQuery? jQuery is a sweet JavaScript Library Its Mantra:  Find stuff and do stuff with it Focuses on simplicity Get it  here Check out the  docs
Finding Elements Say goodbye to document.getElementById() and document.getElementByTag() Say hello to: $() Uses  CSS Selectors  to find elements and returns them as an array of elements.
Finding Elements With $ $(‘a’) $(‘.class’) $(‘#id’) $(‘.content div’) $(‘input[name=bork]’) $(‘input:first’) Here’s an  example . Check out the selector syntax for more info.
Lets do some of the stuff we already did… Adding Text Fields Toggling Element Visibility Ajax Content
jQuery Coolness Browser data $.browser Effects Sliding Fading Animating Chaining $(‘a’).click(function(){alert(‘hello’); return false;}).css(‘font-weight’,’bold’).fadeOut(‘slow’);
jQuery Plugins Pluggable!  Additional jQuery functionality added by including  jQuery plugins .
jQuery in myPlymouth Go Sidebar Bookmarks Tab Stack Etc… Check out the  source .
Resources:  Slide Examples ,  jQuery ,  Image Sprites ,  Mega Man !

More Related Content

PPT
JavaScript: Ajax & DOM Manipulation
borkweb
 
PPT
KMUTNB - Internet Programming 4/7
phuphax
 
PDF
Interacting with the DOM (JavaScript)
Florence Davis
 
PPT
JavaScript & Dom Manipulation
Mohammed Arif
 
PPT
JavaScript
Doncho Minkov
 
PDF
22 j query1
Fajar Baskoro
 
PPTX
Java script
Sukrit Gupta
 
PPTX
Javascript inside Browser (DOM)
Vlad Mysla
 
JavaScript: Ajax & DOM Manipulation
borkweb
 
KMUTNB - Internet Programming 4/7
phuphax
 
Interacting with the DOM (JavaScript)
Florence Davis
 
JavaScript & Dom Manipulation
Mohammed Arif
 
JavaScript
Doncho Minkov
 
22 j query1
Fajar Baskoro
 
Java script
Sukrit Gupta
 
Javascript inside Browser (DOM)
Vlad Mysla
 

What's hot (20)

PPT
Introduction to Prototype JS Framework
Mohd Imran
 
PPTX
JavaScript and jQuery Basics
Kaloyan Kosev
 
PPTX
jQuery PPT
Dominic Arrojado
 
PPTX
jQuery Presentation
Rod Johnson
 
PDF
Devoxx 2014-webComponents
Cyril Balit
 
PDF
JavaScript DOM & event
Borey Lim
 
PPT
J Query Public
pradeepsilamkoti
 
PPTX
Introduction to jQuery
Gunjan Kumar
 
PPT
jQuery
Mohammed Arif
 
PPT
Learn javascript easy steps
prince Loffar
 
PPTX
Html, CSS, Javascript, Jquery, Meteor應用
LearningTech
 
PPT
jQuery
Mostafa Bayomi
 
PPTX
jQuery
Dileep Mishra
 
PPTX
Java Script
Dr. SURBHI SAROHA
 
PPTX
jQuery from the very beginning
Anis Ahmad
 
PPTX
FYBSC IT Web Programming Unit III Document Object
Arti Parab Academics
 
PPTX
Internet and Web Technology (CLASS-9) [React.js] | NIC/NIELIT Web Technology
Ayes Chinmay
 
PPT
Java script
vishal choudhary
 
PPTX
1. java script language fundamentals
Rajiv Gupta
 
PPTX
Introduction to java_script
Basavaraj Hampali
 
Introduction to Prototype JS Framework
Mohd Imran
 
JavaScript and jQuery Basics
Kaloyan Kosev
 
jQuery PPT
Dominic Arrojado
 
jQuery Presentation
Rod Johnson
 
Devoxx 2014-webComponents
Cyril Balit
 
JavaScript DOM & event
Borey Lim
 
J Query Public
pradeepsilamkoti
 
Introduction to jQuery
Gunjan Kumar
 
Learn javascript easy steps
prince Loffar
 
Html, CSS, Javascript, Jquery, Meteor應用
LearningTech
 
Java Script
Dr. SURBHI SAROHA
 
jQuery from the very beginning
Anis Ahmad
 
FYBSC IT Web Programming Unit III Document Object
Arti Parab Academics
 
Internet and Web Technology (CLASS-9) [React.js] | NIC/NIELIT Web Technology
Ayes Chinmay
 
Java script
vishal choudhary
 
1. java script language fundamentals
Rajiv Gupta
 
Introduction to java_script
Basavaraj Hampali
 
Ad

Viewers also liked (6)

PDF
JavaScript Basics And DOM Manipulation
Siarhei Barysiuk
 
PPTX
Document Object Model
Mayur Mudgal
 
PPT
DOM ( Document Object Model )
ITSTB
 
PPTX
Document object model(dom)
rahul kundu
 
PPT
Document Object Model
chomas kandar
 
JavaScript Basics And DOM Manipulation
Siarhei Barysiuk
 
Document Object Model
Mayur Mudgal
 
DOM ( Document Object Model )
ITSTB
 
Document object model(dom)
rahul kundu
 
Document Object Model
chomas kandar
 
Ad

Similar to Javascript: Ajax & DOM Manipulation v1.2 (20)

ODP
Developing and testing ajax components
Ignacio Coloma
 
PPT
Java Script
siddaram
 
PPT
Javascript 2009
borkweb
 
PPT
Javascript
mussawir20
 
PPT
Ajax
Pranay Rana
 
PPT
Ajax
Pranay Rana
 
PPT
Ajaxppt
Ratna Prashanth
 
PPT
Ajaxppt
vsnmurthy
 
PPT
AJAX Workshop Notes
Pamela Fox
 
PPT
Ajax
wangjiaz
 
PPT
Introduction to JQuery
MobME Technical
 
PPT
Ajax Ppt
Hema Prasanth
 
PPT
Ajax ppt
Sanmuga Nathan
 
PPT
jQuery introduction
Tomi Juhola
 
PPT
Vb.Net Web Forms
Dutch Dasanaike {LION}
 
ODP
ActiveWeb: Chicago Java User Group Presentation
ipolevoy
 
PPT
JWU Guest Talk: JavaScript and AJAX
Hilary Mason
 
PPT
Javascript Templating
bcruhl
 
PPTX
Microsoft and jQuery
Eric ShangKuan
 
PPT
my test
lu jimmy
 
Developing and testing ajax components
Ignacio Coloma
 
Java Script
siddaram
 
Javascript 2009
borkweb
 
Javascript
mussawir20
 
Ajaxppt
vsnmurthy
 
AJAX Workshop Notes
Pamela Fox
 
Ajax
wangjiaz
 
Introduction to JQuery
MobME Technical
 
Ajax Ppt
Hema Prasanth
 
Ajax ppt
Sanmuga Nathan
 
jQuery introduction
Tomi Juhola
 
Vb.Net Web Forms
Dutch Dasanaike {LION}
 
ActiveWeb: Chicago Java User Group Presentation
ipolevoy
 
JWU Guest Talk: JavaScript and AJAX
Hilary Mason
 
Javascript Templating
bcruhl
 
Microsoft and jQuery
Eric ShangKuan
 
my test
lu jimmy
 

Recently uploaded (20)

PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
AVTRON Technologies LLC
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
AbdullahSani29
 
PDF
NewMind AI Monthly Chronicles - July 2025
NewMind AI
 
DOCX
Top AI API Alternatives to OpenAI: A Side-by-Side Breakdown
vilush
 
PDF
Test Bank, Solutions for Java How to Program, An Objects-Natural Approach, 12...
famaw19526
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
PDF
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
CIFDAQ
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
agentic-ai-and-the-future-of-autonomous-systems.pdf
siddharthnetsavvies
 
PDF
Shreyas_Phanse_Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
SHREYAS PHANSE
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PPTX
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
AVTRON Technologies LLC
 
This slide provides an overview Technology
mineshkharadi333
 
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
AbdullahSani29
 
NewMind AI Monthly Chronicles - July 2025
NewMind AI
 
Top AI API Alternatives to OpenAI: A Side-by-Side Breakdown
vilush
 
Test Bank, Solutions for Java How to Program, An Objects-Natural Approach, 12...
famaw19526
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
CIFDAQ
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
agentic-ai-and-the-future-of-autonomous-systems.pdf
siddharthnetsavvies
 
Shreyas_Phanse_Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
SHREYAS PHANSE
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 

Javascript: Ajax & DOM Manipulation v1.2

  • 2. Agenda JavaScript: What it is and isn't JavaScript: Uses What is the DOM? What is AJAX? jQuery FTW! Manipulating page elements (the DOM) in sweet ways Simplified AJAX Other Coolness Pluggability jQuery in myPlymouth
  • 3. Before We Start! Important tools to have Use Firefox Firebug JS Debugging FTW Web Developer Toolbar (handy) A sexy text editor (not notepad)
  • 4. JS: What it is and isn’t NOT Java despite its name ECMAScript More than form validation Client-Side Scripting Language Dynamic Weakly Typed Object-Oriented (Prototype-Based) DOM Event Tool
  • 5. JavaScript Sandbox Scripts run in a “sandbox” Can only perform web-related actions, not File-System actions Constrained by a “Same Origin Policy”
  • 6. JS: Usage Drop this puppy in your page: <html> <head> <title>Example JS Page</title> <script type=“text/javascript”> // javascript code goes here </script> </head> <body> … </body> </html>
  • 7. JS: Literals Values (the stuff on the right of the equal sign) are literals. <script type=“text/javascript”> var myNumber = 123; var myString = ‘Bork!’; var myBoolean = true; var myFunction = function(){ return ‘hello’;} var myRegExp = /bork/gi; var myArray = [1, 2, 3]; var myCarObject = { color: ‘red’, tires: 4, windows: 6 } </script>
  • 8. JS: Objects Everything in JS is an Object All literals are object literals . Those literals can be written: <script type=“text/javascript”> var myNumber = new Number(123); var myString = new String(‘Bork!’); var myBoolean = new Boolean(true); var myFunction = new Function(‘’, “return ‘hello’”);} var myRegExp = new RegExp(‘bork’); var myArray = new Array(); myArray[0] = 1; myArray[1] = 2; myArray[2] = 3; var myCarObject = new Object(); myCarObject.color = ‘red’; myCarObject.tires = 4; myCarObject.windows = 6; </script>
  • 9. JS: Objects Objects values are accessed using dot (“.”) notation: example <html> <head> <title>Examples</title> <script type=&quot;text/javascript&quot;> var bork = 'Bork!'; var w00t = { hello: 'Greetings', yo: function(){ alert(bork + ' ' + this.hello); } }; var zomg = { nested: { iMeanReallyNested: { seriously: { out: function(){ alert('whee!'); } } } } }; w00t.yo(); zomg.nested.iMeanReallyNested.seriously.out(); </script> </head> <body> ... </body> </html>
  • 10. JS: Control Structures if (bork) { //... } else { //... } while (bork) { //... } for (var i = 0; i< 10; i++) { //... } for (var element in array_of_elements) { //... } do { //... } while (bork); switch (bork) { case 1: // if bork == 1... case 'whee': // if bork == 'whee'... case false: // if bork == false... default : // otherwise ... } try { //... } catch (err) { //... }
  • 11. What is the DOM? DOM == Document Object Model The DOM is hierarchical <html> <head> <title>Example JS Page</title> </head> <body> <form id=“some_form”> <input type=“text” name=“bork”/> <input type=“submit” value=“Go”/> </form> </body> </html>
  • 12. Finding DOM Elements document.getElementById() returns a specific element document.getElementByTag() returns an array of elements
  • 13. DOM Element Attributes nodeName nodeValue nodeType parentNode childNodes firstChild lastChild previousSibling nextSibling attributes ownerDocument 1 = an HTML element 2 = an element attribute 3 = text 8 = an HTML comment 9 = a document 10 = a document type definition DOM Attributes Node Types Here’s a good article that uses these.
  • 14. Manipulating the DOM Dynamically creating and adding elements document.createElement appendChild example
  • 15. innerHTML Why go through the trouble of creating Nodes? More efficient Easier example
  • 16. Events Click Dblclick Mousedown Mouseup Mouseover Mousemove Mouseout Keypress Keydown Keyup Select Change Submit Reset Focus Blur Load Unload Abort Error Resize Scroll Mouse Keyboard Frame/Object Form
  • 17. Simple Alert Box <html> <head> <title>Example Message Box Page</title> <script type=“text/javascript”> alert(‘I like butter’); </script> </head> <body> … </body> </html>
  • 18. Confirm Box Bound to an Event <html> <head> <title>Example Message Box Page</title> <script type=&quot;text/javascript&quot;> function doLoad() { document.getElementById('sweet-link').addEventListener(‘click’, confirmClick, false); }//end doLoad function confirmClick() { return confirm(‘Are you sure you wish to go to that link?’); }//end confirmClick window.addEventListener(‘load’, doLoad, false); </script> </head> <body> <a id=&quot;sweet-link&quot; href=&quot;http://borkweb.com&quot;>BorkWeb</a> </body> </html> example
  • 19. Hiding/Displaying Elements Element visibility is a nice use of events and DOM manipulation example
  • 20. AJAX AJAX (Asychronous Javascript and XML) Gives you the ability to load content dynamically ! Loading content on demand Possible usability Issues Possible performance problems and benefits Limitation : No AJAX calls beyond the sandbox. Note: The way around this is with XSS (Cross Site Scripting)…which can be dangerous if done poorly.
  • 21. Ajax: XMLHttpRequest Loading content on demand: <script type=&quot;text/javascript&quot;> function ajax(url, vars, callbackFunction){ var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject(&quot;MSXML2.XMLHTTP.3.0&quot;); request.open(&quot;GET&quot;, url, true); request.setRequestHeader(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;); request.onreadystatechange = function(){ if (request.readyState == 4 && request.status == 200){ if (request.responseText){ callbackFunction(request.responseText); } } }; request.send(vars); }//end ajax function out(text){ document.getElementById('content').innerHTML = text; }//end out function ajaxCall(){ ajax('http://borkweb.com/examples/js_workshop/dynamic_content1.html','',out);return false; }//end ajaxCall function doLoad(){ document.getElementById('sweet-link').addEventListener('click', ajaxCall, false); }//doLoad window.addEventListener('load', doLoad, false); </script> example
  • 22. Things can actually be a bit easier. How much? Well most of the above.
  • 23. jQuery. That’s what we use on campus. It is awesome.
  • 24. What is jQuery? jQuery is a sweet JavaScript Library Its Mantra: Find stuff and do stuff with it Focuses on simplicity Get it here Check out the docs
  • 25. Finding Elements Say goodbye to document.getElementById() and document.getElementByTag() Say hello to: $() Uses CSS Selectors to find elements and returns them as an array of elements.
  • 26. Finding Elements With $ $(‘a’) $(‘.class’) $(‘#id’) $(‘.content div’) $(‘input[name=bork]’) $(‘input:first’) Here’s an example . Check out the selector syntax for more info.
  • 27. Lets do some of the stuff we already did… Adding Text Fields Toggling Element Visibility Ajax Content
  • 28. jQuery Coolness Browser data $.browser Effects Sliding Fading Animating Chaining $(‘a’).click(function(){alert(‘hello’); return false;}).css(‘font-weight’,’bold’).fadeOut(‘slow’);
  • 29. jQuery Plugins Pluggable! Additional jQuery functionality added by including jQuery plugins .
  • 30. jQuery in myPlymouth Go Sidebar Bookmarks Tab Stack Etc… Check out the source .
  • 31. Resources: Slide Examples , jQuery , Image Sprites , Mega Man !