HTML JavaScript (js)

In HTML, you can use JavaScript to interact with the DOM (Document Object Model) and manipulate the content and behavior of your web page dynamically.
Mainly Javascript is used to make pages interative.
Let's see some of the methods how javascript is used in HTML.


Inline Javascript

You can include JavaScript code directly within your HTML file using the <script> tag.
This method is suitable for small snippets of code.
Basically these script tag are kept in body and head tags.

Example
<!DOCTYPE html>
<html>
<head>
    <title>Inline JavaScript</title>
</head>
<body>
    <h1>Hello, world!</h1>

    <script>
        // Inline JavaScript code
        alert('This is an inline JavaScript alert!');
    </script>
</body>
</html>

External Javascript

For more extensive JavaScript code, it's better to put the code in an external JavaScript file and link it to your HTML using the <script> tag's src attribute.
These script tag are also kept in body and head tags.

Example
<!DOCTYPE html>
<html>
<head>
    <title>External JavaScript</title>
    <script src="script.js" defer></script>
</head>
<body>
    <h1>Hello, world!</h1>
</body>
</html>

Handling Events

You can also use JavaScript to handle events such as button clicks, form submissions, etc.

Example
<!DOCTYPE html>
<html>
<head>
    <title>Event Handling</title>
</head>
<body>
    <button id="clickButton">Click Me</button>

    <script>
        document.getElementById('clickButton').addEventListener('click', function() {
            alert('Button clicked!');
        });
    </script>
</body>
</html>

<noscript> tag

The <noscript> tag in HTML is used to provide alternative content or instructions that should be displayed or executed if a web browser doesn't support or has disabled JavaScript.
When a browser encounters the <noscript> tag, it will display the content inside the tag only if JavaScript is not available or enabled in the user's browser.

Example
<!DOCTYPE html>
<html>
<head>
    <title>JavaScript Support</title>
</head>
<body>
    <h1>Hello, world!</h1>

    <noscript>
        <p>Please enable JavaScript to enjoy the full functionality of this website.</p>
    </noscript>
</body>
</html>

Quick Recap - Topics Covered

HTML JavaScript
Inline JavaScript
External JavaScript
Handling Events
<noscript> Tag

Practice With Examples in Compilers

The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compile or Try on own Now


Example 1
Example 1 Example 2 Example 3 Example 4 Example 5


Quiz


FEEDBACK