Moving away from alert() Using innerHTML Using an empty div section

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Modifying existing content Adding/Removing content on a page using jQuery.
Events Part III The event object. Learning Objectives By the end of this lecture, you should be able to: – Learn to use the hover() function – Work with.
JavaScript- Introduction. What it is and what it does? What it is? It is NOT Java It is NOT Server-side programming Users can see code It is a client-side.
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.
Web Page Development Identify elements of a Web Page Start Notepad
Escaping Characters document.write(). Learning Objectives By the end of this lecture, you should be able to: – Recognize some of the characters that can.
Random Stuff Colors Sizes CSS Shortcuts. Learning Objectives By the end of this lecture, you should be able to: – Identify the 3 most common ways in which.
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Writing a JavaScript User-Defined Function  A function is JavaScript code written to perform certain tasks repeatedly  Built-in functions.
Working with Files. Learning Objectives By the end of this lecture, you should be able to: – Examine a file that contains code with unfamiliar material,
Java Script User Defined Functions. Java Script  You can define your own JavaScript functions. Such functions are called user- defined, as opposed to.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
Modifying HTML attributes and CSS values. Learning Objectives By the end of this lecture, you should be able to: – Select based on a class (as opposed.
JavaScript Challenges Answers for challenges
Review of HTML and CSS A (very) brief review of some key fundamentals to be aware of in IT-238.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
CSD 340 (Blum)1 Introducing Text Input elements and Ifs.
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
How to build a Web Page or “The things geeks never told you!”
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
JavaScript – If/Else contd
JavaScript Controlling the flow of your programs with ‘if’ statements
Precedence Operators Error Types
Web Basics: HTML/CSS/JavaScript What are they?
Chapter 6 JavaScript: Introduction to Scripting
Concatenation Comments
JavaScript Wrap-up.
JavaScript is a programming language designed for Web pages.
Section 17.1 Section 17.2 Add an audio file using HTML
Retrieving information from forms
Intro to Web Development Class A Review
JavaScript Part 2 Organizing JavaScript Code into Functions
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
Intro to PHP & Variables
HTML Forms and User Input
Data Types Parsing Data
Introduction to DOM.
JavaScript – Part I Mendelsohn.
Integrating JavaScript and HTML
Intro to Web Development HTML Structure
Inputs and Variables Programming Guides.
JavaScript Variables.
Tutorial 10: Programming with javascript
JavaScript Basics What is JavaScript?
JavaScript is a scripting language designed for Web pages by Netscape.
Introduction to Programming and JavaScript
JavaScript: Introduction to Scripting
Introduction to JavaScript
Week 5: Recap and Portfolio Site
Web Programming and Design
Introduction to Web programming
Adding/Removing content on a page using jQuery
Events Part III The event object.
Reading information from files
Modifying HTML attributes and CSS values
JavaScript Variables.
Random Stuff Colors Sizes CSS Shortcuts.
A Few Notes on JavaScript
JavaScript Part 2 Organizing JavaScript Code into Functions
Retrieving information from forms
CSS Classes.
Data Types Parsing Data
Introduction to scripting
JavaScript Variables.
Adios alert() Using innerHTML Using an empty section
Concatenation of Strings JavaScript Comments
Presentation transcript:

Moving away from alert() Using innerHTML Using an empty div section JavaScript Moving away from alert() Using innerHTML Using an empty div section

Learning Objectives By the end of this lecture, you should be able to: Learn a much better technique than alert() to output information to a web page using JavaScript's innerHTML command. Learn how to use a blank <div> section as a placeholder for information you wish to output

Bye-bye alert() It is time to say goodbye to an old friend. Though the alert() function has served us well, it should not typically be used in the “real world”. At the very least, it should be reserved for only a few particular situations. Some limitations of alert() include – but are not limited to: The alert box can not display HTML markup. The alert box can not display any images. The alert box disappears (along with any information inside) when the user clicks the ‘OK’ button. Information in an existing alert box can not be modified using scripting. Therefore, instead of alert boxes, we are going to learn how to use a different JavaScript command to output content into our web page.

Outputting using innerHTML The following JavaScript code will output the words: Look at me! (in <h1> markup) into a div section called 'main_content': var someText = "<h1>Look at me!</h1>"; document.getElementById("main_content").innerHTML = someText; This innerHTML command is extremely helpful and widely used. However it is very important to note that the innerHTML command replaces anything and everything that was in that section before. So in this case, the above command would replace everything that was previously inside 'main_content' with the words 'Look at me!' Imagine if that main_content div section contained all of the content for your web page… and you have just replaced that entire section with the words "Look at me!". This is clearly not a desirable result. Fortunately, there is an easy fix….

Creating an empty <div> section The fix is a simple one: We will create an empty <div> section whose only job in life is to hold the output of the innerHTML command: <div id="results"> </div> The following JavaScript code will output the words: Look at me! in <h1> markup into our "results" div section: var someText = "<h1>Look at me!</h1>"; document.getElementById("results").innerHTML = someText; The first line of code simply creates a variable that holds a string containing the text we want to insert into our <div> section. The second line inserts the string from the variable 'someText' into the 'results' div section.

Creating an empty <div> section Suppose that we are writing a conversion function of some kind (e.g. convert pounds to kilograms), and we want to output the information into our HTML document. I will start by creating an empty div section like so: <div id="conversionResults"> </div> Again, at the moment, this <div> section is essentially invisible on our page since there is nothing inside it. Instead of using the alert()function to output results like we've been doing up to this point: alert(kilograms); we will instead use innerHTML like so: document.getElementById("conversionResults").innerHTML = kilograms;

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Conversions with innerHTML</title> <script type="text/javascript"> function convertKilos() { var pounds = document.getElementById("txtPounds").value; pounds = parseFloat(pounds); var kilos = pounds*2.28; var output = pounds + " pounds equals " + kilos + " kilograms."; document.getElementById("conversionResults").innerHTML = output; } </script> </head> <body> Enter an amount in pounds: <input type="text" id="txtPounds"> <input type="button" value="Convert to kilograms" onclick="convertKilos()" ><p> <div id="conversionResults"> </div> </body> </html>

FILE: inner_html_greeting.htm The steps Here are the steps as I would recommend doing them: Begin by creating an empty <div> section in which you will later output your information. Name this section whatever you like. I use an identifier for the div that indicates what is going inside. For example: 'conversionResults' or 'greetingOutput'. Create a variable to hold the content you wish to output. Place your entire string inside that variable. You may find yourself doing a certain amount of concatenation here. Output that variable into the div section using JavaScript's innerHTML command. FILE: inner_html_greeting.htm

(This would make for a good exam question…) Bug alert!! Recall that the innerHTML command replaces any content that was inside that section before. Suppose that we want to greet the user and print the date. What do you think will happen here? var greeting = "Hello, how are you?" var todaysDate = "Today is " + Date(); document.getElementById("output").innerHTML = greeting; document.getElementById("output").innerHTML = todaysDate; Answer: The second innerHTML command will replace whatever was previously inside the "output" div. Since the second innerHTML command is executed by JavaScript essentially instantly after the first innerHTML command, the visitor to our page will never see the first line (the greeting). (This would make for a good exam question…)

FILE: inner_html_greeting_concatenated.htm Concatenation redux Can you fix this code so that both strings get output into the 'output' div? var greeting = "Hello, how are you?" var todaysDate = "Today is " + Date(); document.getElementById("output").innerHTML = greeting; document.getElementById("output").innerHTML = todaysDate; Answer: var greeting = "Hello, how are you?" + "<br>Today is " + Date() + ". <br>I hope you have a nice day."; FILE: inner_html_greeting_concatenated.htm

Another example FILE: innerHTML_conversions.htm