How many ways we can add javascript

JavaScript can be added to a web page in several ways:

  1. Inline Script: JavaScript code can be directly included within HTML elements using the onclick, onload, or other event attributes. For instance:

                    
                        <button onclick="myFunction()">Click me</button>
                        <script>
                            function myFunction() {
                                alert("Hello, World!");
                            }
                        </script>                    
                    
                

  2. Internal Script: JavaScript can be placed within the <script> tag in the HTML document, usually in the <head> or <body> section. This method allows writing scripts directly in the HTML file.

                    
                        <script>
                        function myFunction() {
                            alert("Hello, World!");
                        }
                        </script>                
                    
                

  3. External File: JavaScript code can be placed in an external file with a .js extension and included in HTML using the <script> tag with the src attribute pointing to the file path.

    HTML file:

                    
                        <script src="script.js"></script>
                    
                

    JavaScript file (script.js):

                    
                        function myFunction() {
                            alert("Hello, World!");
                        }                    
                    
                

  4. Asynchronous Loading: Using the async attribute in the script tag allows the browser to download the script asynchronously while parsing the HTML. This can enhance page loading performance by not blocking other processes.

                    
                        <script src="script.js" async></script>
                    
                

  5. Dynamic Script Insertion: JavaScript can create and append script elements dynamically to the DOM, allowing for loading scripts based on certain conditions or events.

                    
                        const script = document.createElement('script');
                        script.src = 'script.js';
                        document.body.appendChild(script);                    
                    
                

  6. Defer Loading: The defer attribute in the script tag delays script execution until the HTML parsing is complete. Multiple deferred scripts are executed in the order they appear in the document.

                    
                        <script src="script.js" defer></script>
                    
                

Each method has its advantages and use cases, depending on factors such as script size, performance requirements, and the need for modularity in the codebase.

Streamline Data Serialization and Versioning with Confluent Schema Registry …

Using Confluent Schema Registry with Kafka can greatly streamline data serialization and versioning in your messaging system. Here's how you can set it up and utilize it effectively: you can leverage Confluent Schema Registry to streamline data seria …

read more

How To Set Up an Ubuntu Server on a DigitalOcean Droplet

Setting up an Ubuntu Server on a DigitalOcean Droplet is a common task for deploying web applications, hosting websites, running databases, and more. Here's a detailed guide to help you through the process. Setting up an Ubuntu server on a DigitalOce …

read more