Different methods to embed JavaScript in HTML.
By: aathishankaran in JavaScript Tutorials on 2007-03-21
HTML gives you the capability to create remarkable static Web pages. Although these documents have been creative, interesting, and by all means useful, JavaScript gives you the capability to make these extensive static pages interactive and more responsive to user actions and input. Extending your HTML pages with JavaScript puts more power to your page and gives you more flexibility with what your HTML can do. JavaScript cannot stand-alone but is always tied to your HTML page and the browser. JavaScript is an interpreted language that is processed by the browser when the page loads.
JavaScript enables the Web developer to create pages that are more dynamic by embedding a scripting language in the existing HTML structure. You can now put processes behind buttons, run calculations on form-entered data, or perform actions when the user moves the mouse over an HTML object.
There are several ways to embed JavaScript code in HTML:
Inline Script:
You can add JavaScript code inside an HTML element by using the on attribute. For example:
<button onclick="alert('Hello World!')">Click Me</button>
Script Tag:
You can add JavaScript code using the <script> tag. You can include the code inside the tag, or reference an external file. For example:
<script>
function greet() {
alert('Hello World!');
}
</script> <script src="script.js"></script>
Event Handlers:
You can attach JavaScript code to an HTML element using event handlers. For example:
<button id="myButton">Click Me</button>
<script>
document.getElementById('myButton').addEventListener('click', function() {
alert('Hello World!');
});
</script>
URL Handlers:
You can use the javascript: protocol to execute JavaScript code when the user clicks on a link. For example:
<a href="javascript:alert('Hello World!')">Click Me</a>
External Library:
You can use an external library such as jQuery to add JavaScript code to your HTML page. For example:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$('#myButton').click(function() {
alert('Hello World!');
});
});
</script>
These are some of the ways to embed JavaScript code in HTML
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Comments