It's been a long time coming but I think I'm actually starting to get comfortable coding in Javscript. I'm working on a very complicated edit screen with lots of automated fields and behaviors so I'm getting a lot of practice. O'Reilly's Dynamic HTML book has been a great help. I generally find it more useful than their Javascript book. The coverage of cross-browser differences is better and the coverage is wider, covering all of CSS and HTML.
I've definitely made the transition from grudging Javascript hacker to coder: I'm building reusable tools. To whit, here's a very helpful logging utility that shows messages in a separate window refreshed on each hit:
var log_span; function log(msg) { if (!log_span) { var log_window = window.open("", "log", "status,width=700,height=300"); log_window.document.write('JS Log
'); log_window.document.close(); log_span = log_window.document.getElementById("log"); log_span.innerHTML = ""; } var d = new Date(); log_span.innerHTML += "[" + d.toLocaleString() + "] " + msg + "
"; }
Before I came up with that one I was using alert() for confessional debugging which is a complete pain.
I've also learned how to create my own classes in Javascript which allows for much cleaner code than my previous practice. Of course, Javascript doesn't really have classes, it uses prototype-based inheritence instead. It's weird by servicable once I got the syntax right.
Another big factor in my increasing Javascript confidence is the Emacs ecmascript-mode I found on the EmacsWiki. It looks and feels so much like cperl-mode that I can't help but feel at home.
-sam
Hey, looks familiar:
function logAdd(msg, which) {
var name = which != null ? which : "Log"
var l = document.getElementById(name);
var date = new Date();
var string = date + ": " + msg + "<br>" + l.innerHTML
l.innerHTML = string.substr(0, 4000)
}
It's also really useful for debugging long running flash movies (In our case from a couple of weeks, to a couple of years). The substr helps keep the ram usage down
Re:Looks familiar
jplindstrom on 2004-12-28T15:25:06
You have flash movies with an uptime > a year?
Re:Looks familiar
malte on 2004-12-28T15:43:07
Yes, we will use it for distributed "live" display of a lottery, that has draws every 3 minutes on terminals distributed throughout northern Germany. Sometimes the terminals might be turned off at night, but other terminals might run basically forever. We have zero leakage, so there is really to reason to ever restart the thing.
var log_window = window.open("", "log", "scrollbars=1,statusbar=0,resizable=1,width=700,height=300");
That way you can scroll and/or resize the log window as needed.
Re:Recommended change
samtregar on 2004-12-28T19:38:47
For some reason Firefox lets me resize it anyway... But I'm sure you're right.-sam
var _js_log = {
logwin: null,
append: function( elt, tagname ) {
var doc = this.logwin.document;
return elt.appendChild( doc.createElement( tagname ) );
},
appendtext: function( elt, tagname, text ) {
var doc = this.logwin.document;
return this.append( elt, tagname ).appendChild( doc.createTextNode( text ) );
},
msg: function( msg ) {
var first = ! this.logwin;
if( first ) this.logwin = window.open( '', 'log', 'status,width=700,height=300' );
var doc = this.logwin.document;
var body = doc.getElementsByTagName( 'body' )[0];
var autoscroll = body.scrollTop == ( body.scrollHeight - body.clientHeight );
if( first ) {
this.appendtext( body, 'h2', 'Javascript log' );
this.append( body, 'dl' ).id = 'log';
}
var log = doc.getElementById( 'log' );
this.appendtext( log, 'dt', ( new Date() ).toLocaleString() + ':' );
this.appendtext( log, 'dd', msg );
if( autoscroll ) body.scrollTop = body.scrollHeight;
},
};
Then you say _js_log.msg( 'Hello world' );
.