paraObj.innerHTML = inputString;means interpret and/or execute any HTML/CSS/JavaScript code included in inputString.
paraObj.textContent = inputString;means literally display the text that is contained in inputString. Using innerHTML can be a security risk if user input contains malicious JavaScript code. Here is the example that we looked at in class:
<script>
function displayString( ) {
var inputObj = document.querySelector("input");
var paragraphObj = document.querySelector("p");
paragraphObj.innerHTML = inputObj.value;
}
function init( ) {
var buttonObj = document.querySelector("button");
buttonObj.addEventListener("click", displayString);
}
document.addEventListener("DOMContentLoaded",init);
</script>
Paste this string into the input element:
<img src="X" onerror="alert('You\'ve been hacked.');">
then click the button.
An alert box appears with the message "You've been
hacked."
document.addEventListener("DOMContentLoaded", init);
can be used in the place of
window.addEventListener("load", init);
Answer: the first statement, which is executed when the HTML page has been entirely loaded; the
first
statement, which is executed when the DOM Tree has been loaded. The
DOMContentLoaded event
can occur 0.5 to 1.5 seconds before the load event, because the DOM tree
finishes loading before images and videos load for an HTML page. See
Exercise 1 to see how to use the first statement.document.querySelector document.querySelectorAllAnswer:
var obj = document.querySelector(selector);creates an object for the first element that matches the selector.
var obj = document.querySelectorAll(selector);creates an array of all objects that match the selector.
<!DOCTYPE html>
<!-- AndromedaImage Example
Source code file: index.html -->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>Andromeda Image</title>
<style>
img { width: 300px; }
</style>
<script>
function init( ) {
var image1 = document.querySelector("img");
image1.style.padding = "20px";
image1.style.border = "10px solid red";
image1.style.margin = "30px";
}
document.addEventListener("DOMContentLoaded", init);
</script>
</head>
<body>
<h1>Andromeda Image</h1>
<img src="images/andromeda.jpg">
</body>
</html>
<body>
<h2>Ordered List Example</h1>
</body>
Use JavaScript to create and append this ordered list to the body:
--------------------------------------------
<!DOCTYPE html>
<!-- OrderedList Example
Source code file: index.html -->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>Ordered List Example</title>
<link rel="stylesheet" href="styles.css">
<script src="script.js"></script>
</head>
<body>
<h2>Ordered List Example</h1>
</body>
</html>
--------------------------------------------
/* OrderedList Example
Source code file: styles.css */
body { font: 100% Verdana, sans-serif; }
--------------------------------------------
// OrderedList Example
// Source code file: script.css
function init( ) {
// cities array is source of content for ordered list.
var cities = ["New York City", "Los Angeles", "Chicago"];
// Create and add ordered list to body.
var olObj = document.createElement("ol");
var bodyObj = document.querySelector("body");
bodyObj.appendChild(olObj);
// Add cities to ordered list as list items.
for(var c of cities) {
var liObj = document.createElement("li");
liObj.innerHTML = c;
olObj.appendChild(liObj);
}
}
// DOMContentLoaded files when DOM tree has been
// created and made available to the script.
document.addEventListener("DOMContentLoaded", init);
--------------------------------------------
Halloween Greeting Example
Happy Halloween
<body></body>Here is a static HTML file that produces the same page:
<!DOCTYPE html>
<html lang="en">
<head>
<style>
body { background-color:black;
color: orange; }
p { font: bold 200% Chiller; }
</style>
</head>
<body>
<p>Halloween Greeting Example<br>
Happy Halloween!</p>
</body>
</html>
Answer:
<!DOCTYPE html>
<html lang="en">
<head>
<script>
window.onload = ( ) => {
var bodyTag = document.getElementsByTagName("body")[0];
var pTag = document.createElement("p");
pTag.innerHTML = "Halloween Greeting Example<br>" +
"Happy Halloween!";
bodyTag.appendChild(pTag);
bodyTag.style.backgroundColor = "black";
bodyTag.style.color = "orange";
pTag.style.font = "bold 200% Chiller";
// You could set the styles separately like this:
// pTag.style.fontWeight = "bold";
// pTag.style.fontSize = "200%";
// PTag.style.fontFamily = "Chiller";
};
</script>
</head>
<body></body>
</html>
<head>
<script src="../jquery-4.0.0.js">
</script>
... other script tags.
</head>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/4.0.0/jquery.min.js">
</script>
... other script tags.
</head>
$(selector).action( );
// Hide all elements with node name p.
$("p").hide( );
// Change the value of the input
// controls with class="test" to "abc".
$(".test").val("abc");
// Change the inner html of the paragraph
// with id="p1" to "Hello, World!".
$("#p1").text("Hello, World!");
window.addEventListener("load", init);
is$(document).ready(init);or even shorter
$(init);
<!DOCTYPE html>
<html lang="en">
<head>
<script src="jquery-4.0.0.js">
</script>
<script src="myscript.js">
</script>
<link rel="stylesheet"
href="styles.css">
</head>
<body>
<h1>jQuery Examples -- Set 1</h1>
<button id="btn1">Show Greeting</button>
<p id="p1"></p>
</body>
</html>
Test these two choices for myscript.js, then translate them into vanilla JavaScript.
// myscripta.js
$(document).ready( ( ) => {
$("#p1").text("Hello World!");
});
// myscriptb.js
$(document).ready( ( ) => {
$("btn1").click( ( ) => {
$("p1").text("Hello World!");
});
});
// myscripta.java translated into
// vanilla JavaScript
document.addEventListener("DOMContentLoaded", ( ) => {
document.querySelector("p").innerHTML =
"Hello, World!";
});
// myscriptb.java translated into
// vanilla JavaScript
document.addEventListener("DOMContentLoaded", ( ) => {
document.querySelector("btn1").
addEventListener("click", ( ) => {
document.querySelector("p").innerHTML =
"Hello, World!";
});
});
| jQuery | JavaScript |
|---|---|
| $("#p1") | document.querySelector("#p1"); document.getElementById("p1"); |
| $(".red") | document.querySelectorAll(".red"); document.getElementsByClassName("red"); |
| $("p") | document.querySelectorAll("p"); document.getElementsByTagName("p"); |
| $("ol ol") | document.querySelectorAll("ol
ol"); No equivalent document.getElements statement |
Look at some of the examples in the jQuery Tutorial:
www.w3schools.com/jquery/default.asp.
Translate these jQuery function calls from jQuery into vanilla JavaScript. Also, write the JavaScript functions in arrow notation.
$(function( ) {
alert("Hello, World!");
}
or
$(( ) => {
alert("Hello, World!");
});
$(function( ) {
$("btn1").click(function( ) {
alert("Hello, World!");
});
});
or
$(( ) => {
$("bt1").click(( ) => {
alert("Hello, World!");
});
});
$(function( ){
$("p").dblclick(function( ){
$(this).hide( );
});
});
Translate it into vanilla JavaScript. Answer:
function eventHandler(e) {
var para = e.target;
para.style.display = "none";
}
function init( ) {
var para = document.querySelectorAll("p");
for(let node of para)) {
node.addEventListener("dblclick",
eventHandler);
}
}
window.addEventListener("load", init);
Here is the script written with anonymous functions using arrow notation:
window.addEventListener("load", ( ) => {
var para = document.querySelectorAll("p");
for(let node of Array.from(para)) {
node.addEventListener("dblclick", e => {
var para = e.target;
para.style.display = "none";
});
}
});
mouseenter mouseleave mousedown mouseup hover focus blur
$("#p1").hover(function( ) {
alert("Welcome! You entered p.");
},
function( ) {
alert("Bye! You left p.");
});
Here is the Hover Example translated into JavaScript:
// There is no hover event in vanilla JavaScript.
// The mouseenter and mouseleave event handlers
// must be implemented separately.
window.addEventListener("load", ( ) => {
var para1 = document.querySelector("p");
para1.addEventListener("mouseenter", ( ) => {
alert("Welcome! You entered p.");
});
para1.addEventListener("mouseleave", ( ) => {
alert("Bye! You left p.");
});
});