Sunday, December 29, 2024
Google search engine
HomeLanguagesJavascriptHow to include inline JavaScript in HAML ?

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.




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

RELATED ARTICLES

Most Popular

Recent Comments