# 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");
```

![](/files/-M_cVJQUBLEqCn0ovh-X)

![](/files/-M_cVrL7CPtm_DAhBfDy)

### 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;

![](/files/-M_aS3ZMnbWkV1UhmCTI)

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.

![](/files/-M_cGwFAGX0a6alP2HRc)

### 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.

![](/files/-M_cHaTn9CJ9dAvcLVaV)

```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>
```

![](/files/-M_aazhrk1BUA4XhGIp8)

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

![](/files/-M_aaMQ3kdWQ_lUD-Pfv)

### 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");
```

![](/files/-M_apKaLqREki6SKt84i)

![](/files/-M_aomGN67YstE0YzIGl)

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);
```

![](/files/-M_ar97XcnNaCJX0rGWH)

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

![](/files/-M_bIV-YbXJZABuLyoyp)

**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>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chnn-anne.gitbook.io/javascript/introducing-scripts.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
