LING 408/508: Programming for Linguists Lecture 11 October 5 th.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Sue Wills July Objects The JavaScript language is completely centered around objects, and because of this, it is known as an Object Oriented Programming.
Intro to JavaScript. JavaScript History Client (generally browser-side) language invented at Netscape under the name LiveScript around 1995 Netscape wanted.
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
Tutorial 10 Programming with JavaScript
Information Technology Center Hany Abdelwahab Computer Specialist.
CS 299 – Web Programming and Design Overview of JavaScript and DOM Instructor: Dr. Fang (Daisy) Tang.
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
JS: Document Object Model (DOM)
Programming with JavaScript (Chapter 10). XP Various things Midterm grades: Friday Winter Career Fair – Thursday, April 28, 2011 (11 am to 3 pm). – MAC.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
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.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
2440: 211 Interactive Web Programming Expressions & Operators.
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
Chapter 3: Data Types and Operators JavaScript - Introductory.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
5.2 DOM (Document Object Model). 2 Motto: To write it, it took three months; to conceive it three minutes; to collect the data in it — all my life. —F.
1 JavaScript in Context. Server-Side Programming.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Tutorial 10 Programming with JavaScript. XP Objectives Learn the history of JavaScript Create a script element Understand basic JavaScript syntax Write.
Tutorial 10 Programming with JavaScript
Done by: Hanadi Muhsen1 Tutorial 1.  Learn the history of JavaScript  Create a script element  Write text to a Web page with JavaScript  Understand.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
1 Javascript Web Technology. 2 Javascript Javascript is a scripting language –There is no need to compile the code before executing JS is a client-side.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
JavaScript, Fourth Edition
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 JavaScript in Context. Server-Side Programming.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Create Element, Remove Child. The Document Tree Document Element Root Element Element Element Element Element Text: HelloWorld Attribute “href”
JS: Document Object Model (DOM) DOM stands for Document Object Model, and allows programmers generic access to: DOM stands for Document Object Model, and.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
REEM ALMOTIRI Information Technology Department Majmaah University.
JavaScript and AJAX 2nd Edition Tutorial 1 Programming with JavaScript.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
PHP using MySQL Database for Web Development (part II)
>> Introduction to JavaScript
Tutorial 10 Programming with JavaScript
Introduction to Scripting
JavaScript Syntax and Semantics
JavaScript an introduction.
Web Systems Development (CSC-215)
Modern JavaScript Develop And Design
Chapter 8 JavaScript: Control Statements, Part 2
WEB PROGRAMMING JavaScript.
PHP Intro/Overview Bird Book pages 1-11,
PHP.
LING 408/508: Computational Techniques for Linguists
Web Programming Language
LING 408/508: Computational Techniques for Linguists
Tutorial 10 Programming with JavaScript
Presentation transcript:

LING 408/508: Programming for Linguists Lecture 11 October 5 th

Topics Web: – HTML:page content and structure – CSS:page presentation – Javascript programming language inside your browser – DOM: Document Object Model allows a Javascript program to read (and modify) your page content

Debugging In Safari: – Preferences > Advanced > Show Develop menu in menu bar

Debugging In the Develop menu: – Allow JavaScript from Smart Search Field javascript: ok in address bar – Show Page Source – Show Web Inspector

Pop-up notification For browser programming: – alert(string) – no print function Example (from address bar): – javascript:alert(4+5) Changing the html document: – document.write(string)overwrites html document – html_element.innerHTML = "string"modifies html_element

Pop-up notification Google Chrome

Firefox

Javascript Javascript: – invented in 1995 as a browser programming language – not the same as Java (also appeared in 1995) Javascript code: –.. – HTML element events: –onclick="js code" –onmouseover="js code" –onkeydown="js code" Reference: code snippets can be placed in.. or..

Variables Declared variables: –var x = 9;// number variable –var name = "String";// string variable – Note: names are case sensitive, no $ like the bash shell, // for in-line comments function local variables: –function f() { – var x = 99; –}–} Not declared: – i.e. just used without declaration – treated as a global variable Strict: prevents use of undeclared variables etc. –"use strict"; quotes for backwards compatibility with older versions Comments: –// until the end of the line –/* … */ multiline (same as CSS)

Strings Use either double quotes or single quotes – escape using backslash, e.g. \", \n, \\ Properties: –"string".length Methods (like a function but object-based): –.indexOf(string) returns position (0…length), -1 if not found –.search(regex) returns position –. replace(regex,string) replaces with string, returns result –.slice(index,index) substring from position to position –.substr(index,length) substring –.toLowerCase() case conversion –.toUpperCase() –.trim() remove whitespace from both ends –. charAt(index) single character at position –. split(separator) returns array, single characters if separator=""

Numbers Recall the discussion in lectures at the beginning of the semester about number representation? – Integers are considered accurate up to 15 digits.

Numbers Pi to 20 decimal places – – (Javascript)

Operators Arithmetic Operators: +, -, *, /, % (mod), ++, -- Assignment Operators: =, +=, -=, *= /=, %= String Operators: +concatenation +=append to string Comparison Operators: == can be made equal (type coercion) !=not equal === same type and equal (no type coercion) !==not equal (no type coercion) >, =, <= &&logical and(Javascript: Boolean true/false) ||logical or !negation alert("string" + 5) produces string5 Note: &, !, ~ (not), ^ (xor) are bitwise operators

Conditionals if-then –if (condition) { … } else if (condition) { … } else { … } switch –switch (expression) { case value: … break; … default: } Idea: compute expression first 2 nd stage: compare computed value with each case

Loops for loop (just like C): – example: for (i = 0; i < 100; i++) { … } for/in loop (object properties): –for (x in object) { … } while loop: – example: while (true) { … } do/while loop (like traditional repeat/until): –do { … } while (condition) loop exit: –break (jump out of loop immediately) –continue (skip rest of current iteration)

Miscellaneous Random number [0,n-1] : –Math.floor(Math.random()*n ) Swapping two variables (normally): –var a; var b; var temp; –temp = a; –a = b; –b = temp; Javascript arithmetic): –var a; var b; –b=a+(a=b)-b; –a = b + (b=a, 0); Arithmetic priority: Set a to the value of b first, (a=b) evaluates to b, then evaluate rest of expression

DOM

Document Object Model (DOM) HTML document Tree representation html: document.documentElement body: document.body

Document Object Model (DOM) Properties for traversing the DOM: e.childNodes – children of element el as an array, e.g. childNodes[0] e.children – element nodes only (excludes text nodes) e.firstChild e.lastChild e.parentNode e.nextSibling e.previousSibling Object properties: e.nodeType – 1 = element, 3 = text e.nodeName – uppercase e.innerHTML – for element nodes – value is html as text – writeable e.nodeValue – for text nodes (null: for element nodes) – writeable

Document Object Model (DOM) New content: document.createElement(tag) – tag = ‘div’, ‘p’ etc. – creates new DOM element document.createTextNode(text) – creates new DOM element of type text For non-HTML elements: document.createElementNS(NS,tag) – NS = Namespace URL identifier – e.g. and tag “rect” (rectangle) Place new_el: e.appendChild(new_el) – new_el is inserted as last child of el e.insertBefore(new_el,next_el) – new_el inserted as previous sibling of next_el – el is common parent e.removeChild(child_el) – child_el is deleted – el is parent e.replaceChild(new_el,child_el) – new_el replaces child_el – el is parent Old way: document.write(text) document.writeln(text) – adds a newline

Document Object Model (DOM) Locating an element: document.getElementById(id) – useful if you have named the document element using the id=‘Name’ property document.getElementsByTagName(tag) – all document elements of type tag – returns an array e.getElementsByTagName(tag) – all elements of type tag under el – returns an array document.getElementsByName(name) – useful for elements that support name=‘Name’ (document|e).getElementsByClassName( class) (document|e).querySelector(query) – example query ‘BODY > UL > LI’ – ‘>’ means immediately dominates – returns first matching element (document|e).querySelectorAll(query) – returns an array