spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / mozillaapps / chap5 / 2 To page 1To page 2To page 3current pageTo page 5
[previous] [next]

Creating Applications with Mozilla, Chapter 5: Scripting Mozilla

Sr Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


Creating Elements Dynamically

Using the createElement method in XUL lets you accomplish things similar to document.write in HTML, with which you can create new pages and parts of a web page. In Example 5-9, createElement is used to generate a menu dynamically.

Example 5-9: Dynamic menu generation

 <?xml version="1.0"?>
 <?xml-stylesheet href="test.css" type="text/css"?>
 <!DOCTYPE  window>
 <window id="test-win"
         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
         title="test"
         style="
         min-width : 200px;
         min-height: 200px;">
 <script>
 <![CDATA[
 function generate( ){
   var d           = document;
   var popup       = d.getElementById('menupopup');
   var menuitems   = new Array('menuitem_1',
                      'menuitem_2', 'menuitem_3',
                      'menuitem_4', 'menuitem_5');
   var l           = menuitems.length;
   var newElement;
   for(var i=0; i<l; i++)
   {
     newElement = d.createElement('menuitem');
     newElement.setAttribute('id', menuitems[i]);
     newElement.setAttribute('label', menuitems[i]);
     popup.appendChild(newElement);
   }
   return true;
 }
 ]]>
 </script>
 <menu label="a menu">
   <menupopup id="menupopup">
   </menupopup>
 </menu>
 <spacer flex="1" />
 <button id="ctlbutton" class="testButton" label="generate" oncommand="generate( );" />
 </window>

The JavaScript function generate( ) in Example 5-9 gets the menupopup as the parent element for the new elements, creates five menuitems in an array called menuitems, and stores five string ID names for those menuitems.

The variable l is the length of the array. The variable newElement is a placeholder for elements created by using the createElement method inside of the for loop. generate( ) assigns newElement on each iteration of the loop and creates a new menuitem each time, providing a way to dynamically generate a list of menu choices based on input data or user feedback. Try this example and experiment with different sources of data, such as a menu of different auto manufacturers, different styles on group of boxes that come from user selection, or tabular data in a tree.


home / programming / javascript / mozillaapps / chap5 / 2 To page 1To page 2To page 3current pageTo page 5
[previous] [next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: September 26, 2002
Revised: September 26, 2002

URL: http://webreference.com/programming/javascript/mozillaapps/chap5/2/4.html