There may be times when you want several elements to respond to events or changes of state easily. To do this, we can use broadcasters.
Command Attribute Forwarding
We've already seen that elements such as buttons can be hooked up to commands. In addition, if you place the disabledcommandlabelcommand
var el = env.locale; Example 1 : Source View
<command id="my_command" label="Open"/> <button command="my_command"/> <checkbox label="Open in a new window" command="my_command"/>
In this example, the button does not have a label
If you were to modify the command's labeldisabled
This attribute forwarding is quite useful for a number of purposes. For instance, let's say that we want the Back action in a browser to be disabled. We would need to disable the Back command on the menu, the Back button on the toolbar, the keyboard shortcut (Alt+Left for example) and any Back commands on popup menus. Although we could write a script to do this, it is quite tedious. It also has the disadvantage that we would need to know all of the places where a Back action could be. If someone added a new Back button using an extension, it wouldn't be handled. It is convenient to simply disable the Back action and have all the elements that issue the Back action disable themselves. We can use the attribute forwarding of commands to accomplish this.
Broadcasters
There is a similar element called a broadcastercommandbroadcaster
The simplest broadcaster is shown below. You should always use an id
<broadcasterset> <broadcaster id="isOffline" label="Offline"/> </broadcasterset>
Any elements that are watching the broadcaster will be modified automatically whenever the broadcaster has its labelbroadcastersetbroadcasterset
Making elements observers
Elements that are watching the broadcaster are called observers because they observe the state of the broadcaster. To make an element an observer, add an observescommandcommand
<button id="offline_button" observes="isOffline"/>
The observesidisOffline, which is the one defined earlier. If the value of the labellabel
We could continue with additional elements. As many elements as you want can observe a single broadcaster. You can also have only one if you wanted to but that would accomplish very little since the main reason for using broadcasters is to have attributes forwarded to multiple places. You should only use broadcasters when you need multiple elements that observe an attribute. Below, some additional observers are defined:
<broadcaster id="offline_command" label="Offline" accesskey="f"/> <keyset> <key id="goonline_key" observes="offline_command" modifiers="accel" key="O"/> </keyset> <menuitem id="offline_menuitem" observes="offline_command"/> <toolbarbutton id="offline_toolbarbutton" observes="offline_command"/>
In this example, both the labelaccesskey
You can use a broadcaster to observe any attribute that you wish. The observers will grab all the values of any attributes from the broadcasters whenever they change. Whenever the value of any of the attributes on the broadcaster changes, the observers are all notified and they update their own attributes to match. Attributes of the observers that the broadcaster doesn't have itself are not modified. The only attributes that are not updated are the idpersist
Broadcasters aren't used frequently as commands can generally handle most uses. One thing to point out is that there really is no difference between the commandbroadcasterobserves
The Observes Element
There is also a way to be more specific about which attribute of the broadcaster to observe. This involves an observesobserves
var el = env.locale; Example 2 : Source View
<broadcasterset> <broadcaster id="isOffline" label="Offline" accesskey="f"/> </broadcasterset> <button id="offline_button"> <observes element="isOffline" attribute="label"/> </button>
Two attributes have been added to the observeselementattributeobservesbuttonaccesskeyobservesobservesobserves
Broadcast event
There is an additional event handler that we can place on the observesonbroadcast. The event is called whenever the observer notices a change to the attributes of the broadcaster that it is watching. An example is shown below.
var el = env.locale; Example 3 : Source View
<broadcasterset>
  <broadcaster id="colorChanger" style="color: black"/>
</broadcasterset>
<button label="Test">
  <observes element="colorChanger" attribute="style" onbroadcast="alert('Color changed');"/>
</button>
<button label="Observer"
  oncommand="document.getElementById('colorChanger').setAttribute('style','color: red');"
/>
Two buttons have been created, one labeled Test and the other labeled Observer. If you click on the Test button, nothing special happens. However, if you click on the Observer button, two things happen. First, the button changes to have red text and, second, an alert box appears with the message 'Color changed'.
What happens is the oncommandcolor that is red. The broadcaster is not affected by the style change because it doesn't display on the screen. However, the first button has an observer which notices the change in style. The elementattributeobserves
Next, because the broadcast occured, the event handler onbroadcast is called. This results in an alert message appearing. Note that the broadcast only occurs if the attributes on the broadcaster
If you tried duplicating the code for the first button
Next, we'll look at using the Document Object Model with XUL elements.