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.

How To Set Up a Multi-Node Kafka Cluster using KRaft

Setting up a multi-node Kafka cluster using KRaft (Kafka Raft) mode involves several steps. KRaft mode enables Kafka to operate without the need for Apache ZooKeeper, streamlining the architecture and improving management. Here’s a comprehensiv …

read more

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