Buttons: Functions

When you create a button, the second parameter is the name of a function. When you assign one function to several buttons, exactly the same code will be executed for each of them. There is however a way to write generic functions for more than one button and yet get individual behavior. When a button is clicked, MapClient executes the function that was specified on button creation. However, it always passes the address of the button object as the first parameter to this function. This means that you can access all internal properties of the button from within the function. These are in themselves not very interesting, but you can assign your own values to the button when you create it, and use these in the function. This is a difficult concept, so let us try to make it clearer with an example: .

Buttons are created just like before, but with one addition: each button object is assigned a variable (called projName) which is filled with the string that MapServer uses to set this projection:
b1 = new button("Miller",setProjection)
b1.projName = "mill"

function setProjection(b) {
	var projString = "proj=" + b.projName + ",ellps=clrk66"
	w.setVar("map_projection",projString)
	submitWin()
}
All buttons have the same callback function: setProjection(): This function has one parameter: the button object that called it. Within the function the internal variables of this button can be accessed by button.<variable-name>. As we created a variable called projName for every button, the function can access this as b.projName, and build the projection string needed for MapServer. In this way, all projections available in MapServer are shown in .