Neat little example of a useful JavaScript closure

September 11th, 2006

I just picked up the 5th edition of David Flanagan’s JavaScript, the Definitive Guide. I find it amusing that that book is the only JavaScript book officially recommended at comp.lang.javascript.

Closures are a powerful feature of JavaScript. However it’s often hard to explain in a few words, just what is useful about closures. Well, here on page 131 of the Rhino book, is the recipe for a unique ID generator that doesn’t require a global counter.

One thing that I personally enjoy about the Flanagan book is that he says things like “don’t pollute the global namespace.” :) This technique does not pollute the global namespace.

var uid = (
  function(){
    var id=0;
    return function(){
      return id++ ;
    };
  }
)();
//then just say:
alert(uid());

var uid = (function(){var id=0; return function(){return id++ ;};})();

The Simplest Example of Recursion in JavaScript

September 8th, 2006

Here is the simplest example of recursion that I can think of:

(function (arr, count) {
alert[count];
if(count

Why is this important?
Read the rest of this entry »

Why do HTML elements have different widths in MSIE than they do in Firefox?

August 31st, 2006

Why do HTML elements have different widths in MSIE than they do in Firefox? The answer is that IE uses a different box model than Firefox. What is the box model?

I looked in Wikipedia for the answer, and this is what I learned:

The box model allows block-level elements - such as paragraphs and block-quotations - to be drawn with padding, borders, and margins.

As defined by those CSS specifications, when a width is explicitly specified for any block-level element it should determine only the width of the content within the box, then the padding, borders and margins added afterwards. Internet Explorer (prior to version 6) instead includes the padding and borders within the specified width, resulting in a narrower box when displayed.

[PPK favors the IE box model, and says,] “Web designers who create boxes for holding content care about the *visible* width of the box, about the distance from border to border. The borders, and not the content, are the visual cues for the user of the site. Nobody is interested in the width of the content.”

[Use of the ie box model] can make division of interior space (as one might do for colums in a layout) more difficult using the IE box model.

…if there are two boxes, one inside the other, and the inner box describes its width as 50%, it will be calculated based on the width of the outer box that is reported by the current box model. Since the inner box must exist within the border and padding of the outer box, using the IE box model, the inner box’s 50% width will take up more than 50% of the content space available to it.

Internet Explorer box model bug - Wikipedia, the free encyclopedia

Hover Detail

June 11th, 2006

Tonight I added a hover detail to the Flickr thumbnails on the right. If you mouse over a thumbnail, you can see a large version of the photo. If you click on the large photo, its Flickr page loads in a new window.

I’m using the MTFlickrPhotos plugin to generate thumbnails, and to write the HTML for the large images into a hidden DIV. I’d like to load the large images asnchronously, but I need to work out how to request images. So far I’ve just used Ajax.Updater to preload HTML.

Oh yes, did I mention that the hover details are completely hosed in IE?

given a set of id3v1 tags, infer the values of blank ARTIST and TITLE fields with Perl

June 8th, 2006

In 2001, I wrote a Perl script to put id3v1 tags into mp3 files that didn’t have any tag information; using MP3::Info to get and set tag information.
Read the rest of this entry »

Explicitly Trigger A JavaScript Event Handler

June 7th, 2006


<script type="text/javascript">
function foo(){ alert("Hello World") }
</script>
<div id="bar" onclick="foo();">click me to see an alert</div>
<div onclick = "document.getElementById('bar').onclick()" > click me to do the same thing </div>

This is trivial, but the subject came up at work today; and I wound up making a little demo, which I’ll now post.

Explicitly Trigger An Event Handler in JavaScript

Set a nested DOM property with Javascript, using square bracket notation

June 6th, 2006

In the comp.lang.javascript Javascript Best Practices document, I came across an interesting discussion of the reasons for preferring square bracket notation to eval().

The consensus seems to be that eval is evil, and that “if it exists in your page, there is almost always a more correct way to accomplish what you are doing.”

Read the rest of this entry »

Position an element above the fold dynamically with JavaScript

June 5th, 2006

I put together some JavaScript to keep an element above the fold.

This was written at the last minute for the Barney’s New York launch; to spruce up the admin tool for their new web site. I only had time to test it in IE6 and Firefox 1.x, but I believe it works in Safari as well. Additionally, I do know that this bit of code has been in use internally at Barney’s for a few months now without any complaints coming back. So it may work well on older browsers too; although I believe there are compatibility issues with document.body.clientHeight in some browsers.

Read the rest of this entry »

Vic Sussman on Larry King Live (Transcript)

May 31st, 2006

I remember watching Vic on Larry King with the vice president. Al Gore is a huge dude, and Larry’s desk is small, so they were all crammed in right next to each other. Vic did what he could to bring up the difficult questions :)

I would love to find the video of this so I could post it on YouTube. This is just some of the transcript.

The transcript is mirrored after the jump.
Read the rest of this entry »

“Ride Recumbent Ride!” by Vic Sussman

May 30th, 2006

http://www.recumbents.com/vics.htm

It’s nice to see that WHIRL, the recumbent rider’s group that Vic wrote this piece to promote, is still going strong.

The full text of the article is mirrored below.
Read the rest of this entry »