COMP403 Web Design JAVA SCRİPTS Tolgay KARANFİLLER.

Slides:



Advertisements
Similar presentations
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
Advertisements

JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
The Web Warrior Guide to Web Design Technologies
14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,
HTML Forms JavaScript Introduction / Overview Lab Homework #1 CS 183 4/8/2010.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Introduction to Web Site Development Steven Emory Department of Computer Science California State University, Los Angeles Lecture 8: JavaScript I.
Web Page Behavior IS 373—Web Standards Todd Will.
Computer Information System Information System California State University Los Angeles Jongwook Woo CIS 461 Web Development I Javascript Jongwook Woo,
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
JavaScript Switch Statement. Switch JavaScript Switch Statement If you have a lot of conditions, you can use a switch statement instead of an if…elseif…
Introduction to JavaScript. Aim To enable you to write you first JavaScript.
Introduction to scripting
آموزش طراحی وب سایت جلسه دهم – جاوا اسکریپت 1 تدریس طراحی وب برای اطلاعات بیشتر تماس بگیرید تاو شماره تماس: پست الکترونیک :
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Bridges To Computing General Information: This document was created for use in the "Bridges to Computing" project of Brooklyn College. You are invited.
CC1003N Week 6 More CSS and Javascript.. Objectives: ● More CSS Examples ● Javascript – introduction ● Uses of Javascript ● Variables ● Comments ● Control.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
What is Java Script? An extension to HTML. An extension to HTML. Allows authors to incorporate some functionality in their web pages. (without using CGI.
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
1 JavaScript in Context. Server-Side Programming.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
Introduction to JavaScript Basharat Mahmood, Department of Computer Science, CIIT, Islamabad, Pakistan. 1.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
JavaScript - A Web Script Language Fred Durao
LOGO Introduction to Client-Side Scripting and JavaScript CHAPTER 9 Eastern Mediterranean University School of Computing and Technology Department of Information.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
1 JavaScript
JavaScript Programming Unit #1: Introduction. What is Programming?
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Sahar Mosleh California State University San MarcosPage 1 JavaScript Basic.
By Tharith Sriv. To write a web page you use: HHTML (HyperText Markup Language), AASP (Active Server Page), PPHP (HyperText Preprocessor), JJavaScript,
JavaScript Introduction.  JavaScript is a scripting language  A scripting language is a lightweight programming language  A JavaScript can be inserted.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
1 JavaScript in Context. Server-Side Programming.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Pertemuan 5 IT133 Pengembangan Web JavaScript. What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting.
CIS 3.5 Lecture 2.3 "Introduction to JavaScript".
In God we trust Learning Java Script. Java Script JavaScript is the scripting language of the Web! JavaScript is used in millions of Web pages to improve.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
JavaScript Lectures Level 7. What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting.
Web Development JavaScript. Introduction to JavaScript.
Chapter 5: Intro to Scripting CIS 275—Web Application Development for Business I.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
JavaScript JavaScript ( Condition and Loops ). Conditional Statements If Statement If...else Statement if (condition) { code to be executed if condition.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
JavaScript Introduction JavaScript is the most popular scripting language on the internet, and works in all major browsers, such as Internet Explorer,
Introduction to Client-Side Scripting and JavaScript
CHAPTER 10 JAVA SCRIPT.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
My First Web Page document.write("" + Date() + ""); To insert.
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript.
Introduction to Client-Side Scripting and JavaScript
WEB PROGRAMMING JavaScript.
PHP.
My First Web Page document.write("" + Date() + ""); To insert.
T. Jumana Abu Shmais – AOU - Riyadh
JavaScript Defined General Syntax Body vs. Head Variables Math & Logic
CS105 Introduction to Computer Concepts
Tutorial 10: Programming with javascript
Web Programming– UFCFB Lecture 13
CS105 Introduction to Computer Concepts JavaScript
Presentation transcript:

COMP403 Web Design JAVA SCRİPTS Tolgay KARANFİLLER

Table of contents: JavaScript Introduction Hello world! program Example explained Where to write it or ? External.js files Language Basics Comments Variables Assignment operators Arithmetic operators Comparison operators Logical operators if...else statements More about the Language

HTML DOM Nudes Find and access nodes Node information An example HTML DOM Reference JavaScript objects HTML DOM objects Refrences

JavaScript: Introduction: JavaScript is THE scripting language of the Web (a scripting language is a lightweight programming language) JavaScript is an interpreted language (means that scripts execute without preliminary compilation) JavaScript is used in millions of Web pages to add functionality validate forms detect browsers and much more.

JavaScript works in all major browsers, such as Internet Explorer, Mozilla, Firefox, Netscape, and Opera Everyone can use JavaScript without purchasing a license Java and JavaScript are two completely different languages in both concept and design! (Java (developed by Sun Microsystems) is a powerful and much more complex programming language - in the same category as C and C++)

What can a JavaScript do? JavaScript gives HTML designers a programming tool - HTML authors are normally not programmers, but JavaScript is a scripting language with a very simple syntax! Almost anyone can put small "snippets" of code into their HTML pages JavaScript can put dynamic text into an HTML page - A JavaScript statement like this: document.write(" " + name + " ") can write a variable text into an HTML page JavaScript can react to events - A JavaScript can be set to execute when something happens, like when a page has finished loading or when a user clicks on an HTML element JavaScript can read and write HTML elements - A JavaScript can read and change the content of an HTML element

JavaScript can be used to validate data - A JavaScript can be used to validate form data before it is submitted to a server. 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

Hello world! program: Here you can see a simple example of how JavaScript is placed in an HTML document: Example explained: Note that JavaScript codes are written in Red JavaScript is written in a special element name and the attribute defines the language used JavaScript is written in dot syntax model like most of Object Oriented languages document is an object, standing as the document shown in the browser write is a function (method) which can be used by document write takes an input of type string and writes it in the calling object (document in this example) Every line of code in JavaScript is closed by semicolon

The example below shows how to add HTML tags to the JavaScript: document.write(" Hello World! "); Hello World! So, the and tells where the JavaScript starts and ends: The document.write command is a standard JavaScript command for writing output to a page.

Scripts in Scripts to be executed when they are called, or when an event is triggered, are placed in functions. Put your functions in the head section, this way they are all in one place, and they do not interfere with page content. function message() { alert("This alert box was called with the onload event"); }

Scripts in If you don't want your script to be placed inside a function, or if your script should write page content, it should be placed in the body section. document.write("This message is written by JavaScript");

Scripts in and You can place an unlimited number of scripts in your document, so you can have scripts in both the body and the head section. function message() { alert("This alert box was called with the onload event"); } document.write("This message is written by JavaScript");

JavaScript is Case Sensitive Unlike HTML, JavaScript is case sensitive - therefore watch your capitalization closely when you write JavaScript statements, create or call variables, objects and functions.

JavaScript Code JavaScript code (or just JavaScript) is a sequence of JavaScript statements. Each statement is executed by the browser in the sequence they are written. This example will write a heading and two paragraphs to a web page: document.write(" This is a heading "); document.write(" This is a paragraph. "); document.write(" This is another paragraph. ");

JavaScript Blocks JavaScript statements can be grouped together in blocks. Blocks start with a left curly bracket {, and ends with a right curly bracket }. The purpose of a block is to make the sequence of statements execute together. This example will write a heading and two paragraphs to a web page: { document.write(" This is a heading "); document.write(" This is a paragraph. "); document.write(" This is another paragraph. "); }

Comments: Comments can be added to explain the JavaScript, or to make it more readable. There are two types of comments: Single line comments (line starts with // ) Multi line comments (start with /* and ends with */ ) Single line comments can be used to give further explanation for a line of code in the same line, they are also very helpful for preventing execution of a line during development.

// Write a heading document.write(" This is a heading "); // Write two paragraphs: document.write(" This is a paragraph. "); document.write(" This is another paragraph. ");

/* The code below will write one heading and two paragraphs */ document.write(" This is a heading "); document.write(" This is a paragraph. "); document.write(" This is another paragraph. ");

JavaScript Variables As with algebra, JavaScript variables are used to hold values or expressions. A variable can have a short name, like x, or a more descriptive name, like carname. Rules for JavaScript variable names: Variable names are case sensitive (y and Y are two different variables) Variable names must begin with a letter or the underscore character Note: Because JavaScript is case-sensitive, variable names are case-sensitive. var x; var carname; var y = 1; x = 12; carname = "BMW";

var firstname; firstname="Hege"; document.write(firstname); document.write(" "); firstname="Tove"; document.write(firstname); Hege Tove The script above declares a variable, assigns a value to it, displays the value, changes the value, and displays the value again. The script above declares a variable, assigns a value to it, displays the value, changes the value, and displays the value again.

The + Operator Used on Strings The + operator can also be used to add string variables or text values together. To add two or more string variables together, use the + operator. txt1="What a very"; txt2="nice day"; txt3=txt1+txt2; After the execution of the statements above, the variable txt3 contains "What a verynice day". txt1="What a very"; txt2="nice day"; txt3=txt1+" "+txt2; "What a very nice day"

Adding Strings and Numbers The rule is: If you add a number and a string, the result will be a string! x=5+5; document.write(x); document.write(" "); x="5"+"5"; document.write(x); document.write(" "); x=5+"5"; document.write(x); document.write(" "); x="5"+5; document.write(x); document.write(" "); The rule is: If you add a number and a string, the result will be a string The rule is: If you add a number and a string, the result will be a string.

JavaScript If...Else Statements if statement - use this statement to execute some code only if a specified condition is true if...else statement - use this statement to execute some code if the condition is true and another code if the condition is false if...else if....else statement - use this statement to select one of many blocks of code to be executed switch statement - use this statement to select one of many blocks of code to be executed

If Statement Use the if statement to execute some code only if a specified condition is true. Syntax: if (condition) { code to be executed if condition is true }

var d = new Date(); var time = d.getHours(); if (time < 10) { document.write(" Good morning "); } This example demonstrates the If statement. If the time on your browser is less than 10, you will get a "Good morning" greeting.

If...else Statement Use the if....else statement to execute some code if a condition is true and another code if the condition is not true. Syntax: if (condition) { code to be executed if condition is true } else { code to be executed if condition is not true }

var d = new Date(); var time = d.getHours(); if (time < 10) { document.write(" Good morning "); } else { document.write(" Good day "); } This example demonstrates the If...Else statement. If the time on your browser is less than 10, you will get a "Good morning" greeting. Otherwise you will get a "Good day" greeting.

If...else if...else Statement Use the if....else if...else statement to select one of several blocks of code to be executed. Syntax: if (condition1) { code to be executed if condition1 is true } else if (condition2) { code to be executed if condition2 is true } else { code to be executed if condition1 and condition2 are not true }

var d = new Date(); var time = d.getHours(); if (time<10) { document.write(" Good morning "); } else if (time>=10 && time<16) { document.write(" Good day "); } else { document.write(" Hello World! "); } This example demonstrates the if..else if...else statement.

The JavaScript Switch Statement Use the switch statement to select one of many blocks of code to be executed. switch(n) { case 1: execute code block 1 break; case 2: execute code block 2 break; default: code to be executed if n is different from case 1 and 2 }

var d = new Date(); theDay=d.getDay(); switch (theDay) { case 5: document.write(" Finally Friday "); break; case 6: document.write(" Super Saturday "); break; case 0: document.write(" Sleepy Sunday "); break; default: document.write(" I'm really looking forward to this weekend! "); } This JavaScript will generate a different greeting based on what day it is. Note that Sunday=0, Monday=1, Tuesday=2, etc.

JavaScript Popup Boxes Alert Box An alert box is often used if you want to make sure information comes through to the user. When an alert box pops up, the user will have to click "OK" to proceed. alert("sometext");

function show_alert() { alert("Hello! I am an alert box!"); }

Confirm Box A confirm box is often used if you want the user to verify or accept something. When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed. If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false. confirm("sometext");

function show_confirm() { var r=confirm("Press a button!"); if (r==true) { alert("You pressed OK!"); } else { alert("You pressed Cancel!"); }

Prompt Box A prompt box is often used if you want the user to input a value before entering a page. When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value. If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null. prompt("sometext","defaultvalue");

function show_prompt() { var name=prompt("Please enter your name", “ "); if (name!=null && name!="") { document.write("Hello " + name + "! How are you today?"); }

function disp_alert() { alert("Hello again! This is how we" + '\n' + "add line breaks to an alert box!"); }

JavaScript Functions To keep the browser from executing a script when the page loads, you can put your script into a function. A function contains code that will be executed by an event or by a call to the function. You may call a function from anywhere within a page (or even from other pages if the function is embedded in an external.js file). Functions can be defined both in the and in the section of a document. However, to assure that a function is read/loaded by the browser before it is called, it could be wise to put functions in the section. function functionname(var1,var2,...,varX) { some code }

function displaymessage() { alert("Hello World!"); } By pressing the button above, a function will be called. The function will alert a message.

The return Statement The return statement is used to specify the value that is returned from the function. So, functions that are going to return a value must use the return statement. The example below returns the product of two numbers (a and b): function product(a,b) { return a*b; } document.write(product(4,3)); The script in the body section calls a function with two parameters (4 and 3). The function will return the product of these two parameters.

JavaScript Loops Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform a task like this. In JavaScript, there are two different kind of loops: for - loops through a block of code a specified number of times while - loops through a block of code while a specified condition is true

The for Loop The for loop is used when you know in advance how many times the script should run. for (var=startvalue;var<=endvalue;var=var+increment) { code to be executed }

for (i = 0; i <= 5; i++) { document.write("The number is " + i); document.write(" "); } Explanation: This for loop starts with i=0. As long as i is less than, or equal to 5, the loop will continue to run. i will increase by 1 each time the loop runs. The number is 0 The number is 1 The number is 2 The number is 3 The number is 4 The number is 5 Explanation: This for loop starts with i=0. As long as i is less than, or equal to 5, the loop will continue to run. i will increase by 1 each time the loop runs.

The while Loop The while loop loops through a block of code while a specified condition is true. while (var<=endvalue) { code to be executed }

i=0; while (i<=5) { document.write("The number is " + i); document.write(" "); i++; } Explanation: i is equal to 0. While i is less than, or equal to, 5, the loop will continue to run. i will increase by 1 each time the loop runs. The number is 0 The number is 1 The number is 2 The number is 3 The number is 4 The number is 5 Explanation: i is equal to 0. While i is less than, or equal to, 5, the loop will continue to run. i will increase by 1 each time the loop runs.

The do...while Loop The do...while loop is a variant of the while loop. This loop will execute the block of code ONCE, and then it will repeat the loop as long as the specified condition is true. do { code to be executed } while (var<=endvalue);

i = 0; do { document.write("The number is " + i); document.write(" "); i++; } while (i <= 5) Explanation: i equal to 0. The loop will run i will increase by 1 each time the loop runs. While i is less than, or equal to, 5, the loop will continue to run. The number is 0 The number is 1 The number is 2 The number is 3 The number is 4 The number is 5 Explanation: i equal to 0. The loop will run i will increase by 1 each time the loop runs. While i is less than, or equal to, 5, the loop will continue to run.

The break Statement The break statement will break the loop and continue executing the code that follows after the loop (if any). var i=0; for (i=0;i<=10;i++) { if (i==3) { break; } document.write("The number is " + i); document.write(" "); } Explanation: The loop will break when i=3. The number is 0 The number is 1 The number is 2 Explanation: The loop will break when i=3.

The continue Statement The continue statement will break the current loop and continue with the next value. var i=0; for (i=0;i<=10;i++) { if (i==3) { continue; } document.write("The number is " + i); document.write(" "); } Explanation: The loop will break the current loop and continue with the next value when i=3. The number is 0 The number is 1 The number is 2 The number is 4 The number is 5 The number is 6 The number is 7 The number is 8 The number is 9 The number is 10 Explanation: The loop will break the current loop and continue with the next value when i=3.