External Scripts - JavaScript Interview
JavaScript Fundamentals: EXTERNAL SCRIPTS
What is an external script in JavaScript?
View Answer:
How do you access external script files in JavaScript development?
View Answer:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script src="myscript.js"></script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
How do you access external script links in JavaScript development?
View Answer:
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
<script src="https://example.com/myscript.js"></script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
What is the rule for putting scripts into HTML?
View Answer:
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
<script>
function myFunction() {
console.log("I'm Simple");
}
</script>
</head>
<body>
<h1>Hello World!</h1>
<script src="complex.js"></script>
</body>
</html>
What is the benefit of using a separate script file in relation to the browser?
View Answer:
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
<script src="myscript.js"></script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Can a script tag reference an external file and have code inside of it?
View Answer:
<script src="file.js">
console.log(1); // the content is ignored, because src is set
</script>
The example above can be split into two scripts to work:
<script src="file.js"></script>
<script>
console.log('Now it works!');
</script>
What are the advantages of using external scripts?
View Answer:
Is it possible to link multiple external scripts in a single HTML file?
View Answer:
<!DOCTYPE html>
<html>
<head>
<script src="path/to/script1.js"></script>
<script src="path/to/script2.js"></script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Can you include JavaScript code directly in an HTML file instead of using an external script?
View Answer:
<!DOCTYPE html>
<html>
<head>
<script>
function greet(name) {
console.log("Hello, " + name + "!");
}
</script>
</head>
<body>
<button onclick="greet('John')">Click me</button>
</body>
</html>
Including JavaScript code directly in an HTML file can be useful for adding functionality to specific elements on the page or for small scripts that do not require their own file. However, it can lead to code duplication and decreased maintainability if used excessively. Using external scripts can help to improve code organization and reuse.
What is the difference between using an external script and including JavaScript code directly in an HTML file?
View Answer:
// script.js
function greet(name) {
console.log("Hello, " + name + "!");
}
Below index.html loads script.js above
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<script src="script.js"></script>
</head>
<body>
<button onclick="greet('John')">Click me</button>
</body>
</html>
Can you use external scripts in conjunction with inline scripts?
View Answer:
<!DOCTYPE html>
<html>
<head>
<head>
<script src="path/to/script.js"></script>
<script>
// Inline script
console.log("This is an inline script.");
</script>
</head>
</head>
<body>
<button onclick="greet('John')">Click me</button>
</body>
</html>
Using external scripts in conjunction with inline scripts can be useful for adding functionality to specific elements on the page or for handling events that are specific to a particular section of the page. However, it's important to ensure that scripts are loaded and executed in the correct order to avoid unexpected behavior.
How can you ensure that your external scripts are loaded and executed correctly?
View Answer:
let script = document.createElement("script");
script.src = "path/to/script.js";
script.onload = function() {
console.log("Script loaded and executed successfully.");
};
script.onerror = function() {
console.error("Failed to load script.");
};
document.head.appendChild(script);
Using the onload and onerror events to ensure that external scripts are loaded and executed correctly can help to prevent errors and ensure that our code runs as intended.