How to include inline JavaScript in HAML ?

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

%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);  

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

%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);  

chevron_right


Output:

Reference: http://haml.info/docs/yardoc/file.REFERENCE.html#javascript-filter

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : amarjeet_singh