JavaScript – Part I Mendelsohn.

Slides:



Advertisements
Similar presentations
Introduction to JavaScript
Advertisements

Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
The Web Warrior Guide to Web Design Technologies
JavaScript- Introduction. What it is and what it does? What it is? It is NOT Java It is NOT Server-side programming Users can see code It is a client-side.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
Escaping Characters document.write(). Learning Objectives By the end of this lecture, you should be able to: – Recognize some of the characters that can.
Introduction to JavaScript. Aim To enable you to write you first JavaScript.
Introduction to scripting
Javascript and the Web Whys and Hows of Javascript.
CS346 - Javascript 1, 21 Module 1 Introduction to JavaScript CS346.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Client Side Programming with JavaScript Why use client side programming? Web sides built on CGI programs can rapidly become overly complicated to maintain,
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Introduction.  The scripting language most often used for client-side web development.  Influenced by many programming languages, easier for nonprogrammers.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
Introduction to JavaScript CS101 Introduction to Computing.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
Introduction to.
Module 1 Introduction to JavaScript
Moving away from alert() Using innerHTML Using an empty div section
Unit M Programming Web Pages with
Chapter 6 JavaScript: Introduction to Scripting
>> JavaScript: Location, Functions and Objects
Concatenation Comments
JavaScript Wrap-up.
JavaScript is a programming language designed for Web pages.
Donna J. Kain, Clarkson University
Web Development & Design Foundations with HTML5 7th Edition
Introduction to Scripting
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript Part 2 Organizing JavaScript Code into Functions
JavaScript.
Intro to PHP & Variables
Introduction to JavaScript
Introduction to JavaScript
JavaScript Introduction
A second look at JavaScript
Number and String Operations
WEB PROGRAMMING JavaScript.
Introduction to JavaScript
T. Jumana Abu Shmais – AOU - Riyadh
Introduction to JavaScript
Tutorial 10: Programming with javascript
JavaScript Basics What is JavaScript?
Java Script Siddharth Srivastava.
JavaScript is a scripting language designed for Web pages by Netscape.
An Introduction to JavaScript
Introduction to Programming and JavaScript
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Introduction to JavaScript
Web Programming and Design
A Few Notes on JavaScript
JavaScript Part 2 Organizing JavaScript Code into Functions
Introduction to scripting
Adios alert() Using innerHTML Using an empty section
Presentation transcript:

JavaScript – Part I Mendelsohn

What is JavaScript? There are many scripting languages that can be used for working with web pages, but JS is the most widely used. It provides a set of instructions for the computer (via the browser) to follow. Do a calculation Change an image Store some information

JavaScript is not Java One is a scripting language, the other is a full-fledged programming language. They do share some syntax and a few other things. Java is not easily embedded inside web pages.

Client vs Server Side Scripting For client-side scripting, the computations and other work of the script is executed on the client computer. In order to do so, the script must be sent to the client along with the HTML page. For server-side, the script is executed on the server. For this reason, the script is not sent to the client. (The code remains on the server, and the execution of the code is carried out by the server).

Where to put JS Scripts The <script> tag can be put anywhere inside an HTML page. However, most scripts should be inside the <head> section. Certain displays or controls (discussed later) may sometimes be placed inside the <body> section. Scripts may be placed in a separate file entirely (e.g. some_scripts.js) and referenced from inside your HTML document. Eg: Scripts that you might want to use in several different pages. For example, a rotating ad banner page.

Your first JS <html> <head> <title>First JS</title> <script type="text/javascript"> alert("Hello World"); </script> </head> <body> </body> </html>

Your first JS JS usually goes in the <head> section You have to tell the browser that you are going to be scripting in JS (there are others such as VBScript). You can output HTML text inside the alert(“”) line of code. Note the ; required at the end of nearly ALL lines of JS code. One exception is at the end of a function declaration (later). <html> <head> <title>First JS</title> <script type="text/javascript"> alert("Hello World"); </script> </head> <body> </body> </html>

JS is Case Sensitive <html> <head> <title>First JS</title> <script type="text/javascript"> alert("Hello World"); </script> </head> <body> </body> </html>

Your first JS function: ‘alert’ This is the tool we will use most often in this course to output information in JavaScript. Although it does have its uses, we will tend to avoid document.write Examples using the alert function: alert("Welcome to it130"); alert( 3+4*5 ); alert( Date() ); alert( Math.sqrt(25.0) );

Only Scripts should be inside <script> (i.e. no HTML code) <html> <head> <title>First JS</title> <script type="text/javascript"> <h1>My First JavaScript</h1>  Bad!! alert("Hello World"); </script> </head> <body> </body> </html> However, it IS possible to display markup (HTML) text using a command: document.write()

However, there are ways… <script type="text/javascript"> document.write("<h1>My First JavaScript</h1>"); document.write("Hello World<br/>How are <em>you</em> today?"); </script> <body> Text from inside the ‘body’ section. </body> Note that script itself does NOT display in your browser. Only information that your script SAYS to display is shown. Outputted text inside <head> will appear before code inside <body> If you want the text to appear later, place the <script> tag lower down, somewhere inside the body section

*** Where to place scripts - II While it is possible to place individual lines of script inside the <body> section, we will use the following methods: We will typically organize our scripts by placing them inside of functions. Those functions will be placed inside <head>. We will execute (“invoke”) those functions from inside <body>.

Writing a JS Function All functions must have a name Naming conventions for JS (yup, another one) we will use include: First letter uncapitalized All subsequent words should be capitalized No spaces between words Do NOT use reserved words (p. 111) The function name is followed by parentheses ‘( )’ Later we will learn what can go inside these brackets. The beginning and end of the body of the function must be delineated by braces: { and }.

Function Template function firstFunction( ) { alert("My first try at a function!"); } Recall that while nearly all JS lines must end with a semicolon, this does NOT apply to function declarations. (The ‘declaration’ is the first line where you ‘declare’ the name of the function)

Invoking (ie executing) Functions There are two ways of invoking functions that we will focus on during this course: Via a JavaScript command (discussed later) Via the onclick attribute of an HTML button <input type="button" name="btnDate" value="Print the Date!“ onclick="printDate()" />

Bugs… Debugging is MUCH easier when it is caught early. Remember to resave and refresh CONSTANTLY! Just one small error can cause the entire JS to fail to display.

Executing a script from a form: We will be executing most of our scripts from an HTML form. Let’s go through the process of creating a button on a form that will execute a JS. In this case, we will write a very simple form (one button) that will do nothing but greet the user. The steps are: 1. Create the script 2. Create the button. 3. Connect the button to the script.

The Greeting: I: The Script function greetUser( ) { alert("Hello"); } Where should this function be placed? Inside the <head> section

The Greeting: II: The Button <form name="practiceForm"> <input type="button" value="Greet Me!" /> </form>

The Greeting: III: Connect the button to the script. We will use the ‘onclick’ attribute to invoke the greetUser( ) function. <form name="practiceForm"> <input type="button" value="Greet Me!" onclick="greetUser( )"/> </form> The value of the onclick attribute should be the name of one of the JS functions on your page. Note: No semicolon inside the attribute value

<html> <head> <title>First JS</title> <script type="text/javascript"> function greetUser( ) { alert("Hello..."); } </script> </head> <body> <form name="practiceForm"> <input type="button" value="Greet Me!" onClick="greetUser( )" /> </form> </body> </html>

document.write() command Because a big part of JS is outputting HTML, we must become adept at using the document.write() command. This command outputs whatever is inside the brackets as HTML Literal text that you wish to output must be placed inside quotes Contrast with variables / functions (discussed shortly) which should NOT be in quotes May include HTML markup But certain characters must be “escaped” (discussed shortly) Can have multiple groups of text (known as “strings”) separated by + signs  this becomes important later

Examples: document.write("Hello there!"); document.write("<h2>Hello there!</h2>"); document.write("How are you today?<br />I am fine!");

Escape characters in JS: Sometimes in JS you will want to use characters such as ; (semicolons) or ” (quotation marks) without those characters interfering with your code. For example, suppose you wanted to output a quotation mark inside a document.write statement? If you wrote: document.write("This is a quote: ""); this would not work because the second quotation mark (shown in red) would be viewed by JS as being the closing quote of the document.write statement . Fortunately, there is a way: For several different characters, JS allows you to “escape” them by placing a ‘\’ (back-slash) before the character. This tells JS not to treat the character as part of the script, but rather as part of a string. Observe the same example: document.write("This is a quote: \""); Practice: Write an anchor tag to display a link to the New York Times: <a href=“www.nytimes.com”>NY Times</a> using JS: document.write("<a href=\"www.nytimes.com\">NY Times</a>");

Escape Characters document.write("<h2 style=\"color:green\;\">Hello there!"); Again - Why do we have all of those ‘\’ characters? Answer: Look at the quotes with in the style attribute. The Javascript interpreter is going to view those quotes as being part of the quotations from the original document.write method as mentioned in the previous slides. We need to tell the JS interpreter that these are not Javascript quotes and should not be viewed as such. So whenever you are attempting to output a symbol that resembles a key JS character (quotes, semicolons, parentheses, and others we’ll discuss later), we must precede that character the with escape symbol: \ This is the same concept as using the character entities in HTML for characters such as ‘<‘ or ‘>’ or ‘/’ etc.

(Hiding from browsers that can’t display JS) Some browsers are unable to display JS Older browsers Turned off for security reasons Certain PDAs, Tablets are unable to process JS In this case, your JS may cause the page to load improperly or not at all. For this reason, we place the JS inside a special XHTML section call ‘CDATA’. Essentially, you are placing your scripts in a section in which, should the browser be unable to process the script, it will simply ignore it. Here is an example – note that the CDATA line goes after the ‘script’ tag. However all of the actualy JS code is inside the CDATA tag.: <script type="text/javascript"> //<![CDATA[ function doStuff() { alert("Hello"); } ]]> </script>

(The Document Model)