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++ ;};})();
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.
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?
<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.
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.”
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.
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.