SlideShare a Scribd company logo
JavaScript
What is JavaScript?
• Client side interpreted embedded programming language
used to enhance websites
• No relation to Java
• Can manipulate HTML
• Event driven
To use or not to use
• Helpful for:
o Dynamic content
o Adding logic to HTML
o Make changes without refreshing the page
o Form validation
o Ease processing on server
• Not helpful for:
o Accessing resources
o Do anything that requires privacy
 JavaScript is shown publicly
Adding JavaScript to HTML
• Inline:
o <script language="javascript"> code </script>
• External:
o <script language="javascript" src="code.js" />
• Typically this is placed in between the <head> tags
• Case sensitive (unlike HTML)
Variables
• Variables are untyped
o No need to declare type (int, bool, etc)
o Doesn't care about type until called
• Example:
o var name = "Andrew";
o var num = 24;
o var name = num;
o name will now return 24
Arrays
• Strings are arrays
o var x = "Andrew"; x[3] returns "r"
• Array is also it's own data structure and doesn't require
all items to be of the same type.
o var x = new Array();
 x[0] = "Rawr";
 x[1] = 9001;
o var x = new Array("Rawr", 9001);
o var x = ["Rawr", 9001];
• Arrays have a length:
o x.length
If..Else / Switch
• Similar to what you'd expect
o if (<condition>) { code }
o if (<condition>) { code } else { more }
o if (<condition>) { code } else if { more } else { finally }
switch (<variable>) {
case <match>:
code
break;
case <match2>:
break;
default:
}
Loops
• For:
o for (<var>; <condition>; <do>) { code }
o for (<var> in <list>) { code }
• While:
o while (<condition>) { code }
o do { code } while (<condition>);
• break and continue work as expected
Functions
• Similar to any other languages' functions and methods
• Can have nested functions
o Variables have scope similar to nested loops
• Used by events
o Runs only when called
• Example:
o function <name> (<parameters>) { code }
Popups
• Used for displaying information to the user outside of
the page.
o Typically as a warning or when an error has occured
o Ask user for additional information
o Confirmation
• alert(<text>); - Exits via an okay button
• confirm(<text>); - Returns true or false (ok or cancel)
• prompt(<text>, <default>); - Returns user's input
Try it
Create your own html file and using a text editor create.
• Create an Array
• Create a function that:
o Use a for loop to loop through the array
o Print the contents of each element using
document.write();
o Use a prompt() to ask for a username
o Using an if statement, take the input and if it is 4
characters long, print "Yes", else print "No".
• Use <body onload="f()"> to execute function on page load
Exceptions and Try/Catch
• Exceptions are easily thrown in a function by adding the
following line. This will exit the function, returning the
exception text.
o throw <text>;
 Example: throw "Error1";
• Try/Catch is as expected:
o try { code } catch (<error>) { more }
Document Object Model (DOM)
• Standard way to access/manipulate HTML documents
• Hierarchy of objects in HTML
• Examples of objects:
o window, location, document, anchors, body
o images, forms, elements, tables
• Code example:
o document.write("<b>This</b> is displayed.");
Cookies
• Stored in text file on client
• Can store multiple values (";" delimited)
• Limited
o 300 per browser
o 20 per web server
o 4KB per cookie
• Default: Read only by originating webpage
o Can be read by others using:
 path - multiple sites
 domain - multiple servers
• Remove by setting the expiration to current time or a
past time.
Cookies (cont)
Example:
document.cookie("username=Andrew;expires=2011-01-11");
var aCookie = document.cookie;
var items = aCookie.split(";");
var expires = items[1].split("=")[1];
• The use of split returns an array of substrings
• After the first go we have "username=Andrew" and "expires=2011-
01-11"
• After the second go we have "expires" and "2011-01-11"
• The variable "expires" now equals "2011-01-11"
Date()
• Date
(); returns the current date.
• Date(<milliseconds>); returns the date since 1970/01/01
• Date(<date_string>); returns date given by string
• Date(y,m,d,h,m,s,ms); returns the date based on which
variables are filled in
o Date(2011, 6, 17); = 6/17/2011
o Date(2011, 6, 17, 13, 5); = 6/17/2011 13:05
Try it
Goto:
http://www.w3schools.com/js/tryit.asp?filename=tryjs_coo
kie_username
• Look at the code to create a cookie.
• In your browser go to where your cookies are stored.
• Find the "username" cookie for www.w3schools.com
• Notice the fields and when it expires.
• Try running the code again
o The cookie hasn't expired yet!
Math
• JavaScript has it's own math functions built in
o abs(x), random(x), round(x), sin(x), etc
• Also has constants defined
o PI, E, LN2, LOG10E, SQRT2
• To access these, just call Math then the
function/constant directly
o Math.abs(x)
o Math.PI
Stand back...I know RegEx
• JavaScript also has an easy way of doing regular
expressions
• There is the functional way:
o var pattern = new RegEx(<pattern>, <modifiers>);
• Or a simplified way:
o var pattern = /<pattern>/modifiers;
• Use test() with RegEx to see if a string matches:
o pattern.test("Hey there");
 Will return true or false
• User exec() to find a matching string and return the results.
Objects
• Similar to classes in other languages
o Can have variables and methods
• var myObject = new Object();
o myObject.name = "Andrew";
o myObject.number = 42;
• var myObject = {name: "Andrew", number: 42};
o myObject.tired = "Yes"
Objects - Functions
• Functions can create objects, effectively being
constructors.
function dude (name, age) {
this.name = name;
this.age = age;
}
dude.setAge = function (x) { this.age = x; };
var guy = new dude("Andrew", 24);
guy.setAge(42)
Objects - Singletons
If an object will only exist in a single instance, you can do
the following:
var myObject = {firstmethod: function (x,y) { code} };
myObject.firstmethod(5,"A");
Try it
In an HTML file:
• Create an object with a few variables
o one contains a string
o one contains a number
o one contains a function
• In the function, use alert(); to display the object's
string
• Using the "for...in" loop, print each of the object's
variable name, alongside with the value.
o for (x in obj) { print x : obj[x] } //Pseudo code
• Call the object's function
Asynchronous JavaScript and XML
• Fast and Dynamic web pages
• Perform behind the scenes to update portions of a
webpage without having to reload the whole page.
• Based on:
o XMLHttpRequest object - communicate with server
o JavaScript/DOM - display/manipulate information
o CSS - Style it to make it look nice
o XML - Format data for transfering
AJAX - XMLHttpRequest
• Create object
o var xmlrequest = new XMLHttpRequest();
• Send a request
o open(httpMethod, targetURL, async);
 xmlrequest.open("GET", "example.asp", true);
o send();
 xmlrequest.send();
XMLHttpRequest - GET
• Simple, fast, good for cached data.
• Simple:
o open("GET", "demo.asp", true)
o Can return cached data
• Fresh:
o open("GET", "demo.asp?t="+Math.random(), true)
o Unique id prevents cached data from appearing
• Send information:
o open("GET", "demo.asp?username=Andrew&age=24",
true)
XMLHttpRequest - POST
• For database accessing, sending large amounts of data, can't
work with caching, need for security/robust transfer
• Simple:
o open("POST", "demo.asp", true)
• Send form information:
o open("POST", "demo.asp", true)
o setRequestHeader("Content-type", "application/x-
www-form-urlencoded")
o send("name=Andrew&age=24");
XMLHttpRequest - Server Response
• If response is not XML
o request.responseText
o Returns the text from the server
• If it is XML
o request.responseXML
o Returns an XML file from the server
o Probably will need to be parsed and then use
AJAX - readyState
• Holds the status of the XMLHttpRequest object
• Perform actions based on the readyState
• onreadystatechange even is triggered when readyState
changes
• onreadystatechange stores a defined function to occur
and process the readyState upon change
AJAX - readyState (cont)
• readyState statuses:
o 0: request not initialized
o 1: server connection established
o 2: request received
o 3: processing request
o 4: request finished and response is ready
• status:
o 200 = "OK"
o 404 = Page not found
The End

More Related Content

What's hot

AngularJS Internal
AngularJS InternalAngularJS Internal
AngularJS Internal
Eyal Vardi
 
Dom
Dom Dom
Spring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutesSpring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutes
VMware Tanzu
 
Cascading style sheet
Cascading style sheetCascading style sheet
Cascading style sheet
Michael Jhon
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentation
ritika1
 
Xpath presentation
Xpath presentationXpath presentation
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
Anis Ahmad
 
React state managmenet with Redux
React state managmenet with ReduxReact state managmenet with Redux
React state managmenet with Redux
Vedran Blaženka
 
Html basics
Html basicsHtml basics
Html basics
mcatahir947
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Model
chomas kandar
 
JavaScript
JavaScriptJavaScript
JavaScript
Vidyut Singhania
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
Jennifer Estrada
 
Dom
DomDom
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
Document object model
Document object modelDocument object model
Document object model
Amit kumar
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript Basics
Mindfire Solutions
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
Amit Tyagi
 
Introduction into ES6 JavaScript.
Introduction into ES6 JavaScript.Introduction into ES6 JavaScript.
Introduction into ES6 JavaScript.
boyney123
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
Adieu
 
jQuery
jQueryjQuery

What's hot (20)

AngularJS Internal
AngularJS InternalAngularJS Internal
AngularJS Internal
 
Dom
Dom Dom
Dom
 
Spring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutesSpring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutes
 
Cascading style sheet
Cascading style sheetCascading style sheet
Cascading style sheet
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentation
 
Xpath presentation
Xpath presentationXpath presentation
Xpath presentation
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
 
React state managmenet with Redux
React state managmenet with ReduxReact state managmenet with Redux
React state managmenet with Redux
 
Html basics
Html basicsHtml basics
Html basics
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Model
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
 
Dom
DomDom
Dom
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 
Document object model
Document object modelDocument object model
Document object model
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript Basics
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
Introduction into ES6 JavaScript.
Introduction into ES6 JavaScript.Introduction into ES6 JavaScript.
Introduction into ES6 JavaScript.
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
jQuery
jQueryjQuery
jQuery
 

Similar to Java script

BITM3730 10-17.pptx
BITM3730 10-17.pptxBITM3730 10-17.pptx
BITM3730 10-17.pptx
MattMarino13
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Marlon Jamera
 
Javascript
JavascriptJavascript
Javascript
Prashant Kumar
 
Java script
Java scriptJava script
Java script
Jay Patel
 
Unit 4(it workshop)
Unit 4(it workshop)Unit 4(it workshop)
Unit 4(it workshop)
Dr.Lokesh Gagnani
 
Javascript
JavascriptJavascript
javaScript and jQuery
javaScript and jQueryjavaScript and jQuery
javaScript and jQuery
Mehrab Hossain
 
BITM3730 10-4.pptx
BITM3730 10-4.pptxBITM3730 10-4.pptx
BITM3730 10-4.pptx
MattMarino13
 
BITM3730 10-3.pptx
BITM3730 10-3.pptxBITM3730 10-3.pptx
BITM3730 10-3.pptx
MattMarino13
 
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSON
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSONAn introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSON
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSON
Syed Moosa Kaleem
 
lecture 6 javascript event and event handling.ppt
lecture 6 javascript event and event handling.pptlecture 6 javascript event and event handling.ppt
lecture 6 javascript event and event handling.ppt
ULADATZ
 
BITM3730Week6.pptx
BITM3730Week6.pptxBITM3730Week6.pptx
BITM3730Week6.pptx
MattMarino13
 
How to make Ajax work for you
How to make Ajax work for youHow to make Ajax work for you
How to make Ajax work for you
Simon Willison
 
Final Java-script.pptx
Final Java-script.pptxFinal Java-script.pptx
Final Java-script.pptx
AlkanthiSomesh
 
Java Script
Java ScriptJava Script
Java Script
Sarvan15
 
Java Script
Java ScriptJava Script
Java Script
Sarvan15
 
Lect-5--JavaScript-Intro-12032024-105816am.pptx
Lect-5--JavaScript-Intro-12032024-105816am.pptxLect-5--JavaScript-Intro-12032024-105816am.pptx
Lect-5--JavaScript-Intro-12032024-105816am.pptx
zainm7032
 
JavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQueryJavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQuery
Jamshid Hashimi
 
gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptx
sandeshshahapur
 
JSLT: JSON querying and transformation
JSLT: JSON querying and transformationJSLT: JSON querying and transformation
JSLT: JSON querying and transformation
Lars Marius Garshol
 

Similar to Java script (20)

BITM3730 10-17.pptx
BITM3730 10-17.pptxBITM3730 10-17.pptx
BITM3730 10-17.pptx
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Javascript
JavascriptJavascript
Javascript
 
Java script
Java scriptJava script
Java script
 
Unit 4(it workshop)
Unit 4(it workshop)Unit 4(it workshop)
Unit 4(it workshop)
 
Javascript
JavascriptJavascript
Javascript
 
javaScript and jQuery
javaScript and jQueryjavaScript and jQuery
javaScript and jQuery
 
BITM3730 10-4.pptx
BITM3730 10-4.pptxBITM3730 10-4.pptx
BITM3730 10-4.pptx
 
BITM3730 10-3.pptx
BITM3730 10-3.pptxBITM3730 10-3.pptx
BITM3730 10-3.pptx
 
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSON
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSONAn introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSON
An introduction to DOM , JAVASCRIPT , JQUERY, AJAX and JSON
 
lecture 6 javascript event and event handling.ppt
lecture 6 javascript event and event handling.pptlecture 6 javascript event and event handling.ppt
lecture 6 javascript event and event handling.ppt
 
BITM3730Week6.pptx
BITM3730Week6.pptxBITM3730Week6.pptx
BITM3730Week6.pptx
 
How to make Ajax work for you
How to make Ajax work for youHow to make Ajax work for you
How to make Ajax work for you
 
Final Java-script.pptx
Final Java-script.pptxFinal Java-script.pptx
Final Java-script.pptx
 
Java Script
Java ScriptJava Script
Java Script
 
Java Script
Java ScriptJava Script
Java Script
 
Lect-5--JavaScript-Intro-12032024-105816am.pptx
Lect-5--JavaScript-Intro-12032024-105816am.pptxLect-5--JavaScript-Intro-12032024-105816am.pptx
Lect-5--JavaScript-Intro-12032024-105816am.pptx
 
JavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQueryJavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQuery
 
gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptx
 
JSLT: JSON querying and transformation
JSLT: JSON querying and transformationJSLT: JSON querying and transformation
JSLT: JSON querying and transformation
 

More from vishal choudhary

SE-Lecture1.ppt
SE-Lecture1.pptSE-Lecture1.ppt
SE-Lecture1.ppt
vishal choudhary
 
SE-Testing.ppt
SE-Testing.pptSE-Testing.ppt
SE-Testing.ppt
vishal choudhary
 
SE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.pptSE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.ppt
vishal choudhary
 
SE-Lecture-7.pptx
SE-Lecture-7.pptxSE-Lecture-7.pptx
SE-Lecture-7.pptx
vishal choudhary
 
Se-Lecture-6.ppt
Se-Lecture-6.pptSe-Lecture-6.ppt
Se-Lecture-6.ppt
vishal choudhary
 
SE-Lecture-5.pptx
SE-Lecture-5.pptxSE-Lecture-5.pptx
SE-Lecture-5.pptx
vishal choudhary
 
XML.pptx
XML.pptxXML.pptx
SE-Lecture-8.pptx
SE-Lecture-8.pptxSE-Lecture-8.pptx
SE-Lecture-8.pptx
vishal choudhary
 
SE-coupling and cohesion.ppt
SE-coupling and cohesion.pptSE-coupling and cohesion.ppt
SE-coupling and cohesion.ppt
vishal choudhary
 
SE-Lecture-2.pptx
SE-Lecture-2.pptxSE-Lecture-2.pptx
SE-Lecture-2.pptx
vishal choudhary
 
SE-software design.ppt
SE-software design.pptSE-software design.ppt
SE-software design.ppt
vishal choudhary
 
SE1.ppt
SE1.pptSE1.ppt
SE-Lecture-4.pptx
SE-Lecture-4.pptxSE-Lecture-4.pptx
SE-Lecture-4.pptx
vishal choudhary
 
SE-Lecture=3.pptx
SE-Lecture=3.pptxSE-Lecture=3.pptx
SE-Lecture=3.pptx
vishal choudhary
 
Multimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptxMultimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptx
vishal choudhary
 
MultimediaLecture5.pptx
MultimediaLecture5.pptxMultimediaLecture5.pptx
MultimediaLecture5.pptx
vishal choudhary
 
Multimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptxMultimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptx
vishal choudhary
 
MultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptxMultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptx
vishal choudhary
 
Multimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptxMultimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptx
vishal choudhary
 
Multimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptxMultimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptx
vishal choudhary
 

More from vishal choudhary (20)

SE-Lecture1.ppt
SE-Lecture1.pptSE-Lecture1.ppt
SE-Lecture1.ppt
 
SE-Testing.ppt
SE-Testing.pptSE-Testing.ppt
SE-Testing.ppt
 
SE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.pptSE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.ppt
 
SE-Lecture-7.pptx
SE-Lecture-7.pptxSE-Lecture-7.pptx
SE-Lecture-7.pptx
 
Se-Lecture-6.ppt
Se-Lecture-6.pptSe-Lecture-6.ppt
Se-Lecture-6.ppt
 
SE-Lecture-5.pptx
SE-Lecture-5.pptxSE-Lecture-5.pptx
SE-Lecture-5.pptx
 
XML.pptx
XML.pptxXML.pptx
XML.pptx
 
SE-Lecture-8.pptx
SE-Lecture-8.pptxSE-Lecture-8.pptx
SE-Lecture-8.pptx
 
SE-coupling and cohesion.ppt
SE-coupling and cohesion.pptSE-coupling and cohesion.ppt
SE-coupling and cohesion.ppt
 
SE-Lecture-2.pptx
SE-Lecture-2.pptxSE-Lecture-2.pptx
SE-Lecture-2.pptx
 
SE-software design.ppt
SE-software design.pptSE-software design.ppt
SE-software design.ppt
 
SE1.ppt
SE1.pptSE1.ppt
SE1.ppt
 
SE-Lecture-4.pptx
SE-Lecture-4.pptxSE-Lecture-4.pptx
SE-Lecture-4.pptx
 
SE-Lecture=3.pptx
SE-Lecture=3.pptxSE-Lecture=3.pptx
SE-Lecture=3.pptx
 
Multimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptxMultimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptx
 
MultimediaLecture5.pptx
MultimediaLecture5.pptxMultimediaLecture5.pptx
MultimediaLecture5.pptx
 
Multimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptxMultimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptx
 
MultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptxMultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptx
 
Multimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptxMultimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptx
 
Multimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptxMultimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptx
 

Recently uploaded

Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 

Recently uploaded (20)

Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 

Java script

  • 2. What is JavaScript? • Client side interpreted embedded programming language used to enhance websites • No relation to Java • Can manipulate HTML • Event driven
  • 3. To use or not to use • Helpful for: o Dynamic content o Adding logic to HTML o Make changes without refreshing the page o Form validation o Ease processing on server • Not helpful for: o Accessing resources o Do anything that requires privacy  JavaScript is shown publicly
  • 4. Adding JavaScript to HTML • Inline: o <script language="javascript"> code </script> • External: o <script language="javascript" src="code.js" /> • Typically this is placed in between the <head> tags • Case sensitive (unlike HTML)
  • 5. Variables • Variables are untyped o No need to declare type (int, bool, etc) o Doesn't care about type until called • Example: o var name = "Andrew"; o var num = 24; o var name = num; o name will now return 24
  • 6. Arrays • Strings are arrays o var x = "Andrew"; x[3] returns "r" • Array is also it's own data structure and doesn't require all items to be of the same type. o var x = new Array();  x[0] = "Rawr";  x[1] = 9001; o var x = new Array("Rawr", 9001); o var x = ["Rawr", 9001]; • Arrays have a length: o x.length
  • 7. If..Else / Switch • Similar to what you'd expect o if (<condition>) { code } o if (<condition>) { code } else { more } o if (<condition>) { code } else if { more } else { finally } switch (<variable>) { case <match>: code break; case <match2>: break; default: }
  • 8. Loops • For: o for (<var>; <condition>; <do>) { code } o for (<var> in <list>) { code } • While: o while (<condition>) { code } o do { code } while (<condition>); • break and continue work as expected
  • 9. Functions • Similar to any other languages' functions and methods • Can have nested functions o Variables have scope similar to nested loops • Used by events o Runs only when called • Example: o function <name> (<parameters>) { code }
  • 10. Popups • Used for displaying information to the user outside of the page. o Typically as a warning or when an error has occured o Ask user for additional information o Confirmation • alert(<text>); - Exits via an okay button • confirm(<text>); - Returns true or false (ok or cancel) • prompt(<text>, <default>); - Returns user's input
  • 11. Try it Create your own html file and using a text editor create. • Create an Array • Create a function that: o Use a for loop to loop through the array o Print the contents of each element using document.write(); o Use a prompt() to ask for a username o Using an if statement, take the input and if it is 4 characters long, print "Yes", else print "No". • Use <body onload="f()"> to execute function on page load
  • 12. Exceptions and Try/Catch • Exceptions are easily thrown in a function by adding the following line. This will exit the function, returning the exception text. o throw <text>;  Example: throw "Error1"; • Try/Catch is as expected: o try { code } catch (<error>) { more }
  • 13. Document Object Model (DOM) • Standard way to access/manipulate HTML documents • Hierarchy of objects in HTML • Examples of objects: o window, location, document, anchors, body o images, forms, elements, tables • Code example: o document.write("<b>This</b> is displayed.");
  • 14. Cookies • Stored in text file on client • Can store multiple values (";" delimited) • Limited o 300 per browser o 20 per web server o 4KB per cookie • Default: Read only by originating webpage o Can be read by others using:  path - multiple sites  domain - multiple servers • Remove by setting the expiration to current time or a past time.
  • 15. Cookies (cont) Example: document.cookie("username=Andrew;expires=2011-01-11"); var aCookie = document.cookie; var items = aCookie.split(";"); var expires = items[1].split("=")[1]; • The use of split returns an array of substrings • After the first go we have "username=Andrew" and "expires=2011- 01-11" • After the second go we have "expires" and "2011-01-11" • The variable "expires" now equals "2011-01-11"
  • 16. Date() • Date (); returns the current date. • Date(<milliseconds>); returns the date since 1970/01/01 • Date(<date_string>); returns date given by string • Date(y,m,d,h,m,s,ms); returns the date based on which variables are filled in o Date(2011, 6, 17); = 6/17/2011 o Date(2011, 6, 17, 13, 5); = 6/17/2011 13:05
  • 17. Try it Goto: http://www.w3schools.com/js/tryit.asp?filename=tryjs_coo kie_username • Look at the code to create a cookie. • In your browser go to where your cookies are stored. • Find the "username" cookie for www.w3schools.com • Notice the fields and when it expires. • Try running the code again o The cookie hasn't expired yet!
  • 18. Math • JavaScript has it's own math functions built in o abs(x), random(x), round(x), sin(x), etc • Also has constants defined o PI, E, LN2, LOG10E, SQRT2 • To access these, just call Math then the function/constant directly o Math.abs(x) o Math.PI
  • 19. Stand back...I know RegEx • JavaScript also has an easy way of doing regular expressions • There is the functional way: o var pattern = new RegEx(<pattern>, <modifiers>); • Or a simplified way: o var pattern = /<pattern>/modifiers; • Use test() with RegEx to see if a string matches: o pattern.test("Hey there");  Will return true or false • User exec() to find a matching string and return the results.
  • 20. Objects • Similar to classes in other languages o Can have variables and methods • var myObject = new Object(); o myObject.name = "Andrew"; o myObject.number = 42; • var myObject = {name: "Andrew", number: 42}; o myObject.tired = "Yes"
  • 21. Objects - Functions • Functions can create objects, effectively being constructors. function dude (name, age) { this.name = name; this.age = age; } dude.setAge = function (x) { this.age = x; }; var guy = new dude("Andrew", 24); guy.setAge(42)
  • 22. Objects - Singletons If an object will only exist in a single instance, you can do the following: var myObject = {firstmethod: function (x,y) { code} }; myObject.firstmethod(5,"A");
  • 23. Try it In an HTML file: • Create an object with a few variables o one contains a string o one contains a number o one contains a function • In the function, use alert(); to display the object's string • Using the "for...in" loop, print each of the object's variable name, alongside with the value. o for (x in obj) { print x : obj[x] } //Pseudo code • Call the object's function
  • 24. Asynchronous JavaScript and XML • Fast and Dynamic web pages • Perform behind the scenes to update portions of a webpage without having to reload the whole page. • Based on: o XMLHttpRequest object - communicate with server o JavaScript/DOM - display/manipulate information o CSS - Style it to make it look nice o XML - Format data for transfering
  • 25. AJAX - XMLHttpRequest • Create object o var xmlrequest = new XMLHttpRequest(); • Send a request o open(httpMethod, targetURL, async);  xmlrequest.open("GET", "example.asp", true); o send();  xmlrequest.send();
  • 26. XMLHttpRequest - GET • Simple, fast, good for cached data. • Simple: o open("GET", "demo.asp", true) o Can return cached data • Fresh: o open("GET", "demo.asp?t="+Math.random(), true) o Unique id prevents cached data from appearing • Send information: o open("GET", "demo.asp?username=Andrew&age=24", true)
  • 27. XMLHttpRequest - POST • For database accessing, sending large amounts of data, can't work with caching, need for security/robust transfer • Simple: o open("POST", "demo.asp", true) • Send form information: o open("POST", "demo.asp", true) o setRequestHeader("Content-type", "application/x- www-form-urlencoded") o send("name=Andrew&age=24");
  • 28. XMLHttpRequest - Server Response • If response is not XML o request.responseText o Returns the text from the server • If it is XML o request.responseXML o Returns an XML file from the server o Probably will need to be parsed and then use
  • 29. AJAX - readyState • Holds the status of the XMLHttpRequest object • Perform actions based on the readyState • onreadystatechange even is triggered when readyState changes • onreadystatechange stores a defined function to occur and process the readyState upon change
  • 30. AJAX - readyState (cont) • readyState statuses: o 0: request not initialized o 1: server connection established o 2: request received o 3: processing request o 4: request finished and response is ready • status: o 200 = "OK" o 404 = Page not found