Understanding JavaScript and Coding Essentials

Slides:



Advertisements
Similar presentations
Introducing JavaScript
Advertisements

JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
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.
HTML 5 and CSS 3, Illustrated Complete Unit L: Programming Web Pages with JavaScript.
Lesson 12- Unit L Programming Web Pages with JavaScript.
The Web Warrior Guide to Web Design Technologies
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Lecture 11 – DOM Scripting SFDV3011 – Advanced Web Development Reference: 1.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad GM-IT CIIT Islamabad CIIT Virtual Campus, CIIT COMSATS Institute.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
1 JavaScript in Context. Server-Side Programming.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
JavaScript, jQuery, and Mashups Incorporating JavaScript, jQuery, and other Mashups into existing pages.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
Java Script About Java Script Document Object Model Incorporating JavaScript Adding JavaScript to HTML Embedding a Javascript External Scripts Javascript.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
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 Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
4.1. Manage and maintain JavaScript Update the UI by using JavaScript. Understanding JavaScript and Coding Essentials.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Tarik Booker CS 120 California State University, Los Angeles.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
Fall 2016 CSULA Saloni Chacha
Introduction to.
Build in Objects In JavaScript, almost "everything" is an object.
Java Script Introduction. Java Script Introduction.
Getting Started with CSS
Programming Web Pages with JavaScript
Unit M Programming Web Pages with
Chapter 6 JavaScript: Introduction to Scripting
>> JavaScript: Location, Functions and Objects
Week 4: Introduction to Javascript
JavaScript is a programming language designed for Web pages.
Unit M Programming Web Pages with
Intro to JavaScript CS 1150 Spring 2017.
Introduction to Web programming
My First Web Page document.write("" + Date() + ""); To insert.
JavaScript Syntax and Semantics
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
14 A Brief Look at JavaScript and jQuery.
Introduction to JavaScript
DHTML.
JavaScript Introduction
JavaScript an introduction.
DHTML Javascript Internet Technology.
A second look at JavaScript
DHTML Javascript Internet Technology.
T. Jumana Abu Shmais – AOU - Riyadh
Training & Development
E-commerce Applications Development
Introduction to JavaScript
Tutorial 10: Programming with javascript
JavaScript Basics What is JavaScript?
JavaScript is a scripting language designed for Web pages by Netscape.
Introduction to Programming and JavaScript
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Web Programming and Design
Web Programming and Design
Web Programming and Design
Introduction to Web programming
Presentation transcript:

Understanding JavaScript and Coding Essentials 4.1. Manage and maintain JavaScript. 4.2. Update the UI by using JavaScript.

Agenda 1 JavaScript 6 Updating the Content of Elements 2 Functions and Variables 7 Adding Elements 3 jQuery and Third-Party Libraries 4 Locating and Accessing Elements 5 Listening and Responding to Events

JavaScript

Building Interactive Applications HTML5 and CSS3 are awesome for creating beautiful websites However, today’s users want an interactive Web experience Interactivity allows a user to take an action and receive a response Implementing interactivity requires a programming language, such as JavaScript

What is JavaScript? JavaScript is a loosely-typed scripting language that is interpreted by a browser It changes how elements in an HTML document act and respond to user input We create scripts with JavaScript Scripts are step-by-step instructions that tell a browser how to respond to events, such as a user clicking a button The file extension for a script is .js .js

Connecting JavaScript with HTML We can connect JavaScript to HTML documents in a couple of ways: Embedding it with the <script> tag Linking a separate JavaScript file to the HTML document <script type="text/javascript"> document.write("Hello World Wide Web"); </script> 1 <head> <script type="text/javascript" src="Script.js"></script> </head> 2

JavaScript Demo <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title></title> </head> <body> <h1>This is a boring website!</h1> <script type="text/javascript"> document.write("Hello, World!"); </script> </body> </html> 1

Functions and Variables

Functions A function is a group of statements that are combined to perform a specific task A statement is a line of code that performs an action Statements should end with a semicolon (;) If different parts of a script repeat the same task, then you can reuse a function instead of repeating the same statements function doSomethingAwesome() { var name = prompt(“What is your name?”); alert(name + “, you just did something awesome!"); }

How to Define a Function function helloWorld() { alert(‘Hello, World!’); } function keyword function name statement everything between the curly braces is a code block

Naming Functions A function can have any name, but there are a couple guidelines that must be considered: Don’t use any of the reserved words defined by JavaScript standards. See the full list of JavaScript reserved words by clicking here The name must be made of letters, digits, underscores, or dollar signs It can’t start with a number though!

Definition and Execution of Functions The way that a function is defined is different from how it is executed by a browser A function’s definition outlines its name, any parameters it may take, and its statements NOTE: a definition doesn’t perform any of a function’s statements When a function is called, the browser will execute all of the statements within the function <input type="button" value="Click Me" onclick="doSomethingAwesome()"> Calling the Function function doSomethingAwesome() { var name = prompt(“What is your name?”); alert(name + “, you just did something awesome!"); } Defining the Function

Variables Scripts have to temporarily store pieces of information These bits of data can be stored as variables It’s called a variable because its values can vary every time a program is run Variables can be defined using the var syntax with a unique keyword, such as height or width

How to Declare a Variable var height = 6; variable keyword variable name variable value assignment operator

Rules for Naming Variables Variable names must start with a letter, dollar sign ($), or an underscore (_). It must NOT start with a number. Variable names can contain letters, numbers, dollar signs, and underscores, but NOT dashes (-) or periods (.). You cannot use keywords or reserved words. 1 2 3 Variables are case sensitive, which means that thisVariable is different from ThisVariable. Use names that describe the information you are storing. If a variable name uses two or more words, capitalize the first letter of ever word AFTER the first word. 4 5 6

Types of Data Numbers  1, 2, 3 Strings  ‘Zombies freak me out!’ Must always be surrounded by quote marks Boolean  true, false

Comments Add comments to your script to explain what it does /*These comments are typically reserved for describing how an entire script file works or to comment out an entire block of script. */ //this function does something awesome! function doSomethingAwesome() { var name = prompt(“What is your name?”); alert(name + “, you just did something awesome!"); } JavaScript Add comments to your script to explain what it does It will also make your code easier for others to read and understand Add a single-line comment by placing two forward slash characters // in front of your comment Anything after the slashes won’t be interpreted by the browser Add a multi-line comment by starting with the /* characters and ending with the */ characters Anything between these characters won’t be interpreted by the browser

jQuery and Third-Party LIbraries

JavaScript Libraries JavaScript Libraries are made of code that other programmers have already developed libraries include pre-written functions and statements that you can use to create programs Use a library by linking its file to your web page One of the most popular JavaScript libraries is jQuery jQuery allows you to use CSS-like selectors and its methods to perform functions with minimal code

Locating and Accessing Elements

Objects in JavaScript An HTML element is an object, similar to a house or a car Just as with real life objects, we can access and modify HTML objects that appear on a screen The creation of interactive Web pages and apps relies on our ability to manipulate objects on a screen Objects are models of things in the real world that were built using data Objects are grouped into object models, which are used to represent Browsers and Web pages THIS IS AN OBJECT

Document Object Model (DOM) 11/15/2018 1:46 PM Document Object Model (DOM) The Document Object Model (DOM) creates a model of a Web page The DOM is used to update content, structure, and styles on the fly The topmost object is the document object, which represents the page as a whole It has child objects that represent individual elements on a page document <html> <head> <title> <body> <div> attribute <p> text> © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Locating and Accessing Elements We can access objects in the DOM using an element’s ID To do so, we can use the document object’s getElementById() method This means that the element must have an ID Using this method allows you to manipulate the contents of that element document.getElementById(‘demo’); object method name parameter

getElementById() Demo <body> <h1>Get today's date and add it to an element on the page.</h1> <p id="demo"></p> <script type="text/javascript"> document.getElementById("demo").innerHTML=Date(); </script> </body>

Listening and Responding to Events

Events in Programming Events are actions that a user takes 11/15/2018 1:46 PM Events in Programming Events are actions that a user takes JavaScript features event handlers, which respond to specific user events For example, the onClick event handler responds to clicks on screen Event handlers respond by executing functions Event Handlers Associated Events onsubmit form submission onkeydown onkeypress onkeyup keystrokes onclick onmousedown onmouseup mouse or touchpad clicks onload onunload page loading/unloading onselect item selection © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Event Handlers Demo <body> <p>Select some of the text: <input type="text" value="Hello, World!" onselect="myFunction()"></p> <p id="demo"></p> <script> function myFunction() { document.getElementById('demo').innerHTML = "Selecting text is awesome!"; } </script> </body>

Updating the Content of Elements

Updating Content in Elements Use the innerHTML property to change content or insert new content between element tags It can be used on any element To change content, set the innerHTML property to the desired string To do this, we must use the equals symbol (=) To remove content, set it to an empty string

innerHTML Demo <body> <h1>Updating Content</h1> <p id="demo"></p> <script type="text/javascript"> document.getElementById("demo").innerHTML=‘Using JavaScript is super fun!’; </script> </body>

Adding Elements

The createElement method function show_image(src, width, height, alt) { var img = document.createElement("img"); img.src = src; img.width = width; img.height = height; img.alt = alt; // Adds it to the <body> tag document.body.appendChild(img); } JavaScript Make elements, like images, appear on screen with the document object’s createElement method Add the element to the screen using the appendChild() method <button onclick="show_image (’dog.jpg’, 276,110, ’Stella');"> Display an image! </button> HTML

createElement Demo <body> <h1>Creating Elements</h1> 11/15/2018 1:46 PM createElement Demo <body> <h1>Creating Elements</h1> <p id="demo"></p> <button onclick="show_image (’dog.jpg’,300,400,'Stella');">Display an image! </button> </body> <script> function show_image(src, width, height, alt) { var img = document.createElement("img"); img.src = src; img.width = width; img.height = height; img.alt = alt; // Adds it to the <body> tag document.body.appendChild(img); } </script> © 2014 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Summary 1 JavaScript 6 Updating the Content of Elements 2 Functions and Variables 7 Adding Elements 3 jQuery and Third-Party Libraries 4 Locating and Accessing Elements 5 Listening and Responding to Events