Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
The <script> element embeds or references JavaScript code. You can write code directly inside (inline) or link to external .js files.
Inline Scripts
External Files
ES6 Modules
Inline vs External vs Module
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Script Types</title>
</head>
<body>
<div class="types">
<div class="type-card">
<h3>Inline Script</h3>
<code><script>code here</script></code>
<p>JavaScript inside HTML</p>
</div>
<div class="type-card">
<h3>External Script</h3>
<code><script src="file.js"></code>
<p>Separate .js file</p>
</div>
<div class="type-card">
<h3>ES6 Module</h3>
<code><script type="module"></code>
<p>Import/export syntax</p>
</div>
</div>
</body>
</html>Loading preview...
JavaScript inside the HTML file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Inline Script</title>
</head>
<body>
<h1 id="greeting">Hello!</h1>
<button onclick="changeText()">Click Me</button>
</body>
</html>Loading preview...
Using src attribute to load JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>External Script</title>
</head>
<body>
<h1>External JavaScript</h1>
<p id="output">Waiting for script...</p>
<!-- External script -->
<!-- Inline script to demonstrate -->
</body>
</html>Loading preview...
Syntax:
<script src="path/to/file.js"></script>Using type='module' for modern JS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ES6 Module</title>
</head>
<body>
<h1>ES6 Module Script</h1>
<div id="result"></div>
<!-- Modern ES6 module -->
</body>
</html>Loading preview...
srcsrc="script.js"Path to external JavaScript file
typetype="module"Script type (module, text/javascript)
asyncasyncDownload in parallel, execute when ready
deferdeferDownload in parallel, execute after HTML
crossorigincrossorigin="anonymous"CORS requests for external scripts
integrityintegrity="sha384-..."Verify file integrity (SRI)