- JavaScript Introduction
- JavaScript Where To
- JavaScript Output
- JavaScript Statements
- JavaScript Syntax
- JavaScript Comments
- JavaScript Variables
- JavaScript Let
- JavaScript Const
- JavaScript Operators
- JavaScript Arithmetic
- JavaScript Assignment
- JavaScript Data Types
- JavaScript Functions
- JavaScript Objects
- JavaScript Events
- JavaScript Strings
- JavaScript String Methods
- JavaScript Numbers
- JavaScript Number Methods
- JavaScript Arrays
- JavaScript Array Const
- JavaScript Array Methods
- JavaScript Sorting Arrays
- JavaScript Array Iteration
- JavaScript Date Objects
- JavaScript Date Formats
- JavaScript Get Date Methods
- JavaScript Set Date Methods
- JavaScript Math Object
- JavaScript Random
- JavaScript Booleans
- JavaScript Comparison And Logical Operators
- JavaScript If Else And Else If
- JavaScript Switch Statement
- JavaScript For Loop
- JavaScript Break And Continue
- JavaScript Type Conversion
- JavaScript Bitwise Operations
- JavaScript Regular Expressions
- JavaScript Errors
- JavaScript Scope
- JavaScript Hoisting
- JavaScript Use Strict
- The JavaScript This Keyword
- JavaScript Arrow Function
- JavaScript Classes
- JavaScript JSON
- JavaScript Debugging
- JavaScript Style Guide
- JavaScript Best Practices
- JavaScript Common Mistakes
- JavaScript Performance
- JavaScript Reserved Words
- JavaScript Versions
- JavaScript History
- JavaScript Forms
- JavaScript Validation API
- JavaScript Objects
- JavaScript Object Properties
- JavaScript Function Definitions
- JavaScript Function Parameters
- JavaScript Function Invocation
- JavaScript Closures
- JavaScript Classes
- Java Script Async
- JavaScript HTML DOM
- The Browser Object Model
- JS Ajax
- JavaScript JSON
- JavaScript Web APIs
- JS Vs JQuery
JavaScript Where To
The Script Tag
JavaScript code is inserted in HTML between <script> and </script> tags.
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>
The type attribute is optional. HTML's default scripting language is JavaScript.
JavaScript Functions And Events
A JavaScript function is a block of executable code that can be "called" for execution.
A function may be invoked, for instance, when an event occurs, such as when the user clicks a button.
JavaScript In Head Or Body
Any number of scripts can be included in an HTML document.
Scripts can be placed in the <body>, <head>, or both sections of an HTML page.
JavaScript in <head>
In this example, a JavaScript function is placed in the HTML page's <head> section.
When a button is clicked, the following function is invoked:
<html>
<head>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</head>
<body>
<h1>A Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>
JavaScript in <body>
In this example, a JavaScript function is placed in the HTML page's <body> section.
When a button is clicked, the following function is invoked:
<html>
<body>
<h1>A Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</body>
</html>
Because script interpretation slows down the display, placing scripts at the bottom of the <body> element improves the display speed.
External JavaScript
Scripts can be stored in external files as well:
External file: myScript.js
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
External scripts are advantageous when the same code is utilised on numerous web pages.
JavaScript files have the file extension .js.
To use an external script, put the name of the script file in the src
(source) attribute of a <script>
tag:
You may place an external script reference in either the <head> or <body> tags.
The script will operate in a manner consistent with its actual location.
External scripts cannot contain <script>
tags.
External JavaScript Advantages
The following benefits come from storing scripts in external files:
- It divides HTML from code.
- It facilitates reading and maintaining HTML and JavaScript.
- JavaScript cached files can accelerate page loads.
Use multiple script tags to add many script files to a single page:
<script src="myScript2.js"></script>
External References
Both a full URL and a path that is relative to the currently-viewed web page can be used to refer to external scripts.
This example links to a script using the complete URL:
???????
COMPANY
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP