Internal and External JavaScript

  • Internal JavaScript and External JavaScript are two ways to add JavaScript code to an HTML web page.
  • Internal JavaScript is code that is included directly in the HTML document.
  • This is typically done by including the JavaScript code between script tags in the head or body section of the HTML file.
For example:
<!DOCTYPE html>
<html>

<head>
    <script>
      // Internal JavaScript code here
    </script>
</head>

<body>
    <h1>Hello, world!</h1>
    <script>
      // More internal JavaScript code here
    </script>
</body>

</html>
  • Internal JavaScript is easy to include and can be useful for small code snippets, but it can make the HTML document larger and harder to read.
  • Additionally, it can be difficult to reuse internal JavaScript code across multiple pages.
  • External JavaScript is code that is stored in a separate file and referenced by the HTML document.
  • This is typically done by using a script tag with the src attribute set to the URL of the external JavaScript file.
For example:
<!DOCTYPE html>
<html>

<head>
    <script src="my-script.js"></script>
</head>

<body>
    <h1>Hello, world!</h1>
    <script src="other-script.js"></script>
</body>

</html>
  • External JavaScript files can be reused across multiple pages, making it easier to maintain and update the code.
  • They also allow for better separation of concerns between HTML and JavaScript, making the code easier to read and maintain.
  • In summary, Internal JavaScript is included directly in the HTML document, while External JavaScript is stored in a separate file and referenced by the HTML document.
  • Both have their advantages and disadvantages, and the choice between the two will depend on the specific requirements of the project.

No comments:

Post a Comment