JavaScript and AJAX Jonathan Foss University of Warwick

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
Fawaz Ghali AJAX: Web Programming's Toy.
Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
Javascript Introduction Norman White Material is from w3schools.com Go there to run examples interactively.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
The Web Warrior Guide to Web Design Technologies
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Introduction to Scripting.
JavaScript Forms Form Validation Cookies CGI Programs.
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
Tutorial 10 Programming with JavaScript
CS 299 – Web Programming and Design Overview of JavaScript and DOM Instructor: Dr. Fang (Daisy) Tang.
Introduction to JavaScript. Aim To enable you to write you first JavaScript.
Agenda What is AJAX? What is jQuery? Demonstration/Tutorial Resources Q&A.
JQuery CS 268. What is jQuery? From their web site:
4.1 JavaScript Introduction
JavaScript: Control Structures September 27, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
JavaScript Teppo Räisänen LIIKE/OAMK HTML, CSS, JavaScript HTML defines the structure CSS defines the layout JavaScript is used for scripting It.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Dynamic Action with Macromedia Dreamweaver MX Barry Sosinsky Valda Hilley.
JavaScript, Fourth Edition Chapter 12 Updating Web Pages with AJAX.
Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
D2L Notes Be sure to submit your link in the dropbox provided on D2L You can just upload an empty text file if a file upload is required Do not use D2L.
PHP By Jonathan Foss.
Week 9 PHP Cookies and Session Introduction to JavaScript.
CITS1231 Web Technologies JavaScript and Document Object Model.
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
Chapter 8 Cookies And Security JavaScript, Third Edition.
JavaScript, Fourth Edition
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Tutorial 10 Programming with JavaScript
Regular Expression (continue) and Cookies. Quick Review What letter values would be included for the following variable, which will be used for validation.
Chapter 6 Server-side Programming: Java Servlets
Where does PHP code get executed?. Where does JavaScript get executed?
Lecture 9: AJAX, Javascript review..  AJAX  Synchronous vs. asynchronous browsing.  Refreshing only “part of a page” from a URL.  Frameworks: Prototype,
JavaScript Syntax, how to use it in a HTML document
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
JavaScript Introduction. Slide 2 Lecture Overview JavaScript background The purpose of JavaScript A first JavaScript example Introduction to getElementById.
 A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests.
Javascript Overview. What is Javascript? May be one of the most popular programming languages ever Runs in the browser, not on the server All modern browsers.
CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…
Understanding JavaScript and Coding Essentials Lesson 8.
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
CGS 3066: Web Programming and Design Spring 2016 Introduction to JavaScript.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
1/7/2016www.infocampus.co.in1. 1/7/2016www.infocampus.co.in2 Web Development training gives you and all-round training in both the design and the development.
HTML Introduction HTML Editors HTML Basic HTML Elements HTML Attributes HTML Headings HTML Paragraphs HTML Formatting HTML Links HTML Head HTML CSS HTML.
Introduction to.
Web Technologies Computing Science Thompson Rivers University
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Tutorial 10 Programming with JavaScript
19.10 Using Cookies A cookie is a piece of information that’s stored by a server in a text file on a client’s computer to maintain information about.
Web Development & Design Foundations with HTML5 7th Edition
JavaScript Introduction
JavaScript an introduction.
Web Systems Development (CSC-215)
DHTML Javascript Internet Technology.
WEB PROGRAMMING JavaScript.
DHTML Javascript Internet Technology.
CIS 133 mashup Javascript, jQuery and XML
Tutorial 10: Programming with javascript
Web Technologies Computing Science Thompson Rivers University
Presentation transcript:

JavaScript and AJAX Jonathan Foss University of Warwick

Overview JavaScript Cookies DOM XML AJAX JavaScript Frameworks

JavaScript Inserted into HTML pages Processed client-side (by the browser) HTML elements dynamically changed

JavaScript Variables Variables are dynamic, weakly typed: var x = ‘Hello’;//x is a string var y = 2011; //y is an integer

JavaScript Functions Functions do not require variable types function firstFunction(a, b){ } Or a return type function doubleMyNumber(a){ return a*2; }

Basic JavaScript Outputs To display a message box: alert(‘Hello’); Or you could manipulate an HTML control text1.value = "Hello";

JavaScript Events HTML elements call JavaScript events For example, the body element has onload function init(){ alert(“Page Loaded”); }

JavaScript Events Many elements have onclick function linkClicked(){ alert(“Clicked”); } Click

Calling Methods from URLs Note that: Click Could also be written as: Click

Control Structures Most control structures are similar to Java var a = new Array(“first”, “second”, “third”); var i = 0; for (i = 0; i < a.length; i++){ alert(a[i]); } while(i > 0){ alert(i); i--; }

Document Object Model JavaScript allows you to navigate between the elements of a page using the DOM For example: Hello var mytxt = document.getElementById(‘mytxt’); mytxt.value = “Hello ” + user;

Browser implementations As with HTML/CSS displays some browsers are inconsistent If something works in Firefox, it might not work in Internet Explorer The best way of ensuring browser compatibility is to use a JavaScript framework, such as jQueryjQuery

jQuery Example The previous example document.getElementById(‘mytxt’).value = “Hello ” + user; Could be expressed in jQuery as $(“#mytxt”).val(“Hello ” + user);

Cookies The document.cookie property allows variable values to be stored in a ‘cookie’ Cookie file stored on the client’s computer The website can read this file next time the user visits the site For security reasons, cookies can only be read by the domain that stored it.

Cookies: Writing document.cookie needs to contain keys and values document.cookie is a string: document.cookie = “name=sam;” You also need an expiration date, or it will get destroyed when browser is closed document.cookie = “name=sam; expires=Fri, 18 Feb :00:00 UTC;”

Cookies: Writing Multiple key/values can be specified by assigning to the variable twice: document.cookie = "name=sam;expires=Fri, 18 Feb :00:00 UTC;"; document.cookie = "favcolour=orange;expires=Fri, 18 Feb :00:00 UTC;"; To delete a cookie, assign an expiry date in the past

Cookies: Reading Access document.cookie as a string “name=sam; favcolour=orange” var c = document.cookie.split(';'); for(var i = 0; i < c.length; i++){ var key = c[i].split('=')[0]; var value = c[i].split('=')[1]; if(key == 'name') alert('Hello again ' + value); }

AJAX Interacting with web servers using AJAX

AJAX Allows JS pages to retrieve information without reloading the whole page Uses –Form validation –Auto Complete –Title -> Detail views –Refreshing (e.g. Inbox, RSS Feeds)

Introduction to XML XML allows information to be structured Structure is similar to XML, with elements (e.g. ) and attributes (e.g. id="box1") You can define your own type of XML, with your own tags Common uses of XML include: RSSXHTML Office Open XMLSOAP

XML Syntax XML must be well formed. For instance: Hello World is valid HTML, but invalid XHTML – tags must be closed: Hello World Special Characters must also be defined, and escaped correctly (e.g. & to &)

AJAX Architecture

AJAX Libraries Browsers implement AJAX differently Easiest to use a library Our examples use jQuery, which caters for all browsers, making it easier to write compatible code

jQuery AJAX jQuery functions can be called using $(sel).load(url, [data], [callbackfunction]); Where sel selects the element in the HTML page For instance $("#text") selects the HTML element which has id="text" $("#text").load("test.php"); puts the result of test.php into the text element

jQuery AJAX You can also specify parameters such as $("#text").load("test.php", {a: 4, b: 2}); is equivalent to loading "test.php?a=4&b=2" For security reasons, the page you load needs to be on the same domain You can also use $.get(...) or $.post(...) These functions take a callback function

jQuery AJAX The previous example could be written using $.get: $.get("test.php", {a: 4, b: 2}, function(data){ $("#text").html(data); }); This allows you to process the data in the function, without immediately displaying it

JavaScript Frameworks jQuery: Ext-Core: Prototype: Dojo: And there are many more... Choose one that best fits your needs

Conclusion JavaScript can create interactive, dynamic pages Cookies store variables between sessions AJAX used for interactively loading elements of page from other scripts