Spelchan.com Logo

From Scratch Web Games: A Beginners Guide to Game Development using HTML, CSS, and JavaScript

Chapter 5.13: Dialog Concepts

At this point in time, dialog boxes should be something every reader is familiar with. The idea behind them is that you need some information to get from or present to user. Instead of switching to a different screen you instead open a window above the application that the user deals with. Once the user has finished with the dialog box, it disappears. This is useful for small forms and alert messages, so it is a bit surprising that this feature wasn’t officially available until 2022, a decade after HTML5 was released. Before the dialog tag was added, users were restricted to just a few built-in dialog boxes unless they manually created their own.

The three built-in dialog boxes are part of the window element that is built into the browser. In fact, window is the global object so you don’t even need to specify that you want to use the window object as it is at the end of the scope chain. I will use window when using the built in alerts just so it is clear where these methods are coming from. While window is the core object in browsers, if you are running JavaScript as a scripting language in another piece of software (say a game engine or node.js) then this may not be the case.

The basic alert message simply brings up a message that you must click on before continuing. This tends to annoy users so should only be used for serious alerts. Browser makers have noticed that this can be used by miscreants so will tend to limit it’s use to scripts whose origin is the domain where the site is from. It is simple to use as you can see.

function showAlert() {
window.alert("This is an Alert");
}

The purpose of the confirm dialog box is to make sure that the user really wants to do something. This can also be annoying if used too often so you should only do so when there is potential data-losing or other critical reasons for the confirmation. As with alert, this can’t be called by cross-origin scripts on many browsers. Because there are two different possible results, the confirm method will return true if the user confirms the request by hitting ok and returns false if they cancel the request. The code for using confirmation dialog boxes looks like this:

function showConfirm() {
let conf = window.confirm("This is an Confirm\nDid you read this?");
if (conf) {
window.alert("Thanks for reading the message!");
} else {
window.alert("Why didn't you read the message?");
}
}

The final built-in dialog box is used to get the user to enter a line of text. As with the other two build-in dialog boxes, many browsers block it’s use by cross-origin scripts. The text that the user enters is returned to the script so using this is done as follows:

function showPrompt() {
let msg = window.prompt("This is an Prompt", "no text entered");
window.alert("You entered: \n" + msg);
}

You will notice that all three of the built-in dialog boxes block the user while they appear, with all controls unresponsive until the dialog is answered. This is known as a modal dialog box. Another type of dialog box that you may encounter is one that appears but let you do other things while you are working with it. The term for this type of dialog box is modeless.

If we wanted to create our own dialog box before 2022, we could do so by simply having a form on our page (technically, we just need the form controls so the form tag is optional). We can do so by creating a form and having it initially invisible. When activated the form is made visible and when done, the form is hidden again as can be seen by the following code.

<form id="dialogForm" style="visibility:hidden;">

And inside a script element you would have:

function showFakeDialog() {
document.getElementById("characterResults").innerHTML="";
document.getElementById("dialogForm").style.visibility = "visible";
}
function closeFakeDialog() {
// display the dialog results
// CODE REMOVED as not needed
// hide the dialog window
document.getElementById("dialogForm").style.visibility = "hidden";
}

Another way this can be done is we can reserve a div field and create the form when it is needed, then have the close method remove the html. The advantage of this approach is no space is taken on the page for the hidden component.

We can also have the form appear over the page by taking advantage of CSS layout features, which will be discussed in the next chapter.

The above dialog would be modeless, meaning that the user would still be able to do stuff on the page while the dialog was visible. If you wanted a modal dialog, you would have a bit more work involved as you would need to disable all the other components on the page. Some libraries have functions that will do this for you, and you could take advantage of some advanced document methods such as getElementByClassName and getElementsByTagName to loop through all of the elements, which is something we will discuss when we get into Arrays in Chapter 7. Here is our demo code with additional functionality to support modal dialogs but it is clearly a lot more work.

function showFakeDialog(modeless) {
document.getElementById("characterResults").innerHTML="";
document.getElementById("dialogForm").style.visibility = "visible";
if (modeless) {
document.getElementById("alert").disabled = true;
document.getElementById("confirm").disabled = true;
document.getElementById("prompt").disabled = true;
document.getElementById("modelessButton").disabled = true;
document.getElementById("modalButton").disabled = true;
}
}

function closeFakeDialog() {
// display the dialog input
// CODE REMOVED AS NOT NEEDED
// hide the dialog window
document.getElementById("dialogForm").style.visibility = "hidden";
// this only has to be done if modeless, but will do it in both cases
document.getElementById("alert").disabled = false;
document.getElementById("confirm").disabled = false;
document.getElementById("prompt").disabled = false;
document.getElementById("modelessButton").disabled = false;
document.getElementById("modalButton").disabled = false;
}

If only there was some type of dialog tag that would easily let us create modal and modeless dialog boxes. In 2022 the <dialog> tag was introduced, which we will be exploring next.

Chapter contents

Chapter 5 Contents

5.1 Forms Cheat Sheets

This chapter's summary cheat sheet.

5.2 The client-server model

How forms are traditionally used.

5.3 The Form tag

How to set up a form in HTML.

5.4 Inputting text

Getting text input from the user.

5.5 Specialized text formats

Making your input fields more specific and semantic.

5.6 Form cosmetics

Making forms look more like printed forms.

5.7 checkboxes & radio buttons

How to use Checkboxes and Radio buttons.

5.8 Numbers and Dates

Using numbers and dates gives your user better GUI controls.

5.9 special purpose input types

The reset, button, image, color, and file input types.

5.10 Option Select

The select element lets users select options from a drop-down list.

5.11 Data Lists

The datalist elements lets you create pull-down lists for input elements.

5.12 Output

Displaying values in a form.

5.13 dialog box concepts

Built in dialog boxes and pre-dialog tag handmade dialogs.

5.14 using dialog boxes

The <dialog> tag and using it to create dialog boxes.

5.15 Validating forms

Review of validating forms in HTML.

5.16 JavaScript Validation

How to use JavaScript to prevent invalid forms from being submitted.

5.17 Project: Trivia

Coming December 8th.

5.18 Trivia Project Solution

Coming December 15th.

← previous section
Output
next section →
Using Dialog boxes
Table of Contents