|
|
Tech » JSframework helpers OO logging local strings multi class namesFramework
|
![]() |
This pattern allows to write debug messages to the browser window. The html page must contain an empty div-tag where the output should
appear: When the debugging is disabled, or the output panel is not defined,
a call to |
Here is the code:
var bDebug = true;
var divLogPanel = null;
function logMsg (msg) {
if ( !bDebug )
return;
if ( !divLogPanel ) {
divLogPanel = document.getElementById('idLogPanel');
if ( !divLogPanel ) {
bDebug = false;
return;
}
}
var dt = new Date();
divLogPanel .innerHTML += dt.getHours()+':'+dt.getMinutes()
+ ':'+dt.getSeconds()+'.'+dt.getMilliseconds()
+ ' ' + msg + '<br>';
}
Usage (see here for a demo):
// append a log message:
logMsg ('expand ('+this.title+')...');
This pattern allows to define multi-language strings and display them in the users preferred language (see here for a demo).
function localString (msg) {
/* language is FF only (refers to browser settings?)
* systemLanguage/userLanguage is IE only (refers OS lnaguage?)
*/
if ( typeof msg == 'string' )
return msg;
var lang = navigator.userLanguage ? navigator.userLanguage : navigator.language;
lang = lang.toLowerCase();
if ( lang.indexOf('-')>0 )
lang = lang.substring (0, lang.indexOf('-'));
if ( typeof msg[lang] == 'string' )
return msg[lang];
if ( typeof msg['en'] == 'string' )
return msg['en'];
return '<no local string found for language '+lang+'>';
}
Usage:
var CS_Solved = { en:'The Sudoku is solved.', de:'Das Sudoku wurde gelöst.' };
[...]
alert (localString(CS_Solved));
Assigning multiple class names to an html element is a standard CSS feature,
but seems to be rarely used.
An example could be a number of <input> elements, that have a common
class name to apply some CSS style:
<input class='boardCell'>.
The input element that has the input focus should be marked with a red
border. This can be done by a second additional class name separated by
a blank like this: <input class='boardCell focus'>.
To achieve this, we call addClassName() in the onfocus-event,
and remove it onblur (see here
for a demo):
<input type='text' class='boardCell'
onfocus='addClassName(this,"focus")'
onblur='removeClassName(this,"focus")' >
Here are the tool functions:
function addClassName (o, className) {
var a = o.className.split();
for (var i=0; <a.length;i++)
if ( a[i]==className )
return;
o.className += ' ' + className;
}
function removeClassName (o, className) {
o.className = o.className.replace (className, '');
}
And this could be the stylesheet:
input.boardCell
{
border-style:solid;
border-color:White;
text-align:center;
}
input.focus
{
border-style: inset;
border-color: red;
}