How to include inline JavaScript in HAML ? - GeeksforGeeks

03 Jun, 2020
There are mainly two ways to include JavaScript in HTML:
Internal/Inline: By using “script” element in the “head” or “body” section.
External: By using an external JavaScript file.
We will be extending the below example for HTML and see how it could be implemented similarly for HAML. To include inline JavaScript in HTML, the “script” element is used in the “head” or “body” section and the element contains the JavaScript code in-between the opening and closing tags.
Consider the example below for an HTML file where we use JavaScript to insert a new “p” element into the “div” using inline JavaScript in the “body” section.
<!DOCTYPE html><html>  <head>    <title>HTML inline JavaScript demo</title></head>  <body>    <div id="container">        <p>Hi, no script applied yet!</p>    </div>      <script type="text/javascript">        var tag = document.createElement("p");        var text = document.createTextNode(                "Inline JavaScript is applied!");          tag.appendChild(text);        var element = document.getElementById("container");        element.appendChild(tag);      </script></body>  </html>
Output:
In HAML, instead of using the “script” tag, :javascript could be used. Take care of the indentations because HAML is indentation sensitive.
:javascript
    // JavaScript code goes here with proper indentation

So, the same initial HTML example in HAML becomes:
%html     %head         %title Basic Haml Template      %body        %div#container            %p Hi, no script applied yet!          :javascript            var tag = document.createElement("p");            var text = document.createTextNode(            "Inline JavaScript is applied!");            tag.appendChild(text);            var element = document.getElementById("container");            element.appendChild(tag);  
Output:
Another way of including inline JavaScript into HAML is by the use of %script{type: "text/javascript"}
%script{type: "text/javascript"}
    // JavaScript code goes here with proper indentation

This format is useful when you would want to use a different type than text/javascript.The initial example in HAML with the use of this method to include inline JavaScript:
%html     %head         %title Basic Haml Template          %body        %div#container            %p Hi, no script applied yet!                %script{type: "text/javascript"}            var tag = document.createElement("p");            var text = document.createTextNode(            "Inline JavaScript is applied!");            tag.appendChild(text);            var element = document.getElementById("container");            element.appendChild(tag);  
Output:
Reference: http://haml.info/docs/yardoc/file.REFERENCE.html#javascript-filter
amarjeet_singh
HTML-Misc
JavaScript-Misc
Picked
HTML
JavaScript
Web Technologies
Web technologies Questions
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
REST API (Introduction)
HTML Cheat Sheet - A Basic Guide to HTML
Design a web page using HTML and CSS
Form validation using jQuery
Angular File Upload
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to calculate the number of days between two dates in javascript?