Adios alert() Using innerHTML Using an empty 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

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.
CM143 - Web Week 2 Basic HTML. Links and Image Tags.
Computer Science 103 Chapter 4 Advanced JavaScript.
Escaping Characters document.write(). Learning Objectives By the end of this lecture, you should be able to: – Recognize some of the characters that can.
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.,
Basic Responsive Design Techniques. Let’s take a look at this basic layout. It has a header and two columns of text, in a box that is centered on the.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
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,
JavaScript. JavaScript is the programming language of HTML and the Web. Easy to learn One of the 3 languages of all developers MUST learn: 1. HTML to.
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.
1 Javascript CS , Spring What is Javascript ? Browser scripting language  Dynamic page creation  Interactive  Embedded into HTML pages.
Changing HTML Attributes each() function Anonymous Functions $(this) keyword.
Review of HTML and CSS A (very) brief review of some key fundamentals to be aware of in IT-238.
Understanding JavaScript and Coding Essentials Lesson 8.
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.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
Tarik Booker CS 120 California State University, Los Angeles.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
Getting Started With HTML
Precedence Operators Error Types
Introduction to.
Build in Objects In JavaScript, almost "everything" is an object.
Event-Driven Programming
Moving away from alert() Using innerHTML Using an empty div section
Chapter 6 JavaScript: Introduction to Scripting
Predefined Functions Using the JavaScript Documentation
FOP: JavaScript Arrays(Lists)
Concatenation Comments
JavaScript Wrap-up.
Concepts of HTML, CSS and Javascript
Coding, Testing and Valdating a Web Page
Intro to JavaScript CS 1150 Spring 2017.
IS1500: Introduction to Web Development
Retrieving information from forms
Intro to Web Development Class A Review
JavaScript Part 2 Organizing JavaScript Code into Functions
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
Teaching slides Chapter 6.
JavaScript Variables.
Javascript and JQuery SRM DSC.
JavaScript Basics Topics Review Important Methods Writing Functions
Introducing JavaScript
Chapter 7 Event-Driven Pages
Introduction to Programming and JavaScript
JavaScript: Introduction to Scripting
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.
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.
Concatenation of Strings JavaScript Comments
Presentation transcript:

Adios alert() Using innerHTML Using an empty section JavaScript Adios alert() Using innerHTML Using an empty 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 Learn how to use a blank 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, alert() 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 images. The alert box disappears (along with any information inside) when the user clicks the ‘OK’ button. The user must click 'OK' in order to make the alert box disappear. Information in an existing alert box can not be modified. Therefore, we are going to pretty much retire alert boxes, we are now going to learn how to use a different JavaScript command to output content directly into an existing web page.

Outputting using innerHTML The following JavaScript code will output the words: Look at me! (in <h1> markup) into a section on the page that has a ID named 'output': var someText = "<h1>Hello World!</h1>"; document.getElementById("output").innerHTML = someText; This innerHTML command is extremely helpful and widely used. IMPORTANT: It is very important to note that the innerHTML command replaces everything that was in that section before. So in this case, the above command would replace everything that was previously inside 'output' with the words Hello World!. Imagine if that output section contained a whole bunch of important information that was there earlier. If you then issued the innerHTML command, all of that existing content would be replaced with the words "Hello World!". This is clearly not a desirable result. Fortunately, there are various easy fixes. We will discuss one option next.

Creating an empty section We will place an empty section in our document. The one and only job of this section 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 content we want to insert into our web document. The second line inserts the string into the 'results' section.

Restated: 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 section like so: <div id="conversionResults"> </div> NOTE: When the page first loads, this <div> section is invisible since there is nothing inside of it. Instead of using the alert()function to output results like we've been doing up to this point: alert(kilograms); we will from this point forward use innerHTML like so: document.getElementById("conversionResults").innerHTML = kilograms;

The steps Here are the steps as I would recommend doing them: Begin by creating an empty <div> section (or a semantic section if there is one that "fits") in which you will later output your information. Give an identifier to this section using the id attribute. Obviously, you should choose an identifier that gives some idea of what is supposed to be 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 (i.e. joining strings together) here. Output that variable into the section using JavaScript's innerHTML command.

innerHTML_greeting.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Greeting with innerHTML</title> </head> <body> <h1>innerHTML Example</h1> <p>What is your name? <input type="text" id="txtName"> <p><input type="button" value="Say Hello to Me!" onclick="greetUser()" > <div id="greetingOutput"> </div> <!-- end of greetingOutput div --> <script> function greetUser() { var userName = document.getElementById("txtName").value; var greeting = "Hello, " + userName; //Recall that you can join 2 strings together with the + operator document.getElementById("greetingOutput").innerHTML = greeting; } </script> </body> </html> innerHTML_greeting.html

(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. Here is some code: var greeting = "Hello, how are you?" var todaysDate = "Today is " + Date(); document.getElementById("output").innerHTML = greeting; document.getElementById("output").innerHTML = todaysDate; What do you predict will happen? Answer: The second innerHTML command will replace whatever was previously inside the output section. 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…)