There are few ways to use JavaScript in the browser:
- Write in the HTML file as a
<script>
tag. - Write a separate JavaScript file and link it in your HTML page.
- Developer Console in Chrome.
In this lesson, we'll be writing JavaScript in the HTML file using the <script>
tag.
Scenario: You want to welcome someone to your website
Objective:
- Get introduced to Visual Studio Code
- Write JavaScript code in your HTML file to greet someone
Steps:
-
Start Visual Studio Code
-
Create a new project:
- In the menu bar, click on:
File
>New Window
- Open the Explorer sidebar:
View
>Explorer
- In the menu bar, click on:
-
Click on "Open Folder"
-
Navigate to the desktop folder and click on "New Folder"
-
Create a new folder in your desktop. Let's call it "my_javascript"
-
Click on "Open"
-
Create a new file:
File
>New file
-
Copy and paste this into your editor
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Hello, world!</title> </head> <body> <script> document.write('Hello, World') </script> </body> </html>
-
Save the file and give it the file name: index.html
- From the menu bar:
File
>Save
- Or you can use the following keyboard shortcuts:
- MacOS: Press
Cmd
+s
- Windows: Press
Ctrl
+s
- MacOS: Press
- From the menu bar:
-
Look for the file in your desktop folder. It should be inside the "my_javascript" folder.
-
Double-click on the file and it should open in the web browser (otherwise, just drag and drop the file into Chrome browser)
You have just written your first in-line JavaScript. This is the JavaScript code you just wrote:
document.write('Hello, World')
Scenario: Let's customize how we greet the person
Objective:
- Write some JavaScript code that will ask the user's name and write it into the HTML file
Steps:
-
Let's go back to Visual Studio Code
-
In your index.html file, let's change the JavaScript portion to this:
const yourName = window.prompt('Can I have your name, please.', 'World') document.write('Hello, ' + yourName + '!')
Note: You can delete the existing JavaScript code and just copy and paste the code above into the
<script>
tag. -
Save the file
-
Go back to the Chrome browser and reload the page
-
You would be prompted for your name, click "OK". And your name should show up in the browser page.
Hello, Michael
You just used the window.prompt()
command to interact with visitors of your site.
Further reading
Scenario: Let's do some clicking!
Objective:
- Write some HTML button that triggers a JavaScript code
- Learn about the HTML Document Object Model (DOM) and how to make changes to it
Steps:
-
Let's go back to Visual Studio Code
-
In your index.html file, let's add this code in the
<body>
tag:<h1 id="headLine">This is a headline</h1> <button onclick="document.getElementById('headLine').style.color = 'red'"> Click Me! </button>
Note: You can add this above the
<script>
tag. -
Save the file
-
Go back to the Chrome browser and reload the page
-
You should see a new button on screen. Try clicking it.
Explanation
You just used the HTMLElement.click()
command to interact with visitors of your site. You have also managed to change the color of the text by changing its style programmatically by manipulating the DOM.
This is the way that we reference the <h1 id="headLine">...
tag
document.getElementById('headLine')
And then we point to its style
DOM object and change the color
attribute to 'red'
. If you recall the last lesson, 'red'
is a valid "CSS Named Color".
Further reading
- https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click
- https://developer.mozilla.org/en-US/docs/Glossary/DOM