Monday, February 27, 2006

What is the difference between the name attribute and the id attribute?

This is not a question about HTML; it is a question about the Laszlo programming language. It has bothered me no end as I began running through Laszlo sample code, and I just found the answer today.

Q: What is the difference between the name attribute and the id attribute?

A: In Chapter 4 sec 2.2.2 of the Software Engineer's Guide to Developing OpenLaszlo Applications, we see

"In LZX the id attribute of an object is a global identifier that is visible throughout the entire program space, while the name of an object is an attribute like any other, which can only be referenced by its path (except in the case of named children of the canvas, as noted below)."

There's a simple example at their page, and here is some code that looks at a few different possibilities:
<canvas debug="true" width="800">
<simplelayout axis="y"/>
<view id="blue_id" name="blue_view" height="100" width="800" bgcolor="blue">
<simplelayout axis="y"/>
<!--
You can get the height of enclosing view by using its name or its id. Here I use its name.
-->
<text id="show_height_id" name="show_height" text="${blue_view.height}" bgcolor="yellow" width="800"/>
<!-- you cannot do this, show_height is not a child of canvas -->
<text text="${'Item show_height cannot be found by its name even here: ' + show_height.width}" width="800"/>
</view>
<!--
You can do the following, because the view "blue_view" is a "named child of the canvas". That is, it is a child of the canvas and it has a name attribute.
-->
<text text="${'Item blue_view is a named child of canvas; we can refer to it by name to get its height: ' + blue_view.height}" width="800"/>
<!-- you cannot do this; "show_height" is a child of view "blue_id" and is not visible here -->
<text text="${'Item show_height cannot be found by its name: ' + show_height.text}" width="800"/>
<!-- you can do this instead; the id of this item is globally visible -->
<text text="${'But item show_height_id can be found by its id: ' + show_height_id.text}" width="800"/>
</canvas>
If you run this code you see "undefined" anywhere it refers to an object that was out of scope.

Anyway, given all that, I wonder why give anything a "name"; if you need an identifier, just use "id". It's not like named objects have private scope; you can get to them by referring to their path (for example, in the above code, blue_id.show_height.text would work in place of show_height.text).

0 Comments:

Post a Comment

<< Home