Join Regular Classroom : Visit ClassroomTech

JavaScript – codewindow.in

Related Topics

HTML

Introduction
Html page 1
Html page 2
Html page3
Html page4

HTML Elements and structure
Html page 5
Html page 6
Html page 7

HTML Headings and Paragraphs
Html page 8
Html page 9
Html page 10

HTML Lists and Tables
Html page 11
Html page 12
Html page 13

HTML Forms and Input Fields
Html page 14
Html page 15
Html page 16

HTML Images and Media
Html page 17
Html page 18

HTML Links and Anchors
Html page 19
Html page 20
Html page 21

HTML Styles and Formatting
Html page 22

HTML Semantic Elements
Html page 23
Html page 24

HTML Attributes
Html page 25
Html page 26

HTML JavaScript Integration
Html page 27
Html page 28
Html page 29
Html page 30

HTML Document and Browser Support
Html page 31
Html page 32

HTML5 New Elements and Attributes
Html page 33
Html page 34
Html page 35
Html page 36

HTML Accessibility and Web Standards
Html page 37
Html page 38
Html page 39

HTML Responsive Design and Mobile Devices.
Html page 40
Html page 41
Html page 42

JAVASCRIPT




  <title>Dynamic Web Page</title>


  <h1 id="greeting">Hello, World!</h1>

  <button>Change Greeting</button>

  


JavaScript (script.js):
function changeGreeting() {
  var greetingElement = document.getElementById("greeting");
  greetingElement.textContent = "Welcome to the Dynamic Web Page!";
}
In this example, we have an HTML page with a heading element (<h1>) displaying the greeting “Hello, World!” and a button element. When the button is clicked, the JavaScript function changeGreeting() is executed.
The JavaScript code retrieves the element with the id “greeting” using document.getElementById(). It then updates the text content of the element using the textContent property. In this case, the greeting is changed to “Welcome to the Dynamic Web Page!”.
When the button is clicked, the JavaScript code dynamically modifies the HTML element, and the updated text is immediately reflected on the web page without the need for a page refresh.
This is just a simple example, but JavaScript can be used to perform more complex updates, such as manipulating multiple elements, changing styles, making AJAX requests to fetch and display data from a server, and much more.



  <title>JavaScript in HTML</title>
  
    // Inline JavaScript code
    function greet() {
      console.log('Hello, world!');
    }
    greet();
  


  <!-- HTML content -->


  1. External JavaScript File: You can also include an external JavaScript file by linking it in the HTML file using the <script> tag’s src attribute. Create a separate .js file and specify its path using the src attribute. For example:



  <title>JavaScript in HTML</title>
  


  <!-- HTML content -->


JavaScript (script.js):
// External JavaScript code
function greet() {
  console.log('Hello, world!');
}
greet();
Make sure the src attribute value points to the correct location of the JavaScript file.
  1. Asynchronous Loading: If you want to load JavaScript asynchronously to improve page loading performance, you can use the async or defer attribute with the <script> tag.
  • <script async src="script.js"></script>: The script is fetched asynchronously while the HTML parsing continues. Once the script is fetched, it is executed immediately. This can be useful for scripts that don’t depend on the DOM.
  • <script defer src="script.js"></script>: The script is fetched asynchronously but is executed only after the HTML parsing is complete. This is useful when the script depends on the DOM or requires a specific order of execution.
Remember to place the <script> tag appropriately in the HTML document, depending on when you want the JavaScript code to be executed.
By using these methods, you can effectively include JavaScript code in an HTML file and make it executable within a web page.

      

Popular Category

Topics for You

HTML

Introduction
Html page 1
Html page 2
Html page3
Html page4

HTML Elements and structure
Html page 5
Html page 6
Html page 7

HTML Headings and Paragraphs
Html page 8
Html page 9
Html page 10

HTML Lists and Tables
Html page 11
Html page 12
Html page 13

HTML Forms and Input Fields
Html page 14
Html page 15
Html page 16

HTML Images and Media
Html page 17
Html page 18

HTML Links and Anchors
Html page 19
Html page 20
Html page 21

HTML Styles and Formatting
Html page 22

HTML Semantic Elements
Html page 23
Html page 24

HTML Attributes
Html page 25
Html page 26

HTML JavaScript Integration
Html page 27
Html page 28
Html page 29
Html page 30

HTML Document and Browser Support
Html page 31
Html page 32

HTML5 New Elements and Attributes
Html page 33
Html page 34
Html page 35
Html page 36

HTML Accessibility and Web Standards
Html page 37
Html page 38
Html page 39

HTML Responsive Design and Mobile Devices.
Html page 40
Html page 41
Html page 42

Go through our study material. Your Job is awaiting.

Recent Posts
Categories