spacer
Yehuda Shiran July 19, 2000
The setExpression() Method
Tips: July 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
ActiveState Debuts Open Source Business Suite
Salesforce Offers Visual App Builder
Codesion Steps Out From CVS's Shadow

Dynamic properties were first introduced in Internet Explorer 5. This feature allows you to specify an element property as a function of properties of other elements. Let's take an example, and demonstrate with it how you did property assignment before and after Internet Explorer 5. Without dynamic properties, you centered a DHTML element horizontally like this:

object.style.left = document.body.clientWidth/2 - object.offsetWidth/2;

and vertically:

object.style.top = document.body.clientHeight/2 - object.offsetHeight/2;

After the initial placement, the appearance of the DHTML element on the page is subject to changes in the content or size of the client area. Normally, you had to detect events such as onresize and constantly update the position values whenever the client is resized. With dynamic properties, you would do something like that:

oDiv.style.setExpression("left", 
  "document.body.clientWidth/2 - oDiv.offsetWidth/2");
oDiv.style.setExpression("top",
  "document.body.clientHeight/2 - oDiv.offsetHeight/2");

But you have to do all initial setting upon loading of the page. The following code is a complete code of a DIV element that is being positioned at the center of the client area, using dynamic properties:

<SCRIPT>
window.onload = fnInit;
function fnInit() {
  oDiv.style.setExpression("left", 
  "document.body.clientWidth/2 - oDiv.offsetWidth/2");
  oDiv.style.setExpression("top",
  "document.body.clientHeight/2 - oDiv.offsetHeight/2");
}
</SCRIPT>
<DIV ID="oDiv" STYLE="position: absolute"; top: 0; left: 0>
Example DIV
</DIV>

Notice that you have to define in the DIV statement the STYLE parameters you want to set as dynamic properties. For more on Dynamic Properties, go to Column 65, Dynamic Properties.


People who read this tip also read these tips:

Look for similar tips by subject:


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Use Web Caching to Make Your Web Site Faster · Creating an Online Shopping Cart Mechanism in PHP · Log JavaScript Errors Using an AJAX-driven Web Service
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Configuring Granular Settings for a Database Level Audit · The Perils of a Web 2.0 Transition on Your Business Processes · Facebook Redesigns Site —Again — Nears 400M Mark