External Scripts
JavaScript Fundamentals: EXTERNAL SCRIPTS
How do you access external script files in JavaScript development?
View Answer:
Interview Response: Script files are attached to HTML with the src attribute, including the JS file's absolute path.
Code Example:
<script src="/path/to/script.js"></script>
<!-- Example of Multiple script Paths… -->
<script src="/js/script1.js"></script>
<script src="/js/script2.js"></script>
How do you access external script URLs in JavaScript development?
View Answer:
Interview Response: We can access external scripts by using the script-src attribute. Both secure and non-secure domains are permissible.
Code Example:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/ "></script>
What is the rule for putting scripts into HTML?
View Answer:
Interview Response: As a rule, only the most straightforward scripts go into HTML pages. Complex scripts reside in separate files.
What is the benefit of using a separate script file in the browser?
View Answer:
Interview Response: The benefit of a separate file is that the browser downloads and stores it in its cache. Instead of downloading it on every load, other pages referencing the same script take it from the cache. That reduces traffic and makes pages faster.
What limitations exist in script tags using a source file?
View Answer:
Interview Response: A single script tag cannot have both the source attribute and code inside.
Code Example:This is not allowed
<script src="file.js">
alert(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>
alert('Now it works!');
</script>