Javascript Reference
Categories: STYLE

javascript STYLE setExpression( )

@March 15, 2008, 1:22 a.m.
setExpression( )Firefox/Netscape/NN n/a IE 5(Win) Chrome/Safari/DOM n/a  

setExpression("propertyName", "expression", ["scriptLanguage"])

  

Assigns a script expression to an element object's property as a way to calculate dynamically a value for the property. This method works with properties of element objects and their style objects if you like. The expression is re-evaluated automatically for most user-oriented events, or you may explicitly force re-evaluation at any time via the recalc( ) method.

 

Assigning an expression to an element attribute can take the place of some event handling, such as maintaining position relationships among elements when a user resizes the browser window. For example, to keep an element horizontally centered in the browser window, you could use one of the following techniques to apply an expression to the element's style.left property. The first example demonstrates the syntax (also for IE 5 for Windows or later) for assigning an expression as an inline attribute for the the element:

<div id="heading" style="position:absolute; left:expression(
  document.body.clientWidth/2-document.getElementById("heading").offsetWidth/2);
 

Alternatively, a function invoked at load time could include the following statement:

document.getElementById("heading").style.setExpression("left", 
 "document.body.clientWidth/2-document.getElementById('heading').offsetWidth/2;", 
 "JScript");
 

In both cases, the same expression calculates the coordinate position for the element's left edge relative to the current viewable width of the body element. Because this expression depends on a body element dimension property, the browser knows that it should re-evaluate any expression that might be impacted by a change in the body size caused by window resizing.

 

Be sure the resulting value of the expression you assign is the desired data type for the attribute you are setting. Isolate and run some initial tests on the expression before assigning it to the setExpression( ) method. Otherwise debugging will be more difficult.

 

If you want an expression to assign a value to an attribute and force that value to stick, use the removeExpression( ) method to prevent any further re-evaluation of the attribute value.

 
Parameters
 
  • The name of the attribute being controlled by the expression, but in case-sensitive property name form (e.g., use the className property name instead of the corresponding class attribute name).
  • A string that contains the script expression to be evaluated. The expression must evaluate to a value suitable for the property named in the first parameter, so multiple, semicolon-delimited statements are not allowed. References to other elements should be complete references. Early implementations may balk at references that include arrays.
  • One of three constant strings: JScript | JavaScript | VBScript. The default is JScript.
 
Returned Value

None.


Powered by Linode.