Web Technologies Lecture 7 Synchronous vs. asynchronous.

Slides:



Advertisements
Similar presentations
Building Fast 3rd-Party Webapps O'Reilly Velocity Web Performance and Operations Conference 24 June Lessons.
Advertisements

CS193H: High Performance Web Sites Lecture 19: Vol 2 – Load Scripts Without Blocking Steve Souders Google
CS193H: High Performance Web Sites Lecture 10: Rule 6 – Put Scripts at the Bottom Steve Souders Google
Introduction to JavaScript Module 1 Client Side JS Programming M-GO Sponsored By
AJAX Development Mike Ormond Developer & Platform Group Microsoft Ltd
AJAX asynchronous server-client communication. Test.
Chapter 6 DOJO TOOLKITS. Objectives Discuss XML DOM Discuss JSON Discuss Ajax Response in XML, HTML, JSON, and Other Data Type.
Making AJAX Easy with jQuery Chris Renner Health Systems Analyst Programmer & Informatics Manager VUMC Office of Grants & Contracts Management October.
Interactive Web Application with AJAX
Ajax (Asynchronous JavaScript and XML). AJAX  Enable asynchronous communication between a web client and a server.  A client is not blocked when an.
JavaScript & jQuery the missing manual Chapter 11
Lecture 12 – AJAX SFDV3011 – Advanced Web Development Reference: 1.
XP New Perspectives on XML, 2 nd Edition Tutorial 10 1 WORKING WITH THE DOCUMENT OBJECT MODEL TUTORIAL 10.
JQuery 10/21. Today jQuery Some cool tools around the web JavaScript Libraries Drawing libraries HTML Frameworks Conventions.
Intro to Ajax Fred Stluka Jan 25, /25/2006Intro to AjaxFred Stluka2 What is Ajax? "Asynchronous JavaScript and XML" New name for an old technique:
Node.js - What is Node.js? -
06/10/2015AJAX 1. 2 Introduction All material from AJAX – what is it? Traditional web pages and operation Examples of AJAX use Creating.
Ajax. –Asynchronous JavaScript and XML –Umbrella term for technologies that often: Use client-side scripting for layout and formatting Use less than full.
Don't Let Third Parties Slow You Down Arvind Jain, Michael Kleber Google.
AJAX (also known as: XMLHTTP, Remote Scripting, XMLHttpRequest, etc.) Matt Warden.
Telerik Software Academy ASP.NET Web Forms Telerik Software Academy ASP.NET Web Forms.
Lecture 9: AJAX, Javascript review..  AJAX  Synchronous vs. asynchronous browsing.  Refreshing only “part of a page” from a URL.  Frameworks: Prototype,
Session: 1. © Aptech Ltd. 2Introduction to the Web / Session 1  Explain the evolution of HTML  Explain the page structure used by HTML  List the drawbacks.
AJAX محمد احمدی نیا 2 Of 27 What is AJAX?  AJAX = Asynchronous JavaScript and XML.  AJAX is not a new programming language, but.
Fall 2006 Florida Atlantic University Department of Computer Science & Engineering COP 4814 – Web Services Dr. Roy Levow Part 2 – Ajax Fundamentals.
1 JavaScript
-By V.Gouthaman. INTRODUCTION Ajax (shorthand for asynchronous JavaScript and XML) is a group of interrelated web development techniques used on the client-side.
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
JQuery JavaScript is a powerful language but it is not always easy to work with. jQuery is a JavaScript library that helps with: – HTML document traversal.
JavaScript Introduction.  JavaScript is a scripting language  A scripting language is a lightweight programming language  A JavaScript can be inserted.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
 AJAX – Asynchronous JavaScript and XML  Ajax is used to develop fast dynamic web applications  Allows web pages to be updated asynchronously by transferring.
AJAX Asynchronous JavaScript and XML 1. AJAX Outline What is AJAX? Benefits Real world examples How it works 2.
Rich Internet Applications 3. Client JavaScript. Document Object Model (DOM) The DOM, is an interface that allows scripts or programs to access and manipulate.
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…
AJAX and REST. Slide 2 What is AJAX? It’s an acronym for Asynchronous JavaScript and XML Although requests need not be asynchronous It’s not really a.
Dave Salinas. What is XML? XML stands for eXtensible Markup Language Markup language, like HTML HTML was designed to display data, whereas XML was designed.
AJAX AJAX Asynchronous JavaScript and XML --- MADHAVI
AJAX – Asynchronous JavaScript And XML By Kranthi Kiran Nuthi CIS 764 Kansas State University.
INNOV-2: Build a Better Web Interface Using AJAX Chris Morgan Pandora Software Systems
Web Technology (NCS-504) Prepared By Mr. Abhishek Kesharwani Assistant Professor,UCER Naini,Allahabad.
1 AJAX. AJAX – Whatzit? Asynchronous (content loading)‏ Javascript (logic & control)‏ And XML (request handling)‏
A New Way To Web Applications Development Tin Htut Htut Naing Oo Myanmar Information Technology.
4.1. Manage and maintain JavaScript Update the UI by using JavaScript. Understanding JavaScript and Coding Essentials.
NCCUCS 軟體工程概論 Lecture 5: Ajax, Mashups April 29, 2014.
AJAX. Objectives Understand and apply AJAX Using AJAX in DOJO library.
Open Solutions for a Changing World™ Eddy Kleinjan Copyright 2005, Data Access WordwideNew Techniques for Building Web Applications June 6-9, 2005 Key.
JavaScript and Ajax (Ajax Tutorial)
CSE 154 Lecture 11: AJAx.
Not a Language but a series of techniques
AJAX and REST.
Intro to JavaScript CS 1150 Spring 2017.
XMLHttp Object.
AJAX – Asynchronous JavaScript and XML
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
AJAX.
CSE 154 Lecture 11: AJAx.
JavaScript an introduction.
Web-Technology Lecture 10.
CSE 154 Lecture 22: AJAX.
AJAX Robin Burke ECT 360.
ISC440: Web Programming 2 AJAX
JavaScript & jQuery AJAX.
Secure Web Programming
Chengyu Sun California State University, Los Angeles
JavaScript CS 4640 Programming Languages for Web Applications
Intro to Ajax Fred Stluka Jan 25, 2006.
DR. JOHN ABRAHAM PROFESSOR UTPA
Creating dynamic/interactive web pages
Presentation transcript:

Web Technologies Lecture 7 Synchronous vs. asynchronous

Motivation “If the web is the human body, Javascript tags are like its nerve endings” - – Means through which pages sense, respond, execute, measure, and remember Problem – Tag activity slows down pages 0.1s delay can translate into a 1% drop in customer activity (Amazon) 30% variance in page views based on load time (AOL) Load times are increasingly important in Google and Bing search rankings

Javascript is synchronous Javascript is the #1 cause of slow web pages – Blocking behavior – When it loads nothing else happens Browsers render page elements synchronously – One element cannot load until the one before it has Solution – Load elements in a non blocking manner Asynchronously

Example tags-whats-the-big-deal/

Why is JS not asynchronous? document.write construct – Inserts something into a web page Text, tag, etc. – It expects to alter the page inline, inserting content as the page is loaded Messes the content if loaded asynchronously Why do we still use it? – We cannot get rid of it until the entire stack can guarantee that document.write will not be used

Solutions Element creation – document.createElement(‘div’) InnerHTML – element.innerHTML(‘some content’) Libraries (JQuery library) Iframes – Work only if access to top level page is not needed HTML 5 attributes for the script element – Only work if the script does not use document.write

Load scripts asynchronously Do not wait for scripts to render page Solution – New HTML 5 script attributes defer: script loads after page finished loading async: script loads concurrently with the page Example

Example Javascript blocking code var startNow = new Date(); var pauseFor = 5000; // In milliseconds while (new Date() - startNow < pauseFor) ; HTML code alert("Time's up"); Outcome – The alert pops out after the 5,000ms timeout elapses

Example Javascript blocking code var startNow = new Date(); var pauseFor = 5000; // In milliseconds while (new Date() - startNow < pauseFor) ; HTML code alert("Time's up"); Outcome – The alert pops out before the 5,000ms timeout elapses

Callbacks JS functions are first class objects – Their type is object – They can be stored in variables, passed as arguments, returned from functions, etc. Essence of callbacks – Pass a function as an argument to another function and later execute that passed-in function or even return it to be executed later Most widely used functional programming technique in JavaScript

Callbacks When passing a callback as argument we do not call the function var data = getData( function (data) { alert(“We have ” + data) }); The function will be called somewhere in the body of the function function getData(callback) { var data = … //read the data callback(data); return data; }

Asynchronous data transfer Load data without blocking the interface – Facebook comments – Twitter tweets – Youtube movies Solution – Use XMLHttpRequest object Supported by: Chrome, IE7+, Firefox, Safari, and Opera AJAX (Asynchronous Javascript and XML) – XHTML + CSS + DOM + XML + XMLHttpRequest

Example function loadXMLAsync (filePath) { var req = new XMLHttpRequest(); req.open("GET", filePath, true); req.send(null); req.onreadystatechange = function () { if (req.readyState == 4) { if (req.status == 200) { xml = req.responseXML; processXML (xml); } else alert ("Error loading XML"); } Notes – For local access use req.status == 0 instead of the HTTP code 200 – State: 0 – uninitialized, 1 – loading, 2 – loaded, 3 – interactive, 4 – complete

Processing incoming XMLs DOM – createElement – appendChild function processXML(xml) { var imgs = xml.getElementsByTagName('image'); body = document.getElementsByTagName(“body”); for (var i=0; i<imgs.length; i++) { var image = document.createElement(“img”); image.src = imgs[i].firstChild.nodeValue; //set src attribute body[0].appendChild(image); }

Older IE browsers < IE7 Use ActiveXObject instead if (window.XMLHttpRequest) { req = new XMLHttpRequest(); req.overrideMimeType('text/xml'); } else if (window.ActiveXObject) { //try to get the most modern implementation var list = ["Microsoft.XmlHttp","MSXML2.XmlHttp","MSXML2.XmlHttp.3.0","MSXML2.XmlHttp.4.0"," MSXML2.XmlHttp.5.0"]; var ok = false; var i = 5; while (i >= 0 && ok == false) { try { req = new ActiveXObject(list[i]); ok = true; } catch (e) {} i--; }

Async vs. sync API Rule of thumb If API – requires IO, or – heavy processing (>15ms) expose it asynchronously from the start

Building asynchronous APIs Design APIs to be asynchronous from the start var data = getData() alert(“We have ” + data); Freezes the user interface until data is fetched getData( function (data) { alert(“We have ” + data) }); Designed asynchronously even if the app needs to be async or not in a later stage Use callbacks

What’s next? JQUERY Server side programming Web services Cloud computing