Working with the Console in a Browser
To open the Web Console in FireFox, you can navigate to the ☰ menu in the top right corner next to the address bar.
From there, click on the Developer button symbolized by the wrench icon, which will open the Web Developer menu. With that open, click on the Web Console menu item.
Once you do so, a tray will open at the bottom of your browser window:
You can also enter into the Web Console with the keyboard shortcut
K on Linux and Windows, or
K on macOS.
J on Linux or Windows, or
J on macOS, which will bring focus immediately to the Console.
Working in the Console
Let’s start with an alert that prints out the string
- alert("Hello, World!");
Once you press the
Note that the Console will also print the result of evaluating an expression, which will read as
undefined when the expression does not explicitly return something.
To print the
Hello, World! string, we can type the following into the Console:
- console.log("Hello, World!");
Within the console, you’ll receive the following output:
- console.log(2 + 6);
You can also try some more complicated math:
- console.log(34348.2342343403285953845 * 4310.23409128534);
Additionally, we can work on multiple lines with variables:
- let d = new Date();
- console.log("Today's date is " + d);
OutputToday's date is Wed Jun 21 2017 15:49:47 GMT-0400 (EDT)
If you need to modify a command that you passed through the Console, you can type the up arrow ↑ key on your keyboard to retrieve the previous command. This will allow you to edit the command and send it again.
Working with an HTML File
Bear in mind that as soon as you reload a page following modifying it with the Console, it will return to its state prior to your modifying the document, so make sure to save any changes you would like to keep elsewhere.
Let’s take a blank HTML document, such as the following
index.html file to understand how to use the Console to modify it. In your favorite text editor, create an
index.html file and add the following lines of HTML:
<!DOCTYPE html> <html lang="en-US"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Today's Date</title> </head> <body> </body> </html>
If you save the above HTML file, and load it into the browser of your choice, you should see a blank page with the title of the page as
- let d = new Date();
- document.body.innerHTML = "<h1>Today's date is " + d + "</h1>"
You’ll receive the following output on the Console:
Output"<h1>Today's date is Sat Jun 24 2017 12:16:14 GMT-0400 (EDT)</h1>"
And at this point, your page should look similar to this:
We can also go on to modify the style of the page, such as the background color:
- document.body.style.backgroundColor = "lightblue";
As well as the color of the text on the page:
- document.body.style.color = "white";
Now your page will look something like this:
From here, you can create a
<p> paragraph element:
- let p = document.createElement("P");
With this element created, you can then go on to create a text node that we can then add to the paragraph:
- let t = document.createTextNode("Paragraph text.");
We’ll add the text node by appending it to the variable
And finally append
p with its paragraph
<p> element and appended text node to the document:
Once you have completed these steps, your HTML page
index.html will look similar to this:
The Console provides you with a space to experiment with modifying HTML pages, but it is important to keep in mind that you’re not changing the HTML document when you do things on the Console. In this case, once you reload the page it will return to a blank document.
Understanding Other Development Tools
Depending on which browser’s development tools you use, you’ll be able to use other tools to help with your web development workflow. Let’s go over a few of these tools.
DOM — Document Object Model
Each time a web page is loaded, the browser it is in creates a Document Object Model, or DOM, of the page.
These tools enable you to inspect and edit DOM elements and also let you identify the HTML related to an aspect of a particular page. The DOM can tell you whether a text snippet or image has an ID attribute and can let you determine what that attribute’s value is.
The page that we modified above would have a DOM view that looks similar to this before we reload the page:
Additionally, you will see CSS styles in a side panel or below the DOM panel, allowing you to see what styles are being employed within the HTML document or via a CSS style sheet. This is what our sample page above’s body style looks like within the Firefox Inspector:
To live-edit a DOM node, double-click a selected element and make changes. To start, for example, you can modify an
<h1> tag and make it an
As with the Console, if you reload the page you’ll return to the saved state of the HTML document.
The Network tab of your browser’s built-in development tools can monitor and record network requests. This tab shows you the network requests that the browser makes, including when it loads a page, how long each request takes, and provides the details of each of these requests. This can be used to optimize page load performance and debug request issues.
To learn more about how to use the Network tab, you can read about working with Firefox’s Network Monitor or getting started with analyzing Network performance with Chrome’s DevTools.
When websites are responsive, they are designed and developed to both look and function properly on a range of different devices: mobile phones, tablets, desktops, and laptops. Screen size, pixel density, and supporting touch are factors to consider when developing across devices. As a web developer, it is important to keep responsive design principles in mind so that your websites are fully available to people regardless of the device that they have access to.
Both Firefox and Chrome provide you with modes for ensuring that responsive design principles are given attention as you create and develop sites and apps for the web. These modes will emulate different devices that you can investigate and analyze as part of your development process.