Applying JavaScript to HTML(internal and external)
JavaScript is a powerful scripting language that can be used to add interactivity and dynamic functionality to web pages. JavaScript code can be included within a web page's HTML code, or it can be linked to from an external file. Here's an overview of how to use JavaScript internally and externally:
Internal JavaScript:
In the head section of your HTML code, add a script tag with the type attribute set to "text/javascript".
Add your JavaScript code within the script tags.
For example:
<head>
Add your JavaScript code within the script tags.
For example:
<head>
<script type="text/javascript">
function sayHello()
{
alert("Hello, world!");
}
</script>
</head>
Call your JavaScript function from within your HTML code.
For example:
<body>
<body>
<button onclick="sayHello()">Click me</button>
</body>
In this example, clicking the button will trigger the sayHello() function, which displays an alert message saying "Hello, world!".
External JavaScript:
Create a new file with a .js extension, such as "script.js".
Write your JavaScript code within this file.
In this example, clicking the button will trigger the sayHello() function, which displays an alert message saying "Hello, world!".
External JavaScript:
Create a new file with a .js extension, such as "script.js".
Write your JavaScript code within this file.
For example:
function sayHello()
function sayHello()
{
alert("Hello, world!");
}
In your HTML code, add a script tag with the src attribute set to the path of your external JavaScript file.
In your HTML code, add a script tag with the src attribute set to the path of your external JavaScript file.
For example:
<head>
<script src="script.js"></script>
</head>
<head>
<script src="script.js"></script>
</head>
Call your JavaScript function from within your HTML code, just as you would with internal JavaScript.
For example:
<body>
<body>
<button onclick="sayHello()">Click me</button>
</body>
In this example, clicking the button will trigger the sayHello() function, which is defined in the external script file "script.js".
Tags:
JS