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:
You can add JavaScript code inside an HTML element by using the on attribute. For example:
<button onclick="alert('Hello World!')">Click Me</button>
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>
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>
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>
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
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
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:
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.
Most Viewed Articles (in JavaScript )
Dynamically modify the option set in Dynamics 365 forms
promise and .then() in JavaScript
reduce() and filter() in JavaScript
History and evolution of Javascript
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Show how many characters remaining in a html text box using javascript
Latest Articles (in JavaScript)
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate