Today we will look at how to add elements to our page dynamically. We'll also look at error handling with try/catch, where JavaScript can be placed in a web page and the different type of dialog boxes that can be used.
The exam requires you to know how to use the document.write()
and document.createElement()
methods to content to an HTML page.
document.createElement()
allows us to create elements of a particular type and insert them in specific places. For example, this is how we could create a button and add it to the body of the HTML.
let myButton = document.createElement("button");
myButton.innerHTML = "CLICK ME";
document.body.appendChild(myButton);
document.write
allows us to write HTML expressions or JavaScript code directly to a document. Content is output precisely where the JavaScript statement is embedded.
For exampe, let's assume we have the following HTML we'd like to append to our source code.
<h1>Hello World!</h1><p>Have a nice day!</p>
We can add this using the following JavaScript.
document.write("<h1>Hello World!</h1><p>Have a nice day!</p>")
Note that if
document.write
is used after the HTML document is fully loaded, it will delete all existing HTML.
You have been asked to evaluate the following source code. What happens when the function is called?
<!DOCTYPE html>
<html>
<body onload="writeToPage()">
<h1>Welcome to my website!</h1>
<script>
function writeToPage() {
document.write('Hello World!');
}
</script>
</body>
</html>
<h1>
tagh1
tag remains visibleNote that the
onload
DOM method is very useful to run some code after the page has completed loading
Sometimes you know ahead of time that a codeblock may fail, or you would like the codeblock to fail in a "graceful" way without breaking your entire application. For this with can use a try/catch
block:
try {
// try and remove an element
field.nextElementSibling.remove();
} catch (e) {
// print the error message
console.log(e.message);
} finally {
// this section always runs
}
In the code above, we try to remove an element from the HTML. If the element isn't there, then a type error will be thrown. If we wrap it in a try/catch block, we can handle this exception, knowing that it may occur.
You have been asked to evaluate the following code. What is printed out to the console?
try {
throw new Error("There's been a problem!");
} catch (e) {
console.log(e.message);
} finally {
console.log("finally called");
}
If you've worked with CSS before, you may know that you can write CSS directly into the HTML source or you can link to an external file. The same holds true for JavaScript. We could inline a script as follows:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Inline script</title>
<script type="text/javascript">
console.log("Hello! I'm an inline script");
</script>
</head>
<body></body>
</html>
Alternatively, you can place your JavaScipt in an external .js
file, and link to it in your HTML source:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>External script</title>
<script type="text/javascript" src="./assets/js/external.js"></script>
</head>
<body></body>
</html>
<body>
and <head>
sections of an HTML page<body>
and <head>
sections of an HTML pageFor more information and to try out some examples refer to this tutorial
JavaScript has three types of popup boxes. These are
Create instances of all 3 dialog box types.