University of Melbourne home page
 

core.js

This file is included in all pages using the University templates.
It provides mechanisms for adding events to the page, managing the left-hand navigation on pages that include one and allowing default text in search boxes.

Functions

These functions are namespace protected in a variable uM and require dot notation to be called (examples of usage below).

uM.addLoadListener

Usage: uM.addLoadListener(function_name);

Parameters: function_name - the name of the function to execute once the page has loaded

Effect: Adds an event to the onload event handler. Is mindful of existing events.

Returns: nothing

Hat-tip: Cameron Adams

Processing:

  • check what sort of events are handled
  • add a listener/handler if that method is supported
  • otherwise add the function_name to the existing onload function

uM.attachEventListener

Usage: uM.attachEventListener(target, eventType, functionRef, capture);

Parameters:

  • target - element reference: the element to attach the listener to
  • eventType - string: the event trigger (eg: ‘click’, ‘mouseover’, etc.)
  • functionRef - function reference: function to attach
  • capture - boolean: Use capture or bubble mode (usually false)

Effect: Add the event to the named event listener (or handler)

Returns: true after completion of attachment

Hat-tip: Cameron Adams

Processing:

  • Check for listener/handler support
  • Attach the functionRef to the event

uM.detachEventListener

Usage: uM.detachEventListener(target, eventType, functionRef, capture);

Parameters:

  • target - element reference: the element to detach the listener from
  • eventType - string: the event trigger (eg: ‘click’, ‘mouseover’, etc.)
  • functionRef - function reference: function to attach
  • capture - boolean: Use capture or bubble mode (usually false)

Effect: Remove an existing event from the named event listener (or handler)

Returns: true after completion of detachment

Hat-tip: Cameron Adams

Processing:

  • Check for listener/handler support
  • Detach the functionRef from the event

Note: browsers that support neither the removeEventListener (W3C) nor the detachEvent (Microsoft) objects will remove all events from the specified handler

uM.schedule

Usage: uM.schedule(element_id, function_name);

Parameters:

  • element_id - call the function function_name when this element exists
  • function_name - the name of the function to execute once the page has loaded
  • (iteration) - used as a counter for recursive calls - not required

Effect: Adds an event to the onload event handler. Is mindful of existing events.

Returns: nothing

Hat-tip: Cameron Adams

Processing:

  • Check for the existence of the element
  • Run the event is the elements exists
  • Otherwise wait 50 milliseconds
  • Abort after 15 seconds

uM.addClass

Usage: uM.addClass(target, classValue);

Parameters:

  • target - the element to add the class to
  • classValue - the string value of the class to add

Effect: Adds a class to an element.

Returns: nothing

Hat-tip: Cameron Adams

Processing:

  • Check that the class isn’t already set
  • Add the class to the element

uM.removeClass

Usage: uM.removeClass(target, classValue);

Parameters:

  • target - the element to remove the class from
  • classValue - the string value of the class to remove

Effect: Removes a class from an element.

Returns: nothing

Hat-tip: Cameron Adams

Processing:

  • Replace the class (if it exists) in the element with nothing

uM.hasClass

Usage: uM.hasClass(target, classValue);

Parameters:

  • target - the element to remove the class from
  • classValue - the string value of the class to remove

Effect: Checks for the existence of a class in an element.

Returns: Boolean

Hat-tip: inspired by removeClass above

Processing:

  • returns true if the class is there, false otherwise

uM.getElementsByTagAndAttributes

Usage: uM.getElementsByTagAndAttributes(stem, leaf_tag, leaf_attributes);

Parameters:

  • stem - element reference: the element to search within
  • leaf_tag - string: nodeName of the tag to search for (eg: ‘div’)
  • leaf_attributes - associative array: attribute_name ⇒ attribute_value

Effect: finds elements within the stem that match all the listed attributes

Returns: array of elements

Hat-tip: Patrick Maslen

Processing:

  • refine the results from getElementsByTagName by checking for attribute/value pairs matching the list provided in leaf_attributes
  • return the sub-list

uM.plainTextChildren

Usage: uM.plainTextChildren(element);

Parameters: element - element reference: the element whose text you want

Effect: removes all html tags from within an element

Returns: string - plain text from within the element

Hat-tip:

Processing:

  • loop through all children of the element extracting the text nodes therein

uM.debug

Usage: uM.debug(text);

Parameters: text - the text you want to see

Effect: adds a paragraph to a debug div

Returns: nothing

Hat-tip:

Processing:

  • look for a debug div and append a paragraph containing the supplied text
 
templates/javascript/core.txt · Last modified: 2007/05/03 14:06 by aharris
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki