06 – Java Script (2) Informatics Department Parahyangan Catholic University.

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

Tutorial 5 Windows and Frames Section A - Working with Windows.
Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
JavaScript and AJAX Jonathan Foss University of Warwick
Intro to JavaScript. JavaScript History Client (generally browser-side) language invented at Netscape under the name LiveScript around 1995 Netscape wanted.
Java Script Session1 INTRODUCTION.
Javascript Introduction Norman White Material is from w3schools.com Go there to run examples interactively.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
WEAVING CODE EXTENSIONS INTO JAVASCRIPT Benjamin Lerner, Herman Venter, and Dan Grossman University of Washington, Microsoft Research.
HTML 5 and CSS 3, Illustrated Complete Unit L: Programming Web Pages with JavaScript.
The Web Warrior Guide to Web Design Technologies
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
Computer Science 103 Chapter 4 Advanced JavaScript.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Introduction to scripting
CST JavaScript Validating Form Data with JavaScript.
JQuery CS 268. What is jQuery? From their web site:
4.1 JavaScript Introduction
Tutorial 14 Working with Forms and Regular Expressions.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Sascha Wener.  Definition from Microsoft Developer Network: “A theme is a unified set of design elements and color schemes that you apply to pages to.
JQuery 10/21. Today jQuery Some cool tools around the web JavaScript Libraries Drawing libraries HTML Frameworks Conventions.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
THE BIG PICTURE. How does JavaScript interact with the browser?
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
JavaScript Library Showdown Rob Larsen htmlcssjavascript.com htmlcssjavascript.com /downloads/libraries.ppt.
JQUERY | INTRODUCTION. jQuery  Open source JavaScript library  Simplifies the interactions between  HTML document, or the Document Object Model (DOM),
Week 9 PHP Cookies and Session Introduction to JavaScript.
1 Introduction to Javascript Peter Atkinson. 2 Objectives To understand and use appropriately some of the basic elements of Javascript: –alert and prompt.
Review IDIA 619 Spring 2013 Bridget M. Blodgett. HTML A basic HTML document looks like this: Sample page Sample page This is a simple sample. HTML user.
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Lecture 9: AJAX, Javascript review..  AJAX  Synchronous vs. asynchronous browsing.  Refreshing only “part of a page” from a URL.  Frameworks: Prototype,
ALBERT WAVERING BOBBY SENG. Week 4: JavaScript  Quiz  Announcements/questions.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
05 – Java Script (1) Informatics Department Parahyangan Catholic University.
JQuery JavaScript is a powerful language but it is not always easy to work with. jQuery is a JavaScript library that helps with: – HTML document traversal.
JavaScript Challenges Answers for challenges
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
“The world’s most misunderstood language has become the world’s most popular programming language” Akshay Arora
1 Javascript CS , Spring What is Javascript ? Browser scripting language  Dynamic page creation  Interactive  Embedded into HTML pages.
Javascript Overview. What is Javascript? May be one of the most popular programming languages ever Runs in the browser, not on the server All modern browsers.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
Understanding JavaScript and Coding Essentials Lesson 8.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
JavaScript and Ajax (JavaScript Arrays) Week 5 Web site:
Chapter 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
CGS 3066: Web Programming and Design Spring 2016 Introduction to JavaScript.
Chapter 5 Murach's JavaScript and jQuery, C1© 2012, Mike Murach & Associates, Inc.Slide 1.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
The Web Wizard’s Guide To DHTML and CSS Chapter 2 A Review of CSS2 and JavaScript.
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
JQuery is a fast, small, and feature-rich javascript library. It makes things like HTML document traversal and manipulation, event handling, animation,
JavaScript: Good Practices
HTML.
Introduction to JavaScript
JavaScript and Ajax (Expression and Operators)
A second look at JavaScript
Web Systems Development (CSC-215)
ARRAYS MIT-AITI Copyright 2001.
Introduction to JavaScript
JavaScript CS 4640 Programming Languages for Web Applications
JavaScript: Introduction to Scripting
PHP an introduction.
Web Programming and Design
Web Programming and Design
Web Programming and Design
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

06 – Java Script (2) Informatics Department Parahyangan Catholic University

 An Object is a collection of attributes (variables) and methods (functions) that deal with related values and tasks  Simplest way to create an object in JS: var rocket = new Object(); This creates a new instance of an Object, a blank object template, and assigned it to a variable

 Adding attributes : var rocket = new Object(); rocket.engineCount = 2; rocket.thrust = 5000; rocket.astronautCount = 4; rocket.colour = "red";

 How do we make 2 rockets ? var rocket = new Object(); rocket.engineCount = 2; var anotherRocket = new Object(); anotherRocket.engineCount = 1; anotherRocket.wings = 2; Both rocket objects are seemingly identical, however, if we outputted the number of wings on the second rocket, we’d be returned “2”, but if we did the same to the first rocket, we’d be returned “undefined”.

 Similar concept as Java’s Class’ constructor  Creating objects from a template: function Rocket(engineCount, thrust, wings){ this.engineCount = engineCount; this.thrust = thrust; this.wings = wings; } var rocket = new Rocket(2, 5000, 4); var anotherRocket = new Rocket(1, 2000, 2); var creates a local variable this. creates an attribute for this instance of the object var creates a local variable this. creates an attribute for this instance of the object

 x is assigned a foo object with name attribute equals “FOOOOO”  y is assigned a number value: 123 var x = new foo(); var y = foo(); function foo(){ this.name = "FOOOOO"; return 123; }

 Adding methods:  Usage example: function Rocket(engineCount, thrust, wings) { this.engineCount = engineCount; this.enginesOn = false; this.thrust = thrust; this.wings = wings; this.turnEnginesOn = function() { this.enginesOn = true; alert("Engines are now on"); } var rocket = new Rocket(2, 5000, 4); rocket.turnEnginesOn();

 An object method is basically a function assigned to an object attribute.  functions are objects so they’re a data type and can be assigned to a variable.  The name of the method will take the name of the attribute the method is assigned to

 Similar to Java’s static keyword  prototype attributes / methods are shared among all instances of the same object function Rocket(engineCount, thrust, wings) { this.engineCount = engineCount; this.enginesOn = false; this.thrust = thrust; this.wings = wings; Rocket.prototype.fuel = “Liquid hydrogen”; Rocket.prototype.turnEnginesOn = function() { this.enginesOn = true; alert("Engines are now on"); }

 An associative array is one whose elements are referenced by name rather than by numeric offset. balls = { "golf": "Golf balls, 6", "tennis": "Tennis balls, 3", "soccer": "Soccer ball, 1", "ping": "Ping Pong balls, 1 doz“ } for (b in balls) document.write(b + " = " + balls[b] + " ")

 Creating an empty Array  concat concatenates two arrays, or a series of values with an array fruit = ["Banana", "Grape"]; veg = ["Carrot", "Cabbage"]; all = fruit.concat(veg); document.write(all); var A = Array(); pets = ["Cat", "Dog", "Fish"]; more_pets = pets.concat("Rabbit", "Hamster"); document.write(more_pets); RESULT: Banana,Grape,Carrot,Cabbage RESULT: Banana,Grape,Carrot,Cabbage RESULT: Cat,Dog,Fish,Rabbit,Hamster RESULT: Cat,Dog,Fish,Rabbit,Hamster

 push inserts a new value into an array  pop deletes the last inserted value and returns it sports = ["Football", "Tennis", "Baseball"]; document.write("Start = " + sports + " "); sports.push("Hockey"); document.write("After Push = " + sports + " "); removed = sports.pop(); document.write("After Pop = " + sports + " "); document.write("Removed = " + removed + " "); RESULT: Start = Football,Tennis,Baseball After Push = Football,Tennis,Baseball,Hockey After Pop = Football,Tennis,Baseball Removed = Hockey RESULT: Start = Football,Tennis,Baseball After Push = Football,Tennis,Baseball,Hockey After Pop = Football,Tennis,Baseball Removed = Hockey

 reverse  join convert all the values in an array to strings and then join them together into one large string, placing an optional separator between them. sports = ["Football", "Tennis", "Baseball", "Hockey"]; sports.reverse(); document.write(sports); pets = ["Cat", "Dog", "Rabbit", "Hamster"]; document.write(pets.join() + " "); document.write(pets.join(' : ') + " "); RESULT: Hockey,Baseball,Tennis,Football RESULT: Hockey,Baseball,Tennis,Football RESULT: Cat,Dog,Rabbit,Hamster Cat : Dog : Rabbit : Hamster RESULT: Cat,Dog,Rabbit,Hamster Cat : Dog : Rabbit : Hamster

 sort // Alphabetical sort sports = ["Football", "Tennis", "Baseball", "Hockey"]; sports.sort(); document.write(sports + " "); // Reverse alphabetical sort sports = ["Football", "Tennis", "Baseball", "Hockey"]; sports.sort().reverse(); document.write(sports + " "); // Ascending numerical sort numbers = [7, 23, 6, 74]; numbers.sort(function(a,b){return a - b}); document.write(numbers + " "); // Descending numerical sort numbers = [7, 23, 6, 74]; numbers.sort(function(a,b){return b - a}); document.write(numbers + " ");

 timers are methods that allow us to run blocks of code once after a certain amount of time has passed, or many times after a repeating interval

 Setting one-off timers  Unsetting one-off timers function onTimeout() { alert("Ding dong!"); }; var timer = setTimeout(onTimeout, 3000); clearTimeout(timer);

 Setting repeating timers  Unsetting repeating timers function onInterval() { alert("Ding dong!"); }; var interval = setInterval(onInterval, 3000); clearInterval(interval);

 jQuery is a JavaScript library  Why are we using it?  Most of the core features are apparent in all the major browsers  However, a lot of browsers implement other features in slightly different ways.  One example is detecting when a HTML document has finished loading (more on this later)

 To use JQuery, we need to link the JQuery file into our HTML document (need to go to the jQuery website, download the library, and add it into the same folder as your HTML page.)  Second option is to use Google-Hosted version

 Why can’t we just run JavaScript at any time?  Three ways:  The wrong way (window.onload)  The long way (The DOM)  The easy way (JQuery)

 The wrong way (window.onload)  window.onload is too patient. It doesn’t run until absolutely every piece of content has finished loading. window.onload = function() { alert("Hello, World!"); };

 The long way (The DOM) the DOM represents the raw structure of our content, which means it has to be created before the content is displayed on the screen. If we can find out when the DOM has finished loading, we’ll know when the content is accessible, regardless of whether it’s visible on the screen.  Unfortunately, detecting when the DOM has loaded is a troublesome task. There’s just no consistent method across all the major browsers.

// Dean Edwards/Matthias Miller/John Resig function init() { // quit if this function has already been called if (arguments.callee.done) return; // flag this function so we don't do the same thing twice arguments.callee.done = true; // kill the timer if (_timer) clearInterval(_timer); // do stuff }; /* for Mozilla/Opera9 */ if (document.addEventListener) { document.addEventListener("DOMContentLoa ded", init, false); } /* for Internet Explorer document.write("<script id=__ie_onload defer src=javascript:void(0)> "); var script = document.getElementById("__ie_onload"); script.onreadystatechange = function() { if (this.readyState == "complete") { init(); // call the onload handler } /* for Safari */ if (/WebKit/i.test(navigator.userAgent)) { // sniff var _timer = setInterval(function() { if (/loaded|complete/.test(document.readyState)) { init(); // call the onload handler } }, 10); } /* for other browsers */ window.onload = init;

 The easy way (JQuery) $(document).ready(function() { alert("Hello, World!"); }); JQuery selector a method that does the same thing as Dean Edwards’ method

USING PURE JAVA SCRIPT  getElementById(“blog”)  getElementsByTagName(“p”)  innerHTML USING JQUERY  $(“#blog”)  $(“p”) .html() var secondaryHeadings = $("h2"); alert(secondaryHeadings.html()); secondaryHeadings.html("Now we've changed the content"); Example: