MooTools – Functions


MooTools – Functions


”;


Functions in MooTools is a concept from JavaScript. We already know how to use functions in JavaScript. Generally, it is better to keep the function outside the page body in the script tag. In MooTools, we follow the same pattern. Here, you can design your own function according to the requirement. We now have to call all the user-defined functions in the domready function.

Take a look at the following syntax to understand how to use generalized function in MooTools.

Syntax

<script type = "text/javascript">
   /*
   Function definitions go here
   */
   window.addEvent(''domready'', function() {
      /* Calls to functions go here */
   });
</script>

Basic Structure

There are a few basic ways to define a function in MooTools. There is no difference between the function syntaxes of JavaScript and MooTools but the difference is in calling a function. Let us take a small example that defines a function named demo_function. Take a look at the following code.

Example

<html>

   <head>
      <script type = "text/javascript" src = "MooTools-Core-1.6.0.js"></script>
      <script type = "text/javascript" src = "MooTools-More-1.6.0.js"></script>
      
      <script type = "text/javascript">
         //Define simple_function as a function
         var simple_function = function(){
            document.write(''This is a simple function'');
         }
         
         window.addEvent(''domready'', function() {
            //Call simple_function when the dom(page) is ready
            simple_function();
         });
      </script>
   </head>
   
   <body>
   </body>
   
</html>

You will receive the following output −

Output