HTML JavaScript

HTML JavaScript enhances the functionality and interactivity of HTML documents.

Web sites can be made more engaging, dynamic, and interactive with the use of a script, which is a short programme used in conjunction with HTML to achieve these goals (think: alert popup window on mouse click). Nowadays, JavaScript is the most widely used scripting language for websites.

The <script> element of HTML Tag

To identify a script that runs on the client, use the HTML <script> tag. For this reason, the <script> tag can go either in the body or the head, depending on whether the scripting statements are located in an internal or external JavaScript.

Its primary functions are dynamic content modification, form validation, and picture manipulation. To target a specific HTML element, JavaScript makes use of the document.getElementById() function.

Example:

<!DOCTYPE html>
<html>
<body>

<h1>First JavaScript</h1>

<h3  id=\”demo\”>JavaScript can change the style of an HTML element.</h3>

<script>
function testFunction() {

document.getElementById(\”demo\”).style.backgroundColor = \”blue\”;
document.getElementById(\”demo\”).style.fontSize = \”40px\”;
document.getElementById(\”demo\”).style.color = \”white\”;
}
</script>

<button type=\”button\” onclick=\”testFunction()\”>Click Me!</button>

</body>
</html>

 

HTML events with JavaScript

If we want something to happen whenever the user or browser triggers an event—for example, when the mouse is clicked or when a page loads—JavaScript plays a crucial role.

The HTML specification includes event handler characteristics that, when combined with JavaScript, allow enable a page to respond to a certain event.

Example

<input type=\”button\” value=\”Click Here\” onclick=\”alert(\’Hi, testing javascript\’)\”>

The following are examples of events that can be included in HTML:

  • Reset, submit, and other form events are examples.
  • Choose the appropriate action: text box, text area, etc.
  • Focusing on the focus, blurring, etc.
  • Select, MouseUp, MouseMove, MouseDown, Click, Double-Click, etc. are all examples of mouse events.

The properties of Window events are as follows:

Event Event Name Handler Name Occurs when
onBlur blur When a form input loses focus
onClick click When the user clicks on a form element or a link
onSubmit submit When a user submits a form to the server.
onLoad load When a page loads in a browser.
onFocus focus When a user focuses on an input field.
onSelect select When a user selects the form input filed.

Use External Script

If many HTML files need to share a common script, we can isolate the script in its own file and reference it from another, where it will run as expected. Use the.js file extension when saving a JavaScript file to disc.

Take care not to include the script> tag when linking to an external file, and give the full location of the JS file.

Example

<!DOCTYPE html>
<html>
<head>
<script type=\”text/javascript\” src=\”quesry.js\”></script>
</head>
<body>
<h2>External JavaScript Example</h2>
<form onsubmit=\”submit()\”>
<input type=\”text\” name=\”name\” placeholder=\”Enter your name\” id=\”name\” /><br>

<input type=\”email\” placeholder=\”Enter your Email-address\” name=\”email\” id=\”email\” /><br>
<input type=\”submit\”>
</form>
</body>
</html>

quesry.js file js code

function submit() {
var x = document.getElementById(\”frm1\”).value;
alert(\”Hi\”+\” \”+x+ \”you have successfully submitted the details\”);
}

HTML <noscript> Tag

The <noscript> tag in HTML is used to prevent the browser from displaying script. The browser will not show the content between the <noscript> and </noscript> tags.

Example:

<!DOCTYPE html>
<html>
<body>
<h2 id=\”test\”>click here</h2>
<script>
document.getElementById(\”test\”).innerHTML = \”Testing JavaScript!\”;
</script>
<noscript>This text is not visible in the browser.</noscript>
</body>
</html>

HTML Script Tags

Tag Description
<script> Defines a client-side script
<noscript> Users who do not support client-side scripts are directed to an alternative content that is defined here.
Related Posts
Introduction to HTML

HTML Introduction HTML is the most common language used to mark up Web pages. What does HTML mean? Hyper Text Read more

What is HTML

HTML stands for Hyper Text Markup Language, which is a programming language used to make web pages and web apps. Read more

HTML Basic Examples

In this chapter, we\'ll show you some simple examples of HTML code. Don\'t worry if we use tags that you Read more

HTML text Editors

Editors for HTML text HTML files are text files, so any text editor can be used to make them. Text Read more

HTML Tags

Learn HTML Tags HTML tags are kind of like keywords that tell a web browser how to format and show Read more

HTML Elements

Elements make up an HTML file. These elements are what make web pages and tell you what content is on Read more

HTML Attributes

Attribute in HTML Attributes in HTML are special words that tell you more about an element. Attributes change the way Read more

HTML Headings

Headings in HTML The <h1> to <h6> tags are used to set up headings in HTML. <h1> shows which heading Read more

Scroll to Top