mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-10-24 23:49:06 +08:00
feat(curriculum): Add interactive example to script element lesson (#62647)
This commit is contained in:
+8
-4
@@ -5,19 +5,23 @@ challengeType: 19
|
||||
dashedName: what-is-the-role-of-the-script-element-in-html
|
||||
---
|
||||
|
||||
# --description--
|
||||
# --interactive--
|
||||
|
||||
The `script` element is used to embed executable code. Most developers will use this to execute JavaScript code. JavaScript is used to add interactivity to your web pages. Common examples of using JavaScript include interactive games, image sliders, and dynamic forms that validate user input in real-time. Here is an example of using the `script` element in an HTML document:
|
||||
The `script` element is used to embed executable code. Most developers will use this to execute JavaScript code. JavaScript is used to add interactivity to your web pages. Common examples of using JavaScript include interactive games, image sliders, and dynamic forms that validate user input in real-time.
|
||||
|
||||
Here is an example of using the `script` element in an HTML document. Remove the `//` from in front of the `alert("Welcome to freeCodeCamp");` and you should see an alert pop up.
|
||||
|
||||
:::interactive_editor
|
||||
|
||||
```html
|
||||
<body>
|
||||
<script>
|
||||
alert("Welcome to freeCodeCamp");
|
||||
// alert("Welcome to freeCodeCamp");
|
||||
</script>
|
||||
</body>
|
||||
```
|
||||
|
||||
In this example, we have an `alert` to display the message `Welcome to freeCodeCamp.` When the page first loads, the alert will pop up. Then the user can click on the OK button to dismiss the message.
|
||||
:::
|
||||
|
||||
While you can technically write all of your JavaScript code inside the `script` tags, it is considered best practice to link to an external JavaScript file instead. Here is an example of using the `script` element to link to an external JavaScript file:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user