JavaScript and Ajax (JavaScript window object) Week 6 Web site:

Slides:



Advertisements
Similar presentations
Chapter 7 JavaScript: Introduction to Scripting. Outline Simple Programs Objects and Variables Obtaining User Input with prompt Dialogs – –Dynamic Welcome.
Advertisements

Tutorial 5 Windows and Frames Section A - Working with Windows.
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
Javascript Document Object Model. How to use JavaScript  JavaScript can be embedded into your html pages in a couple of ways  in elements in both and.
The Browser Object Model (BOM) The API of JavaScript’s browser host environment SE-2840 Dr. Mark L. Hornick 1.
JavaScript, Third Edition
Create a Web Site with Frames
Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.
XP Tutorial 5New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Designing a Web Site with Frames Using Frames to Display Multiple Web Pages Tutorial.
Introduction to scripting
CST JavaScript Validating Form Data with JavaScript.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Event and Event Handling.
JavaScript - Document Object Model. Bhawna Mallick 2 Unit Plan What is DOM and its use? Hierarchy of DOM objects. Reflection of these objects in an HTML.
Server vs Client-side validation. JavaScript JavaScript is an object-based language. JavaScript is based on manipulating objects by modifying an object’s.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
XHTML Introductory1 Forms Chapter 7. XHTML Introductory2 Objectives In this chapter, you will: Study elements Learn about input fields Use the element.
1 CS101 Introduction to Computing Lecture 18 Objects, Properties, Methods (Web Development Lecture 6)
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
Unobtrusive JavaScript
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
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.
CSE 154 LECTURE 19: EVENTS AND TIMERS. Anonymous functions function(parameters) { statements; } JS JavaScript allows you to declare anonymous functions.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
Creating an Animated Web Page
JavaScript, Fourth Edition
Using Client-Side Scripts to Enhance Web Applications 1.
Introduction to JavaScript 41 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 17.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
Tutorial 5 Windows and Frames Section B - Working with Frames and Other Objects Go to Other Objects.
Chapter 5: Windows and Frames
XP Tutorial 6 New Perspectives on JavaScript, Comprehensive1 Working with Windows and Frames Enhancing a Web Site with Interactive Windows.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Window Object.
JavaScript - A Web Script Language Fred Durao
JS Basics 1 Lecture JavaScript - Basics. JS Basics 2 What is JavaScript JavaScript is a “simple”, interpreted, programming language with elementary object-
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Scott Marino MSMIS Summer Session Web Site Design and Authoring Session 8 Scott Marino.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Introduction to JavaScript Objects, Properties, Methods.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
JavaScript, Fourth Edition Chapter 4 Manipulating the Browser Object Model.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Project 5: Using Pop-Up Windows Essentials for Design JavaScript Level One Michael Brooks.
CISW CRC - Fishman 2014 / 2015 PRESENTATION 4b: CISW 400 (Online) – Client-side Scripting for the Internet Cosumnes River College Fishman – 2015.
Working with the Window Object JavaScript considers the browser window an object, which it calls the window object.
JavaScript and Ajax (JavaScript Functions) Week 5 Web site:
Chapter 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Chapter 5 Validating Form Data with JavaScript
JavaScript and HTML Simple Event Handling 11-May-18.
In this session, you will learn to:
Applied Component I Unit II Introduction of java-script
Using DHTML to Enhance Web Pages
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
WEB PROGRAMMING JavaScript.
Integrating JavaScript and HTML
CNIT 133 Interactive Web Pags – JavaScript and AJAX
JavaScript: Introduction to Scripting
Presentation transcript:

JavaScript and Ajax (JavaScript window object) Week 6 Web site:

Window and Document Objects

The Window Object (Global Object) - Properties Window Properties  document: document object  Location: location object  history: history object (array). history.back(), history.forward(), history.go()  navigator: navigator object (Browser information)  self: Refers to the current window. Equivalent to window  opener: Refers to the window from which a particular window was opened  close: Boolean value, window has been closed or not  defaultStatus: set or return default text in status bar  name: set or return name of window

The Window Object (Global Object) - Methods Window Methods alert(), back(), blur(), clearInterval(), clearTimeout(), close(), confirm(), focus(), forward(), home(), open(), print(), prompt(), setInterval(), setTimeout(), stop()

The Document Object - Properties Document object collection: images[] forms[] elements[] (button, checkbox, hidden, password, radio, reset, select, submit, text, textarea) links[] anchors[] Document object properties:  lastModified  title  URL  location (deprecated since Netscape 6, use URL instead)

The Document Object - Methods Document object methods: close() getElementById() getElementsByName() getElementsByTagname() open() write() writeln()

The Document Object - Properties sample document Object properties document Object properties document.write("lastModified (return last modified date): ", document.lastModified, " "); document.write("title (set or return the title of the document): ", document.title, " "); document.write("URL (return the full URL of the document): ", document.URL, " "); document.write("location (deprecated, use document.URL): ", document.location, " ");

The Location Object The location property of a window (or frame) is a reference to a Location object; it represents the URL of the document currently being displayed in that window. The href property of the Location object is a string that contains the complete text of the URL. The toString() method of the Location object returns the value of the href property, so you can use location.toString() in place of location.href: location.href location.toString() /* this 2 statements are the same */

Loading New Documents Although the location property of a window refers to a Location object, you can assign a string value to the property. When you do this, the browser interprets the string as a URL and attempt to load and display the document at the URL: /* if the browser does not support the document.getElementById function, redirect to a static page. */ if (!document.getElementById) { location = "staticpage.html"; } Historically, assigning a URL to the location property of a window has been the supported technique for loading new pages.

The Location Object The Location object have two methods with related purposes:  The reload() method reloads the currently displayed page from the web server;  The replace() method loads and displays a URL that you specify. When you call this method, the specified URL replaces the current one in the browser’s history list rather than creating a new entry in that history list. Therefore, the Back button does not take the user back to the original document. location.replace(" Finally, the location property of the Window object refers to a Location object. The deprecated location property of the Document object is simply a read-only string (deprecated since Netscape 6, use document.URL instead).  document.location is a synonym for document.URL In most cases, document.location/document.URL is the same as location.href. When there is a server redirect, however, document.location/document.URL contains the URL as loaded, and location.href contains the URL as originally requested.

The History Object The history property of the Window object refers to the History object for the window. For security and privacy reasons, the array elements of the History object are never actually accessible to scripts. Although its array elements are inaccessible, the History object supports three methods:  The back() and forward() methods move backward or forward in a window’s (or frame’s) browsing history, replacing the currently displayed document with a previously viewed one.  The go() method, takes an integer argument and can skip any number of pages forward (for positive arguments) or backward (for negative arguments) in the history list.  history.back()  history.forward()  history.go(+2)  history.go(-3)

Obtaining Window, Screen, and Browser Information The navigator property of a Window object refers to a Navigator object that contains information about the web browser as a whole. In the past, the Navigator object was commonly used by scripts to determine if they were running in Internet Explorer or Netscape. This browser-sniffing approach is replaced by the following capability-testing approach: if (window.addEventListener) { // addEventListener() method supports by Netscape/Mozilla/Firefox } else if (window.attachEvent) { // attachEvent() method supports by IE } else { // old browsers }

The Navigator Object Browser sniffing is sometimes still valuable, however, one such case is when you need to work around a specific bug that exists in a specific version of a specific browser. The Navigator object lets you do this. The Navigator object has five properties that provide version information about the browser that is running:  appName; appVersion; userAgent; appCodeName; platform. The following lines of JavaScript code display each Navigator object property in a dialog box: var browser = "BROWSER INFORMATION: \n"; for (var propname in navigator) { browser += propname + ": " + navigator[propname] + "\n"; } alert(browser);

The Navigator Object (continue…) Client sniffer ( developer/sniffer/browser_type.html) developer/sniffer/browser_type.html /* this module defines an object named “browser” that is easier to use than the “navigator” object */ var browser = { version: parseInt(navigator.appVersion), isNetscape: navigator.appName.indexOf("Netscape") != -1, isMicrosoft: navigator.appName.indexOf("Microsoft") != -1 }; This is one of the reasons that browser sniffing has become less useful and is being superseded by capability testing.

Window Methods (Opening and Manipulating Windows) The Window object defines several methods allow you to open and close windows, control window position and size, request and relinquish keyboard focus, and scroll the contents of a window.

Opening Windows – Window Method open() You can open a new web browser window with the open() method of the Window object. window.open() takes four optional arguments and returns a Window object that represents the newly opened window: window.oprn("URL", "winname", "featureslist", booleanvalue);  URL: URL of the document to display in the new window. If omitted or null or empty string, the window will be empty  Winname: the name of the window  Featureslist: list of features that specify the window size and GUI decorations.  Booleanvalue: specifies whether the URL specified as the first argument should replace the current entry in the window’s browsing history (true) or create a new entry in the window’s browsing history (false), which is the default behavior var w = window.open("smallwin.html", "smallwin", "width=400,height=350,status=yes,resizable=yes"); The return value of the open() method is the Window object that represents the newly created window. The opener property of a window refers to the window from which it was opened. If the window was created by the user, the opener property is null.

Closing Windows – Window Method close() The close() method closes the current window. If you create a Window object w, you can close it with: w.close(); JavaScript code running within that window itself can close it with: window.close(); Note: the explicit use of the window identifier to distinguish the close() method of the Window object from the close() method of the Document object (document.close( )).

Keyboard Focus and Visibility – Window Method focus() and blur() Calling focus( ) requests that the system give keyboard focus to the window Calling blur( ) relinquishes keyboard focus Note: the focus( ) method ensures that the window is visible by moving it to the top of the stacking order.

Simple Dialog Boxes – Window Methods alert() confirm() prompt() The Window object provides three methods for displaying simple dialog boxes to the user. alert( ) displays a message to the user and waits for the user to dismiss the dialog (use for debugging) confirm( ) asks the user to click an OK or Cancel button to confirm or cancel an operation prompt( ) asks the user to enter a string

Error Handling – onerror property/handler The onerror property of a Window object is special. If you assign a function to this property, the function is invoked whenever a JavaScript error occurs in that window: the function you assign becomes an error handler for the window. Three arguments are passed to an error handler when a JavaScript error occurs:  The first argument is a message describing the error  The second argument is a string that contains the URL of the document containing the JavaScript code that caused the error  The third argument is the line number within the document where the error occurred If the onerror handler returns true, it tells the browser that the handler has handled the error and that no further action is necessary.

Error Handling – onerror property/handler (continue…) Define an error handler that handles all errors silently: // don’t bother the user with error messages window.onerror = function() { return true; }

onerror sample window onerror property /* comment out the following code to see the original error message. */ window.onerror = function (message, url, linenum) { alert("There is an error!!"); alert("Error Message: " + message + "\n URL: " + url + "\n Line Number: " + linenum); return true; } Window onerror property/onerror event handler document.write("No error"); document.write("With error here“

Window Synonyms

Targeting Windows sample Window target Some page

Targeting Windows

Window Methods

Window Methods (continue…)

Opening a pop-up Window – Window Method open() [windowObjectName=] [window.] open("URL", "targetName" [, "features"]) windowObjectName: specifies the name of the window object variable. It is used to call methods on the window and access the window properties. URL: specifies the URL of the file to open in the new window. If you want to write to the new window dynamically, do not specify a URL, just provide empty quotes (""). targetName: name to be used in the target attribute of an tag. It can only be alphanumeric or underscore. features: comma-separated list (without spaces) of any of the following options and values.

Opening a pop-up Window

Opening a pop-up Window (continue…)

myWin = window.open(“test.html”,“TestWin”,“height=100,width=100,status”); myWin = window.open(“test.html”,“TestWin”,“height=100,width=100,status=yes”); myMap = window.open(“map.html”, “MapWin”, “width=150,height=150, toolbar,status”);

Closing a pop-up Window – Window Method close() Call the close method with the window’s name To close myMap:  myMap.close();

Opening and Closing a Pop-up Window sample Open and close pop-up window var map = null; function showMap() { var features = "width=620, height=650"; features += ", left=50, top=50,status"; map = window.open("map.html", "MapWin", features); map.focus(); } function closeMap() { if (map != null) { map.close(); map = null; } Contact us (CCSF) 50 Phelan Avenue San Francisco, CA (415) View Map | View Driving Directions | Close Map

map.html Map file Map to CCSF

Pop-up News Popup News function showNews() { news = window.open(" "NewsWin", "width=200, height=220"); var newsTimer = setTimeout("news.close()", "20000"); } Popup News

Window Methods (Timers) The core JavaScript language does not provide ability to schedule code to be executed at some point in the future, but client-side JavaScript does provide it in the form of the global functions setTimeout(), clearTimeout(), setInterval(), and clearInterval(). The setTimeout() method of the Window object schedules a function to run after a specified number of miliseconds elapses. It returns an opaque value that can be passed to clearTimeout() to cancel the execution of the scheduled function. The setInterval() invokes the specified function repeatedly at intervals of the specified number of miliseconds. It returns an opaque value that can be passed to clearInterval() to cancel any future invocations of the scheduled function.