Anonymous
Domino 2.0 Rich Internet Applications with IBM Lotus Notes/Domino
You are here: Today » The base JavaScript object explained
« Bookmark web pages in a Notes database
Making a photo gallery: Domino, Semantic XHTML and Ajax »

The base JavaScript object explained

This document describes the JavaScript base object. All global functions and variables are stored in one single object: Jbase. This is common practice in JavaScript to avoid global namespace pollution and to make naming conflicts with other libraries less likely. The Jbase object is a Factory object: it groups functions and variables together. It is not intended as a prototype for other objects.

Jbase.init [function]

var Jbase={
    init:function(){var g=this,o;for(o in g)if(g[o].init)g[o].init()},
    .../...
}
.../...
window.onload=function(){Jbase.init()}

This function is called without parameters when the page is loaded. It goes over all its elements. If any element has an init function, it is executed. This allows the Jbase object to be extended with modules, each with their own init function, which is called automatically on page load. This way, different JavaScripts competing for the onload event can be avoided.

Jbase.get [function]

get:function(a,b){return(b||document).getElementById(a)}

Short for getElementByID.
Parameter a: the id [string] of the HTML element.
Parameter b (optional): the document [object]; the document object of the current page is taken by default.
Returns: The HTML element with that ID. If it does not exist, the function returns null.

Jbase.tags [function]

tags:function(a,b){return(b||document).getElementsByTagName(a)}

Short for getElementsByTagName.
Parameter a: the tag name [string].
Parameter b (optional): the HTML element in which to search; the document object of the current page is taken by default.
Returns: An array of HTML elements with that tag name. If none are found, the function returns null.

Jbase.set [function]

set:function(a,b){for(var o in b)a[o]=b[o];return a}

Allows to set multiple properties of an object with one single function call.
Parameter a: the object; can be a normal JavaScript Object, but also object.prototype, an HTML element or the style of an HTML element.
Parameter b: an object with the properties to set.
Returns: The modified object a.

Example 1: Extending String.prototype with 3 new functions

Jbase.set(String.prototype,{
    begins:function(a){return this.indexOf(a)==0},
    has:function(a){return this.indexOf(a)>-1},
    middle:function(a,b){var o=this.split(a);return(o.length<2)?'':o[1].split(b)[0]}
})

Example 2: Setting attributes and style of an HTML element

var o=Jbase.get('testDiv')
Jbase.set(o, {title:'this is the test div', onclick:function(){alert('clicked')}})
Jbase.set(o.style, {border:'solid 1px #000', background:'#ffc', padding:'5px'})

Jbase.middle [function]

middle:function(a,b,c){var o=a.split(b);return(o.length<2)?'':o[1].split(c)[0]}

Parameters a, b, c: strings.
Returns: the first string fragment in string a between string b and c. If not are found, the function returns an empty string.

Jbase.time [function]

time:function(){return new Date().getTime()}

Returns: the number of milliseconds since midnight of January 1, 1970. Used in time events, animations and debugging.

Star rating

0%

Comments

To add a comment, log in or register as new user. It's free and safe.