<script>
A block of JavaScript.

JavaScript: lz.script
extends <node> » lz.Eventable »

The script element contains JavaScript code that is executed when the application is loaded. This element must be at the canvas level; that is, it cannot be contained within any subordinate tag. If the src attribute is present, it names a JavaScript file whose contents are compiled into the application.

In the example below, we add a method to the built-in Array class that will let us find the index of elements in an array. Note that Array.find uses === for finding, so that two objects that are similar will not be confused. This is why looking for {example: 'sneaky'} finds nothing, whereas looking for sneaky succeeds.

<canvas debug="true" height="200">
   <debug y="5%" height="90%"/>
   <script><![CDATA[
   
     // Add a find method to Array
     Array.prototype.find = function ( what ) {
       for (var i = 0; i < this.length; ++i) {
         if (this[i] === what) {
           return i;
         }
       }
     }

     var sneaky = {example: 'sneaky'};
     var tryit = ['foo', 42, sneaky, Math.PI, false];

     Debug.format("42 is at: %s\n", tryit.find(42));
     Debug.format("false is at: %s\n", tryit.find(false));
     Debug.format("'bar' is at: %s\n", tryit.find('bar'));
     Debug.format("{example: 'sneaky'} is at: %s\n", tryit.find({example: 'sneaky'}));
     Debug.format("sneaky is at: %s\n", tryit.find(sneaky));
   
   ]]></script>
 </canvas>

Attributes

Name (CSS property) Type (tag) Type (js) Default Category
src string String   initialize-only
  The pathname of a Javascript file. This pathname is resolved relative to the file that contains the <script> element.

Methods

Methods inherited from lz.Eventable

destroy, setAttribute

Events

Events inherited from <node>

onconstruct, ondata, oninit

Events inherited from lz.Eventable

ondestroy