Home > Articles > Web Development > Ajax and JavaScript

Like this article? We recommend

Like this article? We recommend

Creating a Custom Object Front End

The library of cross-browser code that I'll introduce to you in the rest of this chapter uses as its base a custom object front end, as described in general terms earlier in this chapter. More specifically, I use code to create a custom object for every <div> and <span> tag in the current document because these are the elements that you can manipulate via DHTML in all three object models.

The next couple of sections show you the structure of these custom objects as well as the code that constructs them.

Examining the Object

Listing 1 shows the dhtml_object() constructor function that's used to create each custom object.

Listing 1: The Constructor Function for the Custom Object

function dhtml_object (obj, css, id) {
  this.obj = obj
  this.css = css
  this.id = id
  this.left = get_left
  this.right = get_right
  this.top = get_top
  this.bottom = get_bottom
  this.width = get_width
  this.height = get_height
  this.visibility = get_visibility
  this.zIndex = get_zIndex
  this.move_to = move_to
  this.move_by = move_by
  this.set_left = set_left
  this.set_top = set_top
  this.set_width = set_width
  this.set_height = set_height
  this.set_visibility = set_visibility
  this.set_zIndex = set_zIndex
  this.move_above = move_above
  this.move_below = move_below
  this.set_backgroundColor = set_backgroundColor
  this.set_backgroundImage = set_backgroundImage
  this.set_html = set_html
  this.get_clip_top = get_clip_top
  this.get_clip_right = get_clip_right
  this.get_clip_bottom = get_clip_bottom
  this.get_clip_left = get_clip_left
  this.get_clip_width = get_clip_width
  this.get_clip_height = get_clip_height
  this.resize_clip_to = resize_clip_to
  this.resize_clip_by = resize_clip_by
}

Table 1 summarizes the properties and methods in this object.

Table 1 Properties and Methods of the Custom Object

Property/Method

Description

Properties

obj

The object to which the custom object serves as a front end.

css

A reference that specifies the property that the surfer's browser uses to access CSS style properties (more on this a bit later).

id

The original object's id value.

Properties Implemented as Functions

left

The position, in pixels, of the left edge of the object. This "property" is actually the result of the custom get_left() function.

right

The position, in pixels, of the right edge of the object, as given by the custom get_right() function.

top

The position, in pixels, of the top edge of the object, as given by the custom get_top() function.

bottom

The position, in pixels, of the bottom edge of the object, as given by the custom get_bottom() function.

width

The width, in pixels, of the object, as given by the custom get_width() function.

height

The height, in pixels, of the object, as given by the custom get_height() function.

visibility

The visibility style of the object, as given by the custom get_visibility() function.

zIndex

The z-index of the object, as given by the custom get_zIndex() function.

Methods Implemented as Functions

move_to

Moves the object to the specified position.

move_by

Moves the object by the specified amount.

set_left

Sets the position of the object's left edge.

set_top

Sets the position of the object's top edge.

set_width

Sets the object's width.

set_height

Sets the object's height.

set_visibility

Sets the object's visibility.

set_zIndex

Sets the object's z-index.

move_above

Moves the object on top of a specified object.

move_below

Moves the object below a specified object.

set_backgroundColor

Sets the object's background color.

set_backgroundImage

Sets the object's background image.

set_html

Sets the tags and text that appear inside the object.

get_clip_top

Gets the position of the top edge of the object's current clipping region.

get_clip_right

Gets the position of the right edge of the object's current clipping region.

get_clip_bottom

Gets the position of the bottom edge of the object's current clipping region.

get_clip_left

Gets the position of the left edge of the object's current clipping region.

get_clip_width

Gets the width of the object's current clipping region.

get_clip_height

Gets the height of the object's current clipping region.

resize_clip_to

Resizes the object's current clipping region to the specified coordinates.

resize_clip_by

Resizes the object's current clipping region by the specified amounts.


Cross-Browser Style References

Of the various properties and methods shown in Table 1, one of the most important from a cross-browser point of view is the css property. This property holds information on how the user's browser refers to styles. To see why this is important, let's look again at how each of the three object models refer to styles.

For the W3C DOM, you first get a reference to an object using getElementById() and then access the style property. For example, here's some code that gets a reference to a <div> tag with id equal to my_div and then sets the element's left style:

document.getElementById("my_div").style.left = 0

For Internet Explorer 4's DHTML DOM, you use document.all to get a reference to the element and then use the style property. Here's an example:

document.all.my_div.style.left = 0

For Netscape 4's LDOM, you use document.layers to get a reference to the element and then set the style attribute directly. Here's an example:

document.layers.my_div.left = 0

These are quite different, as you can see. To "hide" this difference from your code, the custom dhtml_object uses its css property, which holds the object reference followed by whatever the browser uses to access styles. Here's what the css property stores for the above examples:

W3C DOM: document.getElementById("my_div").style

Internet Explorer 4: document.all.my_div.style

Netscape 4: document.layers.my_div

The custom object hides all this inside css, so to set any style, you use the following syntax:

Custom_Object.css.Style = Value

Custom_Object

A reference to the custom object

Style

The name of the style

Value

The value of the style


For example, if the current_object variable references one of these custom objects, you'd use the following statement to set the value of the left style to 0:

current_object.css.left = 0

Creating the Custom Objects

Before you can use the properties and methods of the dhtml_object, you have to create one of these custom objects for every <div> and <span> tag in your page. That's the job of the code in Listing 2.

Listing 2: Creating the Custom Objects

// This array holds all of the document's DHTML-able objects
var dhtml_objects = new Array()

// This function creates the custom objects 
// that serve as cross-browser front-ends
function create_object_array() {

  // All the <div> and <span> tags are stored in these variables
  var div_tags
  var span_tags
  var css_tags

  // Is the browser W3C DOM compliant?
  if (document.getElementById) {
  
    // If so, use getElementsByTagName() to get the <div> tags
    div_tags = document.getElementsByTagName("div")

    // Loop through the <div> tags
    for (var counter = 0; counter < div_tags.length; counter++) {

      // Store the current object
      current_object = div_tags[counter]
      
      // Store how the browser accesses styles
      object_css = current_object.style
      
      // Store the object's id
      object_id = current_object.id
      
      // Only store those tags that have an id
      if (object_id) {
      
        // Create a new dhtml_object and store it in dhtml_objects
        dhtml_objects[object_id] = new dhtml_object(current_object,
                              object_css, 
                              object_id)
      }
    }
    
    // Use getElementsByTagName() to get the <span> tags
    span_tags = document.getElementsByTagName("span")
    
    // Loop through the <span> tags
    for (var counter = 0; counter < span_tags.length; counter++) {
      
      // Store the current object
      current_object = span_tags[counter]
      
      // Store how the browser accesses styles
      object_css = current_object.style
      
      // Store the object's id
      object_id = current_object.id
      
      // Only store those tags that have an id
      if (object_id) {
      
        // Create a new dhtml_object and store it in dhtml_objects
        dhtml_objects[object_id] = new dhtml_object(current_object,
                              object_css, 
                              object_id)
      }
    }
  }
  
  // Is the browser DHTML DOM compliant?
  else if (document.all) {
    
    // If so, use document.all to get the <div> tags
    div_tags = document.all.tags("div")
    
    // Loop through the <div> tags
    for (var counter = 0; counter < div_tags.length; counter++) {
    
      // Store the current object
      current_object = div_tags[counter]
      
      // Store how the browser accesses styles
      object_css = current_object.style
      
      // Store the object's id
      object_id = current_object.id
      
      // Only store those tags that have an id
      if (object_id) {
      
        // Create a new dhtml_object and store it in dhtml_objects
        dhtml_objects[object_id] = new dhtml_object(current_object,
                              object_css, 
                              object_id)
      }
    }

    // Use document.all to get the <span> tags
    span_tags = document.all.tags("span")
    
    // Loop through the <span> tags
    for (var counter = 0; counter < span_tags.length; counter++) {
    
      // Store the current object
      current_object = span_tags[counter]
      
      // Store how the browser accesses styles
      object_css = current_object.style
      
      // Store the object's id
      object_id = current_object.id
      
      // Only store those tags that have an id
      if (object_id) {
      
        // Create a new dhtml_object and store it in dhtml_objects
        dhtml_objects[object_id] = new dhtml_object(current_object,
                              object_css, 
                              object_id)
      }
    }
  }
  
  // Is the browser LDOM compliant?
  else if (document.layers) {
  
    // Use document.layers to get the positioned <div> and <span> tags
    css_tags = document.layers

    // Loop through the layers
    for (var counter = 0; counter < css_tags.length; counter++) {
      
      // Store the current object
      current_object = css_tags[counter]
      
      // Store how the browser accesses styles
      object_css = current_object
      
      // Store the object's id
      object_id = current_object.id
      
      // Only store those tags that have an id
      if (object_id) {
      
        // Create a new dhtml_object and store it in dhtml_objects
        dhtml_objects[object_id] = new dhtml_object(current_object,
                              object_css, 
                              object_id)
      }
    }
  }
}

This script opens by declaring the global dhtml_objects array, which will hold the custom objects created for each <div> and <span> tag. The script then runs the create_object_array() function, which is used to add the custom objects to the dhtml_objects array.

The function begins by declaring the div_tags, span_tags, and css_tags variables, which are used to hold the collections of <div> and <span> elements in the page. Then the function performs some object detection, which in broad strokes looks like this:

if (document.getElementById) {
  Work with the elements using W3C DOM code
}
else if (document.all) {
  Work with the elements using DHTML DOM code
}
else if (document.layers) {
  Work with the elements using LDOM- code
}

For the W3C DOM branch, the function first uses the getElementsByTagName() method to return a collection of the document's <div> elements, which is stored in the div_tags variable. Then a for() loop runs through all the <div> elements and performs five tasks for each element:

  1. It stores the element itself in current_object.

  2. It stores how the element accesses styles in object_css.

  3. It stores the element's id in object_id.

  4. It checks to see if the element has an id value (we want to ignore those that don't have one).

  5. It calls the dhtml_object() constructor function to create a custom object for the element. Note that the current_object, object_css, and object_id variables are passed as the function's obj, css, and id arguments. The resulting object is stored in the global dhtml_objects array.

The function then repeats this process for the document's <span> tags.

For the DHTML DOM branch, the function does basically the same thing. The major difference is that it uses document.all.tags to return the <div> and <span> collections.

The LDOM branch is shorter because the function gets all the positioned <div> and <span> tags in one shot by using document.layers.

Using a Custom Object

It's important to note here that the index of the dhtml_objects array that's assigned to each custom object is not the usual numeric value starting at 0. Instead, the index is the object_id value, so that's how you refer to one of these objects in the array. For example, if a <div> element has an id equal to my_div, then the following expression refers to that element's custom object in the dhtml_objects array:

dhtml_objects["my_div"]

From here, you access the custom object's properties and methods just as you would any object. For example, the following expression returns the my_div custom object's id property:

dhtml_objects["my_div"].id

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020