JavaScript Introduction

Similar documents
What is Java Script? Writing to The HTML Document. What Can JavaScript do? CMPT 165: Java Script

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

Introduction to JavaScript

Programming language components

<script type="text/javascript"> script commands </script>

JavaScript Introduction

CHAPTER 6 JAVASCRIPT PART 1

var num1 = prompt("enter a number between 1 and 12","1"); var promptstring = "What is " + num1 + " times 3?"; var num2 = prompt(promptstring);

introjs.notebook March 02, 2014

Q1. What is JavaScript?

Lecture 14. Introduction to JavaScript. Mr. Mubashir Ali Lecturer (Dept. of Computer Science)

Princess Nourah bint Abdulrahman University. Computer Sciences Department

Manju Muralidharan Priya. CS4PM Web Aesthetics and Development WEEK 11

Lesson 1: Writing Your First JavaScript

Just add an HTML comment tag <!-- before the first JavaScript statement, and an end-of comment tag --> after the last JavaScript statement, like this:

CITS1231 Web Technologies. JavaScript

CSC Web Programming. Introduction to JavaScript

Web Programming/Scripting: JavaScript

c122mar413.notebook March 06, 2013

3 The Building Blocks: Data Types, Literals, and Variables

Dynamism and Detection

<form>. input elements. </form>

ORB Education Quality Teaching Resources

Session 6. JavaScript Part 1. Reading

PHP and MySQL for Dynamic Web Sites. Intro Ed Crowley

Session 16. JavaScript Part 1. Reading

JavaScript s role on the Web

CS1046 Lab 4. Timing: This lab should take you 85 to 130 minutes. Objectives: By the end of this lab you should be able to:

Enhancing Web Pages with JavaScript

Exercise 1: Basic HTML and JavaScript

Controlled Assessment Task. Question 1 - Describe how this HTML code produces the form displayed in the browser.

(Refer Slide Time: 01:40)

Programming Lab 1 (JS Hwk 3) Due Thursday, April 28

1. Please, please, please look at the style sheets job aid that I sent to you some time ago in conjunction with this document.

PIC 40A. Lecture 10: JS: Wrapper objects, Input and Output, Control structures, random numbers. Copyright 2011 Jukka Virtanen UCLA 1 04/24/17

CMPT 100 : INTRODUCTION TO

Place User-Defined Functions in the HEAD Section

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

Arrays/Branching Statements Tutorial:

What is PHP? [1] Figure 1 [1]

JS Lab 1: (Due Thurs, April 27)

JavaScript Functions, Objects and Array

The first sample. What is JavaScript?

Loops/Confirm Tutorial:

By the end of this section of the practical, the students should be able to:

c122jan2714.notebook January 27, 2014

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

<script> function yourfirstfunc() { alert("hello World!") } </script>

PHP 5 Introduction. What You Should Already Know. What is PHP? What is a PHP File? What Can PHP Do? Why PHP?

Website Development Komodo Editor and HTML Intro

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

Working with JavaScript

Exercise 1 Using Boolean variables, incorporating JavaScript code into your HTML webpage and using the document object

INFS 2150 Introduction to Web Development and e-commerce Technology. Programming with JavaScript

Objectives. Introduction to JavaScript. Introduction to JavaScript INFS Peter Y. Wu, RMU 1

JavaScript CS 4640 Programming Languages for Web Applications

Bruce Moore Fall 99 Internship September 23, 1999 Supervised by Dr. John P.

Interactive Web Application

Unit Notes. ICAWEB411A Produce basic client-side script for dynamic web pages Topic 1 Introduction to JavaScript

SEEM4570 System Design and Implementation Lecture 03 JavaScript

For loops and Window Objects:

COMP519 Practical 5 JavaScript (1)

Chapter Two Bonus Lesson: JavaDoc

Mobile Site Development

JavaScript. What s wrong with JavaScript?

Adding a new account:

COMS 469: Interactive Media II

UNIT -II. Language-History and Versions Introduction JavaScript in Perspective-

A340 Laboratory Session #5

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2017)

Figure 1 Forms category in the Insert panel. You set up a form by inserting it and configuring options through the Properties panel.

FOR MORE PAPERS LOGON TO

Tutorial 10: Programming with JavaScript

CONVERSION TRACKING PIXEL GUIDE

Introduction to Web Development

Client Side JavaScript and AJAX

JS Tutorial 3: InnerHTML Note: this part is in last week s tutorial as well, but will be included in this week s lab

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

JavaScript Introduction

Software. Programming Languages. Types of Software. Types of Languages. Types of Programming. Software does something

Autoresponder Guide. David Sharpe

Lecture 3: The Basics of JavaScript. Background. Needs for Programming Capability. Origin of JavaScript. Using Client-side JavaScript

INTRODUCTION TO WEB DEVELOPMENT AND HTML. Lecture 13: Intro to JavaScript - Spring 2011

JavaScript!= Java. Intro to JavaScript. What is JavaScript? Intro to JavaScript 4/17/2013 INTRODUCTION TO WEB DEVELOPMENT AND HTML

(try adding using css to add some space between the bottom of the art div and the reset button, this can be done using Margins)

Alpha College of Engineering and Technology. Question Bank

Figure 1 - The password is 'Smith'

Syllabus - July to Sept

Chapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like?

JavaScript. Advanced HTML

Unit Test Plan for the Joomla Menu Manager

Computer Hardware. Java Software Solutions Lewis & Loftus. Key Hardware Components 12/17/2013

JQuery and Javascript

Web Programming Week 2 Semester Byron Fisher 2018

Yup, left blank on purpose. You can use it to draw whatever you want :-)

More cool scripts: Slide Show: What the settimeout() function does:

FOR MORE PAPERS LOGON TO

Project 2: After Image

Programming Basics. INFO/CSE 100, Spring 2005 Fluency in Information Technology.

Table Basics. The structure of an table

Transcription:

JavaScript Introduction What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is usually embedded directly into HTML pages JavaScript is an interpreted language (means that scripts execute without preliminary compilation) What can a JavaScript Do? JavaScript gives HTML designers a programming tool - JavaScript is a scripting language with a very simple syntax. JavaScript can put dynamic text into an HTML page You can use JavaScript to add html code into your html page, e.g., document.write("<h1>" + name + "</h1>") JavaScript can react to events You can make JavaScripts that execute when something happens, like when a page has loaded or when a user clicks on something. JavaScript can read and write HTML elements - A JavaScript can read and change the content of HTML. JavaScript can be used to validate data - A JavaScript can be used to validate form data before it is submitted to a server (e.g., make sure you ve typed in a 7-digit. This saves the server from extra processing JavaScript can be used to detect the visitor's browser - A JavaScript can be used to detect the visitor's browser, and - depending on the browser - load another page specifically designed for that browser JavaScript can be used to create cookies - A JavaScript can be used to store and retrieve information on the visitor's computer How to Put a JavaScript Into an HTML Page <!DOCTYPE html> <title>javascript Guidelines</title> <script> document.write("hello World!"); Note: If we had not entered the <script> tag, the browser would have treated the document.write("hello World!") command as pure text, and just written the entire line on the page. Example Explained To insert a JavaScript into an HTML page, we use the <script> tag. So, the and tells the browser where the JavaScript starts and ends: 1

<script>... The word document.write is a standard JavaScript command for writing output to a page. Document is an object in JavaScript. There are a bunch of objects in JavaScript, like forms, windows, etc. Each object has functions (or actions) associated with it. The document object has a function called write that writes to the document. By entering the document.write command between the <script> and tags, the browser will recognize it as a JavaScript command and execute the code line. In this case the browser will write Hello World! to the page: JavaScript is Case Sensitive JavaScript is case sensitive - therefore watch your capitalization closely when you write JavaScript statements, create or call variables, objects and functions. document.write() IS NOT the same thing as Document.write() IS NOT the same thing as document.write() IS NOT the same thing as Document.Write() JavaScript Code A JavaScript statement is a command to the browser. The purpose of the command is to tell the browser what to do. This JavaScript statement tells the browser to write "Hello Dolly" to the web page: document.write("hello Dolly") JavaScript code (or just JavaScript) is a sequence of JavaScript statements. Each statement is executed by the browser in the order they are written. Example document.write("<h1>this is a header</h1>"); document.write("<p>this is a paragraph</p>"); document.write("<p>this is another paragraph</p>"); JavaScript Variables Variables are "containers" for storing information. A variable can have a short name, like x, or a more descriptive name, like carname. Variables can be used to hold values (x=5 or carname = volvo ) THE VALUE ON THE RIGHT SIDE GOES INTO THE VARIABLE NAME ON THE LEFT!! Rules for JavaScript variable names: Variable names are CASE SENSITIVE 2

(y and Y are two different variables) Variable names MUST NOT contain spaces Variable names must begin with a letter Variable names can only contain letters or numbers or _ No special characters!! Variable names can be anything except words that already belong to javascript (e.g., document, var, write, etc.) Variable names are CASE SENSITIVE Example var firstname; firstname="fred"; document.write(firstname + <br /> ); firstname="wilma"; document.write(firstname + <br /> ); Creating JavaScript Variables Create variables with the var statement: var x; var carname; The variables are empty (they have no values yet). You can assign values to the variables when you create them: var x=5; var carname="volvo"; After the execution of the statements above, the variable x will hold the value 5, and carname will hold the value Volvo. Note: When you assign a text value to a variable, use quotes around the value. You can always change the value inside a variable: x = 7 carname = Mazda Before, x held 5, now it holds 7. carname held Volvo, now the value has been changed to Mazda You can change the values again if you like. 3

Prompt Box A prompt box is an easy way for you to ask user to enter info into your javascript. When a prompt box pops up, the user types something into the box, then clicks either "OK" or "Cancel". Syntax: x = prompt("sometext","defaultvalue"); where: x is a variable, sometext is the question you want to ask, and defaultvalue is the default answer The best way to understand a prompt box is to try it, so in your web page do the following: Example: var x = prompt( What state do you live in?", Delaware") document.write( <p>you currently live in " + x + </p> ) The prompt box deconstructed: var x = prompt( What state do you live in?", Delaware") Start on right side of = Prompt creates a popup box in which the user can type in text. Within the first set of quotes is what shows up as the prompt s text E.g., What state do you live in? In the second set of quotes is the default answer Delaware You don t have to include a default answer Left side of =: a variable (an empty box) named x what is typed in by the user goes into the variable on the left side If the user doesn t type in anything, the default value ( Delaware ) will end up in the variable x. When the user types something into the prompt box, whatever they typed in is stored in the variable x 4

Example2: var num1 = prompt("enter a number between 1 and 6","1") document.write("<h"+num1+"> A lovely header of some sort </h"+num1+">") In this case, the variable num1 holds the number the user typed in. We then put that number into the string being written into the web page. So if the user typed in 3, num1 will hold the value 3, and the line: becomes: document.write("<h"+num1+"> A lovely header of some sort </h"+num1+">") document.write("<h3> A lovely header of some sort </h3>") Example3: var num1 = prompt("enter a number between 1 and 12","1") var promptstring = "What is " + num1 + " times 3?" var num2 = prompt(promptstring) document.write("<p> You think " + num1 + " times 3 is " + num2 + "</p>") In this case, num1 holds the number that the user types in. Say the user typed in the number 5. Then the variable num1 will hold the value 5. The string, What is + num1 + times 3? Becomes What is 5 times 3? So the variable promptstring holds, What is 5 times 3? Now the line, Becomes Var num2 = prompt(promptstring) Var num2 = prompt( What is 5 times 3? ) And whatever the user types in is what the variable num2 will hold. Say the user typed in 17. Then the variable num2 will hold 17. So the line, becomes: document.write("<p> You think " + num1 + " times 3 is " + num2 + "</p>") document.write("<p> You think 5 times 3 is 17</p>") 5