# JavaScript in the Browser

### Project Setup

Create a new project folder named scripts-intro. Open the folder in VS Code and create an index.html file and add the following in the body element.

### \<script> element with embedded JavaScript code

JavaScript scripts can be included directly within the HTML document using the \<script> element and placing the JavaScript code between the opening and closing tags.

```markup
<body>
  <script>
    console.log("Hello");
  </script>
</body>
```

### \<script> element with external JavaScript code

But the preferred method is to load the script from an external file, by specifying the location of the file with the src attribute.

```javascript
/* script.js */

console.log("Hello");
```

```markup
<html lang="en">
<head>
</head>
<body>
  <!-- all other html first -->
  <script src="script.js"></script>
</body>
</html>

```

### script placement

The \<script> element causes the web browser to stop processing the HTML and process the script element before continuing to process the HTML document.

It is legal for a script element to be placed anywhere in the document, including the head element. But a script could run into a problem if it is located in the document before an element that the script references.

For example, if a script element is inside the head element, and it set the background color of the body element, an error would occur because the body element has not been generated yet.

For that reason, it is best practice to position any script elements at the very end of the body element.\
&#x20;  &#x20;

### JavaScript Statement

The code below is a JavaScript statement.&#x20;

```javascript
console.log("Hello");
```

A statement is an instruction that performs an action. &#x20;

### JavaScript built-in objects

Objects represent things in real life and are also used in computer programs to model data. Objects have properties (characteristics of the object, such as a person having a first name and last name) and methods. A method is an action that can be invoked to complete a task.

The JavaScript runtime environment within the web browser provides several objects you will use frequently.

#### console object

The console object is available to JavaScript code and provides methods for logging content to the console.

```javascript
console.log("Hello");
```

![](https://4178747114-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MXd9FZsxrESRpx9oFdw%2F-M_aN4zSu92GGm-I6eIa%2F-M_cVJQUBLEqCn0ovh-X%2Fimage.png?alt=media\&token=3f1622b1-c021-4469-ae57-cabe152130f3)

![](https://4178747114-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MXd9FZsxrESRpx9oFdw%2F-M_aN4zSu92GGm-I6eIa%2F-M_cVrL7CPtm_DAhBfDy%2Fimage.png?alt=media\&token=01d6db15-93d9-4a5d-9ac3-3ebb6b944943)

### Exercise

Add a script element to your html document and first add an embedded script to call the console.log method with some text;

Now, move your script code to an external script.js file.

####

#### document object

If you open Chrome Dev Tools/Console and type document you will get an object that represents the HTML document.&#x20;

![](https://4178747114-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MXd9FZsxrESRpx9oFdw%2F-M_aN4zSu92GGm-I6eIa%2F-M_aS3ZMnbWkV1UhmCTI%2Fimage.png?alt=media\&token=d03a60d0-57ce-4de1-9a11-f483838c6114)

The  document object is available to JavaScript code and is the entry into the DOM, which is the API that allows us to manipulate the HTML elements in the document.

### object properties

The statement below is setting the value of the backgroundColor property on the body element. Each dot in the statement is accessing an object contained within the parent object.

```javascript
document.body.style.backgroundColor = "red";
```

After the statement has been executed, the HTML document will be modified to have added the background-color to the body element.

![](https://4178747114-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MXd9FZsxrESRpx9oFdw%2F-M_aN4zSu92GGm-I6eIa%2F-M_cGwFAGX0a6alP2HRc%2Fimage.png?alt=media\&token=d7f7fb76-423f-4396-8a24-5d9b8cb52cc2)

### window object

When JavaScript code is executed within a browser, the window object is the global object and represents the browser window.&#x20;

The document and console object are actually not a global objects, but  properties of the window object. But, all properties of the global window object can be accessed directly.

![](https://4178747114-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MXd9FZsxrESRpx9oFdw%2F-M_aN4zSu92GGm-I6eIa%2F-M_cHaTn9CJ9dAvcLVaV%2Fimage.png?alt=media\&token=93324c79-dc18-41bd-b626-8c916fc49e01)

```markup
/* this */
window.alert("Hello");

/* is the same as this */
alert("Hello");
```

### window.alert method

The statement is calling the alert method on the window object in order to display a message window to the user.

```markup
<body>
    <script>
        window.alert("Hello");
    </script>
</body>
```

![](https://4178747114-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MXd9FZsxrESRpx9oFdw%2F-M_aN4zSu92GGm-I6eIa%2F-M_aazhrk1BUA4XhGIp8%2Fimage.png?alt=media\&token=5d841867-b9b7-4459-bb91-919ff2ba4844)

**alert(message)** - shows a message to the user and waits for the user to click Ok.

![](https://4178747114-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MXd9FZsxrESRpx9oFdw%2F-M_aN4zSu92GGm-I6eIa%2F-M_aaMQ3kdWQ_lUD-Pfv%2Fimage.png?alt=media\&token=cae0b6d8-0f36-44eb-818a-20c382a6d445)

### window.prompt method

**prompt(message, default value)** - shows a modal dialog with a message and a input field for the user to enter a value and waits for the user to click Ok or Cancel. The second parameter is optional, and will be used as the default value if it is supplied. The function will return the value the user entered.

The script below is calling the prompt method on the window object to ask the user what color to change the background color to.

The prompt method returns a value entered by the user. In order to store the return value we need to create a variable to hold the return value.

The **let keyword** is how you define a variable to hold a value.&#x20;

The **= symbol** is how you assign a value to a variable.

```javascript
let color = window.prompt("What color should I set the background to?", "yellow");
```

![](https://4178747114-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MXd9FZsxrESRpx9oFdw%2F-M_aN4zSu92GGm-I6eIa%2F-M_apKaLqREki6SKt84i%2Fimage.png?alt=media\&token=80fb7f01-1c16-44ac-81bc-e4836a122edf)

![](https://4178747114-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MXd9FZsxrESRpx9oFdw%2F-M_aN4zSu92GGm-I6eIa%2F-M_aomGN67YstE0YzIGl%2Fimage.png?alt=media\&token=f0212fb1-f50e-4717-b1a1-0ea08b37a070)

Use the value stored in the color variable to set the value of the body element's background color;

```javascript
let color = window.prompt("What color should I set the background to?", "yellow");
document.body.style.backgroundColor = color;
```

### Concatenating Strings

Modify the above script to use the window.alert method to display the value of the background color after it has been changed.

The "+" character can be used to concatenate strings to form a larger string.

```javascript
let color = window.prompt("What color should I set the background to?", "yellow");
document.body.style.backgroundColor = color;
window.alert("The color is: " + color);
```

![](https://4178747114-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MXd9FZsxrESRpx9oFdw%2F-M_aN4zSu92GGm-I6eIa%2F-M_ar97XcnNaCJX0rGWH%2Fimage.png?alt=media\&token=2a432430-97bf-43e8-a43a-98d44319ff47)

Modify the script above to display the color before and after the change.

```javascript
let colorBefore = document.body.style.backgroundColor;
let color = window.prompt("What color should I set the background to?", colorBefore);
document.body.style.backgroundColor = color;
window.alert("The color has been changed from " + colorBefore + " to " + color);
```

### window.confirm method

![](https://4178747114-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MXd9FZsxrESRpx9oFdw%2F-M_aN4zSu92GGm-I6eIa%2F-M_bIV-YbXJZABuLyoyp%2Fimage.png?alt=media\&token=cb38106a-fd1a-49ab-a1dd-def5aa04f676)

**confirm(message)** - shows a modal dialog with a message and waits for the user to click Ok or Cancel. The function will return true if the user clicks Ok, and false if the user clicks Cancel.

In the script below, on line 3, we've added a call to the window.confirm method to ask whether the user wants to change the background color. But we're not doing anything with the return value. But we're not using the answer to decide whether to change the color.

```javascript
let colorBefore = document.body.style.backgroundColor;
let answer = window.confirm("Do you want to change the background color?");
let color = window.prompt("What color should I set the background to?", colorBefore);
document.body.style.backgroundColor = color;
window.alert("The color is: " + color);
```

### Conditional Statements

Here, we are testing the answer to see if it is true, and if it is, then we prompt to ask for a color to change.

```markup
<body>
  <script>
    let answer = window.confirm("Do you want to change the background color?");
    if (answer === true) {
      let color = window.prompt("What color should I set the background to?", colorBefore);
      document.body.style.backgroundColor = color;
      window.alert("The color is: " + color);
    }
  </script>
</body>
```
