JavaScript DOM and CSS What we should learn in this lesson –What is DOM-The JavaScript Document Object Module, its architectures, methods.. –Dynamic Website.

Slides:



Advertisements
Similar presentations
Sue Wills July Objects The JavaScript language is completely centered around objects, and because of this, it is known as an Object Oriented Programming.
Advertisements

Programming Club IIT Kanpur. Work environment Before you begin coding,always set up your work environment to your needs IDE Notepad++ Sublime Text 2.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
HTML Overview - Cascading Style Sheets (CSS). Before We Begin Make a copy of one of your HTML file you have previously created Make a copy of one of your.
HTML – A Brief introduction Title of page This is my first homepage. This text is bold  The first tag in your HTML document is. This tag tells your browser.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Lesson 12- Unit L Programming Web Pages with JavaScript.
The Web Warrior Guide to Web Design Technologies
Introduction to Cascading Style Sheets (CSS) Module 2: HTML Basics LESSON 4.
What we learned in last lesson How to add JavaScript to your HTML pages How to use dialog boxes and variables to store and utilize user input How to write.
Web Workshop: CSS Objectives: - “What is CSS?” - Structure of CSS - How to use CSS in your webpage.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Introduction to JavaScript for Python Programmers
JQuery CS 268. What is jQuery? From their web site:
© 2012 Adobe Systems Incorporated. All Rights Reserved. LEARNING THE LANGUAGE OF THE WEB INTRODUCTION TO HTML AND CSS.
Javascript and the Web Whys and Hows of Javascript.
DHTML. What is DHTML?  DHTML is the combination of several built-in browser features in fourth generation browsers that enable a web page to be more.
HTML DOM.  The HTML DOM defines a standard way for accessing and manipulating HTML documents.  The DOM presents an HTML document as a tree- structure.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Applied Software for Internet Engineers by Dr Zhanfang ZHAO Room: T409 Tel: Study hours: 150hrs, 24 hrs lecture/Tutorials,
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
Bridges To Computing General Information: This document was created for use in the "Bridges to Computing" project of Brooklyn College. You are invited.
The Document Object Model. Goals Understand how a JavaScript can communicate with the web page in which it “lives.” Understand how to use dot notation.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
UFCEWT-20-3 Advanced Topics in Web Development Lecture 4 : JavaScript, Browsers & the HTML DOM-API.
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
 HTML stands for Hyper Text Mark-up Language. The coding language used to create documents for the World Wide Web  HTML is composed of tags. HTML tags.
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
Cascading Style Sheets by Pavlovic Nenad by. Presentation Contents  What is CSS?  Why CSS?  Types of Style Sheets  Style Sheets Syntax  Box Formatting.
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.
Cascading Style Sheets CSS by Pavlovic Nenad by. 2Cascading Style Sheets Presentation Contents What are CSS? What are CSS? History of CSS History of CSS.
Cascading Style Sheets Orientation Learning Web Design: Chapter 11.
Cascading Style Sheets Dave Edsall IAGenWeb County Coordinator’s Conference June 30, 2007.
Client Side Programming with JavaScript Why use client side programming? Web sides built on CGI programs can rapidly become overly complicated to maintain,
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 7.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing.
JavaScript, jQuery, and Mashups Incorporating JavaScript, jQuery, and other Mashups into existing pages.
DOM Animation Changing the DOM objects over time.
1 JavaScript
Advanced Web Development Instructor: Thomas Bombach.
Web Development 101 Presented by John Valance
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Cascading Style Sheets CSS.  Standard defined by the W3C  CSS1 (released 1996) 50 properties  CSS2 (released 1998) 150 properties (positioning)  CSS3.
Introduction to CSS. Why CSS? CSS Provides Efficiency in Design and Updates CSS relatively easy to use Can give you more flexibility and control Faster.
Jaana Holvikivi 1 Introduction to Javascript Jaana Holvikivi Metropolia.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
CISW CRC - Fishman 2014 / 2015 PRESENTATION 4b: CISW 400 (Online) – Client-side Scripting for the Internet Cosumnes River College Fishman – 2015.
IN THIS LESSON, WE WILL BECOME FAMILIAR WITH HTML AND BEGIN CREATING A WEB PAGE IN YOUR COMPUTER HTML – the foundation.
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:
JavaScript 101 Lesson 6: Introduction to Functions.
XML DOM Week 11 Web site:
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
JavaScript and Ajax (JavaScript Dynamic HTML (DHTML)) Week 7 Web site:
CSCI 3100 Tutorial 5 JavaScript & Ajax Jichuan Zeng Department of Computer Science and Engineering The Chinese University of Hong.
COM621: Advanced Interactive Web Development Lecture 6 – JavaScript (cont.)
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
Introduction to.
Applied Component I Unit II Introduction of java-script
CSS Layouts: Grouping Elements
Document Object Model (DOM): Objects and Collections
Week 11 Web site: XML DOM Week 11 Web site:
JavaScript is a scripting language designed for Web pages by Netscape.
Web Programming and Design
Web Programming and Design
Web Programming and Design
Presentation transcript:

JavaScript DOM and CSS What we should learn in this lesson –What is DOM-The JavaScript Document Object Module, its architectures, methods.. –Dynamic Website structure: Placeholders and separated JavaScript files –Using DOM method to generate HTML structures –Formatting with CSS

One JavaScript DOM exmple Why the following codes works? – document.the_image.src=‘ ~zhaoza/sun.gif '; –document.writeln(“ test ”); DOM is the way JavaScript describes Web pages, and it lies at the heart of all JavaScript programming

The JavaScript Document Object Model JavaScript is an Object-oriented programming language. The main idea of OOP is that information is organized in terms of objects. JavaScript is wonderful because it comes with a built-in library of objects. For example, a window is an object. Object properties Objects have properties that describe them. For example the windows object has properties such as its name, the words in its status bar, the URL of the document inside the window, and the window’s document. Objects methods In addition to properties, objects also have methods. Methods are the actions an object knows how to take. For example, Windows know how to open other Windows: window.open("URL,""name,""features"). This tells JavaScript to use the open method of the Window object to open a new window. For the windows object, we already learn its methods such as: open(), alert(), prompt(), confirm() etc. Here introduces two more, focus and blur, The focus method moves a window that's behind other windows to the front. The blur method does the reverse — it moves a window behind other windows.

One neat thing about objects is that the properties of an object can be objects too. For example, windows have a property called document that refers to the actual HTML document in the window. This document property is itself an object that has properties and methods of its own. We saw an example of this when we talked about image swapping. Go back to the last lesson, we learned that you can do an image swap like this: change That long string, window.document.the_image.src='button_d.gif', translates into: "Find the document property of the window, find the_image property of the document, find the src property of the_image, and set it to button_d.gif.“ It may seem like a lot of detail to keep track of. The JavaScript Document Object Model describes a small hierarchy of objects. Here it is:

JavaScript’s objects The top box of the diagram represents your browser window. Following the line from that box down, you'll see it connects to seven more boxes. These are the properties of the browser window. The sixth box here, "document," represents the contents of your window. If you follow the little line leading out of the document box, you'll see it connects to six more boxes. These are the properties of the document object.

Introduction to DOM The Document Object Model (DOM) is the model that describes how all elements in an HTML page, like input fields, images, paragraphs etc., are related to the topmost structure: the document itself. By calling the element by its proper DOM name, we can influence it. The purpose of the DOM: –It has been developed by the W3C to provide any programming language with access to each part of an XML document. What are the nodes of DOMnodes How you can walk the DOM tree from node to nodewalk the DOM tree How to get a specific node and how to change its value or attributes.getchange How to create nodes yourself, the ultimate purpose of the new DOM.create nodes

What are the nodes of DOMnodes In the Level 1 DOM, each object, whatever it may be exactly, is a Node. So if you do This is a paragraph you have created two nodes: an element P and a text node with content 'This is a paragraph'. The text node is inside the element, so it is considered a child node of the element. Conversely, the element is considered the parent node of the text node. If you do This is a paragraph the element node P has two children, one of which has a child of its own: The element node P also has its own parent, this is usually the document, sometimes another element like a DIV. So the whole HTML document can be seen as a tree consisting of a lot of nodes, most of them having child nodes (and these, too, can have children).

DOM tree structure

Walking through the DOM tree Knowing the exact structure of the DOM tree, you can walk through it in search of the element you want to influence. For instance, assume the element node P has been stored in the variable x Then if we want to access the BODY we do x.parentNode We take the parent node of x and do something with it. To reach the B node: x.childNodes[1] childNodes is an array that contains all children of the node x. Of course numbering starts at zero, so childNodes[0] is the text node 'This is a' and childNodes[1] is the element node B. –Two special cases: x.firstChild accesses the first child of x (the text node), while x.lastChild accesses the last child of x (the element node B). So supposing the P is the first child of the body, which in turn is the first child of the document, you can reach the element node B by either of these commands: document.firstChild.firstChild.lastChild; document.firstChild.childNodes[0].lastChild; document.firstChild.childNodes[0].childNodes[1];

Dynamic Website structureDynamic Website structure: Placeholders and separated JavaScript files AJAX Foundations: JavaScript and DOM I love you! // declaring new variables var date = new Date(); var hour = date.getHours(); // demonstrating the if statement if (hour >= 22 || hour <= 5) document.write("Goodnight, world!"); else document.write("Hello, world!");

Dynamic Website structureDynamic Website structure: Placeholders and separated JavaScript files Morejsdom.html: AJAX Foundations: More JavaScript and DOM Hello Dude! Here's a cool list of colors for you: Morejsdos.js function process() { // Create the HTML code var string; string = " " + " Black " + " Orange " + " Pink " + " "; // obtain a reference to the element on the page myDiv = document.getElementById("myDivEleme nt"); // add content to the element myDiv.innerHTML = string; }

Dynamic Website structure: Placeholders and separated JavaScript files Access the named element programmatically from the JavaScript function. Having the JavaScript code execute after the HTML template is loaded, so you can access the element (no HTML elements are accessible from JavaScript code that executes referenced from the element). You will do that by calling JavaScript code from the element's onload event. Group the JavaScript code in a function for easier code handling.

Using DOM method Using DOM method to generate HTML structures Evenmorejs.html: AJAX Foundations: Even More JavaScript and DOM Evenmoreisdos.js: function process() { // create the first text node oHello = document.createTextNode ("Hello Dude! Here's a cool list of colors for you:"); // create the element oUl = document.createElement("ul") // create the first element and add a text node to it oLiBlack = document.createElement("li"); oBlack = document.createTextNode("Black"); oLiBlack.appendChild(oBlack); // create the second element and add a text node to it oLiOrange = document.createElement("li"); oOrange = document.createTextNode("Orange"); oLiOrange.appendChild(oOrange); // create the third element and add a text node to it oLiPink = document.createElement("li"); oPink = document.createTextNode("Pink"); oLiPink.appendChild(oPink); // add the elements as children to the element oUl.appendChild(oLiBlack); oUl.appendChild(oLiOrange); oUl.appendChild(oLiPink); // obtain a reference to the element on the page myDiv = document.getElementById("myDivElement"); // add content to the element myDiv.appendChild(oHello); myDiv.appendChild(oUl); }

Formatting with CSS AJAX Foundations: CSS Product Name Airplane Big car

Formatting with CSS / Change table style to style 1 function setStyle1() { // obtain references to HTML elements oTable = document.getElementById("table"); oTableHead = document.getElementById("tableHead"); oTableFirstLine = document.getElementById("tableFirstLine"); oTableSecondLine = document.getElementById("tableSecondLine"); // set styles oTable.className = "Table1"; oTableHead.className = "TableHead1"; oTableFirstLine.className = "TableContent1"; oTableSecondLine.className = "TableContent1"; } // Change table style to style 2 function setStyle2() { // obtain references to HTML elements oTable = document.getElementById("table"); oTableHead = document.getElementById("tableHead"); oTableFirstLine = document.getElementById("tableFirstLine"); oTableSecondLine = document.getElementById("tableSecondLine"); // set styles oTable.className = "Table2"; oTableHead.className = "TableHead2"; oTableFirstLine.className = "TableContent2"; oTableSecondLine.className = "TableContent2"; }.Table1 { border: DarkGreen 1px solid; background-color: LightGreen; }.TableHead1 { font-family: Verdana, Arial; font-weight: bold; font-size: 10pt; }.TableContent1 { font-family: Verdana, Arial; font-size: 10pt; }.Table2 { border: DarkBlue 1px solid; background-color: LightBlue; }.TableHead2 { font-family: Verdana, Arial; font-weight: bold; font-size: 10pt; }.TableContent2 { font-family: Verdana, Arial; font-size: 10pt; }

Communicating between Windows Getting and using a window reference <!-- hide me // open a new window, In order to communicate with a window using // JavaScript, you need a reference to that window. var new_window = window.open("hello.html","html_name","width=200,height=200"); //This opens a little window and assigns the variable new_window //to refer to it. And then blur the new window new_window.blur(); // show me --> A new window has been opened and moved to the background. Bring it forward Put it backward

Messing with the DOM in other Windows Image Remote <!-- hide me //opens a new window and assigns the variable display_window to that window var display_window = window.open("slide_show_main.html","display_window"); window.focus(); // stop hiding -->

slide_show_main.html Remote Image Swapping

Getting Framed In JavaScript, Frames are treated just like windows Main.html: Using Frames with JavaScript Frames_example_controls.html: Frames Example Controls <a href="#" onClick="top.target_frame.document.writeln('Monkey do! ');">Monkey see

Some other JavaScript Syntax Loops –While loops: while (some test is true) { do the stuff inside the curly braces } –For loops: for (initial value; test; increment) { do this stuff; } Arrays- Arrays are lists. –An example to create an array of colors: var colors = new Array("red","blue","green"); good thing about arrays is that elements of an array can be accessed by number. The first element is number 0 and can be accessed like this: var the_element = colors[0]; After you execute this line of JavaScript, the variable the_element will hold the string "red."

An example for Loops and Arrays(slide show) URL Slideshow <!-- hide me var url_names = new Array("hits.org","awaken.org","bianca.com"); var a_url; for (loop = 0; loop < url_names.length; loop++) { // make the name of a url, for example a_url = " + url_names[loop]; // open a window var new_window=open(a_url,"new_window","width=300,height=300"); // wait for the click alert("Hit OK for the next site"); } // show me -->

Functions(Timer.html) Function with No Parameters <!-- hide me function announceTime() { //get the date, the hour, minutes, and seconds var the_date = new Date(); var the_hour = the_date.getHours(); var the_minute = the_date.getMinutes(); var the_second = the_date.getSeconds(); //put together the string and alert with it var the_time = the_hour + ":" + the_minute + ":" + the_second; alert("The time is now: " + the_time); } // show me --> time!