Warning ! this module is no longer supported
( Please read this before considering using it)

Description

The MJSLIB main module contains general purpose stuff which are used all along other modules of this collection. Part of this will probably be very useful for your coding too, hence this page of documentation.

Javascript runtime

Event Handlers

Dealing with events is a pain because of differences between browsers. The main module offer a mecanism which works everywhere the same way:

mjs_setEventCallback(evname,name,obj,code)

This function binds the given event 'evname' within the DOM element 'obj' to the function 'func'. The event name need to be expressed in the standardized way, not with the "on" prefix that is used in few browsers, and from the inline HTML event binding (ex: "click" is a good event name, not "onclick").

The second parameter is a name that you will have to choose and associate with this binding; it will be used to make sure the same event is not bound twice inadvertently.

Your callback function will receive an unique parameter: the element that triggered the event (a button for "click", a form for "submit", etc.). This is the element you passed to the registration function (parameter 'obj'). Your callback will have to return a boolean value. When returning false, the default handing of the event will be bypassed. This is something especially useful when dealing with "submit" event, when you want to prevent a form to be submitted for example.

Array Class

Few browsers, such as IE, implement the String class partially. The main module adds the methods "push", "pop" and "shift" when they are missing. A few non-strandard Array methods are also added:

list1.append(list2)

This methods add all list2 elements in list1.

list.remove(item)

This methods the removes the given item from the list.

Null, undefined => valued

mjs_valued(expr)

This methods returns true when the given expression is neither null or undefined. I used this all over the places because DOM and functions are not always consistent across browsers: some return null where others would return undefined. IE does not understand the "undefined" value natively, that's why I added a declaration in main: "undefined=void(0);".

New features

Logging (LOG* functions)

Logging messages from various places of your code is a key element for troubleshooting problems. You'll see detailed information in the debugging section of this collection.

sprintf collection

I did not believe that JavaScript lack the amazing sprintf() function. This is such a miss... Anyway, I reimplemented it and it should behaves more or less the same way than your libc favorite function. Also, there is this "alertf(fmt,p1,...)" function which is just an alert with formatted parameters. You probably gonna like it!

String utilities

strcmp(str1,str2) strcasecmp(str1,str2)

Just the replication of those libc functions. You may find the first useless since the "==" operator usually does what you want, but there is a javascript subtlety/trap here again ! "s1==s2" may give unexpected result depdending of s1/s2 types: if s1 is a string with the value "003" and s2 is an integer set to 3, s1==s2 will tells you "equal" where strcmp will tells you "different". Those function turn both parameters into strings before processing. Also, it makes the code somewhat easier to read because it makes evident that we are dealing with strings and not something else.

strlc=mjs_lc(str) struc=mjs_uc(str)

Aliases to str.toUpperCase() and str.toLowerCase() respectively. This is just because I got use to those things when coding with perl !