Archive
HTML/CSS tips to reduce use of JavaScript
https://calendar.perfplanet.com/2020/html-and-css-techniques-to-reduce-your-javascript/
Great article with examples of uses of JacaScript and how they can be replaced with HTML or CSS. I had no idea these techniques were available
Pandoc – an essential tool for Markdown users
Pandoc is a great tool to convert between various text based formats. For instance, with a single input Markdown file, I can generate an HTML page of that document, a LaTeX document, and a beautifully typeset PDF.
I had troubles installing it on Mac OSX via MacPorts; a simpler solution for me was to download and install the Haskell package and then use the commands:
cabal update
cabal install pandoc
This assumes, of course, that the cabal
program that the Haskell package installs is accessible from your path.
The next step for me was to install the excellent Pandoc TextMate bundle. This gives you the standard things like syntax highlighting of your document, as well as a variety of useful snippets. For instance, when I am in Pandoc mode and press ⌃ ⌥ ⌘ P, I get the following popup from which I can easily choose options via mouse or keyboard:

Easy way to preview your document in various output formats
Before you can start using the Pandoc TextMate bundle, you must ensure that the Pandoc executable is on the PATH exposed to TextMate, which is different than your global system path. In other words, just because you can execute pandoc
in a shell and have it work, this doesn’t mean it will work in TextMate. For instance, on my computer, Pandoc is located in:
$ which pandoc
/Users/ndunn/Library/Haskell/bin/pandoc
Go to TextMate -> Preferences -> Advanced -> PATH and append :/Users/ndunn/Library/Haskell/bin
to the end of the PATH variable.

Appending the Pandoc path to the PATH variable
Pandoc makes a few extensions to the Markdown syntax, which I really like. For instance, you can designate a section of text to be interpreted literally by surrounding it with three ~
characters. Furthermore, you can specify what language the source code is in, and the Pandoc converter will syntax highlight it in the final document (assuming the correct extensions have been installed).
I like this setup because it allows you to specify the language of the block of text, which means that you can force TextMate to interpret it the same way. As I’ve blogged about previously, one can add source code syntax highlighting embedded in HTML documents. I added the following lines to my HTML language grammar in order to have a few different languages recognized and interpreted as source code within these delimited blocks.
Here is the relevant section:
{ name = 'source.java';
comment = 'Use Java grammar';
begin = '~~~\s*{.java}';
end = '~~~';
patterns = ( { include = 'source.java'; } );
},
{ name = 'text.xml';
comment = 'Use XML grammar';
begin = '~~~\s*{.xml}';
end = '~~~';
patterns = ( { include = 'text.xml'; } );
},
{ name = 'source.shell';
comment = 'Use Shell grammar';
begin = '~~~\s*{.shell}';
end = '~~~';
patterns = ( { include = 'source.shell'; } );
},
{ name = 'source';
begin = '~~~';
end = '~~~';
patterns = ( { include = 'source'; } );
},
(One tricky bit to get used to is that you need to have at least one blank space between surrounding text and a ~~~
delimited block, or else the ~
characters are interpreted as strikeouts through the text.)
Here is a screenshot of this working in TextMate:

Syntax highlighting of sourcecode within the Pandoc document
Finally, just to get really meta on you here’s a screenshot of the text of this document

Text version of the document
followed by a screenshot of the HTML that Pandoc produces:
followed by a screenshot of the PDF that LaTeX formatted via Pandoc:
I hope this has piqued your interest in Pandoc. I love the beautiful output of LaTeX but hate working with its syntax. With Pandoc I’m free to compose in Markdown, a language with a very lightweight syntax, and then convert into TeX when and if I want to.
JS 101 Week 5: Event handling
2011-02-23
Reflection
Why is it better to use either trickling or bubbling of events? Why not just give the event to the node on which it was invoked?
There are two main reasons to use the trickling down or bubbling up of events. The first is performance. If we have 100 different images on a page and we want each one to respond to the hover
event, it is very inefficient to create 100 different event listeners and attach them to each element. We can instead create a single hover event listener on the <div>
which contains all of these pictures, and from within that event handler, determine which of its enclosed <img>
elements was clicked. Additionally, when the event bubbles up the parent chain, the programmer is free to do interesting things like change the style of entire subtrees as a result of an event listener lower down in the hierarchy.
Can you think of one situation where you would want to prevent the browser from submitting a form after a user has clicked on the ‘submit’ button? How would you achieve this?
One example would be if we had some client side validation we wanted to take place before submitting a form to the server. For instance, we might want to validate that all the form fields are filled out, or that the values entered into these fields have a certain format (e.g. mm/dd/yyyy format for dates). When the form submit button is clicked and the validation routine takes place, if there is an error that function can call the preventDefault
method on the corresponding event object.
Homework
12.1 of Eloquent Javascript
Write a function asHTML which, when given a DOM node, produces a string representing the HTML text for that node and its children. You may ignore attributes, just show nodes as . The escapeHTML function from chapter 10 is available to properly escape the content of text nodes.
Hint: Recursion!
function isTextNode(node) { return node.nodeType == 3; } function asHTML(node) { // we’re done recursing if (node.childNodes.length === 0) { if (isTextNode(node)) { return node.nodeValue; // This is unavailable in the jsFiddle environment //return escapeHTML(node.nodeValue); } else { return "<" + node.nodeName + ">"; } } var returnString = "<" + node.nodeName + ">"; for (var i = 0; i < node.childNodes.length; i++) { returnString += asHTML(node.childNodes[i]) + "\n"; } return returnString; } alert(asHTML(document.body));
12.2 of Eloquent Javascript
Write the convenient function removeElement which removes the DOM node it is given as an argument from its parent node.
function removeElement(node) { node.parentNode.removeChild(node); }
13.1 of Eloquent Javascript
Write a function called registerEventHandler to wrap the incompatibilities of these two models. It takes three arguments: first a DOM node that the handler should be attached to, then the name of the event type, such as “click” or “keypress”, and finally the handler function.
To determine which method should be called, look for the methods themselves ― if the DOM node has a method called attachEvent, you may assume that this is the correct method. Note that this is much preferable to directly checking whether the browser is Internet Explorer. If a new browser arrives which uses Internet Explorer’s model, or Internet Explorer suddenly switches to the standard model, the code will still work. Both are rather unlikely, of course, but doing something in a smart way never hurts.
// eventname is the name of the event without the ‘on’ prefix. e.g. // to register for a click event, the eventname value should be "click" function registerEventHandler(node, eventname, handler) { // node has an attachEvent method; use that. This is how ie works if (node.attachEvent) { node.attachEvent("on" + eventname, handler); } // Mozilla model else if (node.addEventListener) { // false - use bubble up rather than trickle down node.addEventListener(eventname, handler, false); } else { node["on" + eventname] = handler; } }
JSFiddle example – when mouse enters element, it becomes bold. After it leaves, it becomes normal
Create an HTML page and some Javascript to allow a user to add n numbers.
First display a simple form with the question “How many numbers do you want to add (max is 10)”. The user should enter a number between 2 to 10 and click on a button in the form. You have to validate the answer. If the user has entered a correct value (between 2 and 10), then dynamically create a form with n text input fields and an “Add” button. Once the form is displayed the user will enter n numbers in the n input fields and when they click on the “Add” button, dynamically create a span element with the result. You will have to perform validation on the values entered in the input fields to make sure that they are numbers. If they are not numbers, display an alert dialogue with an error message.
JSFiddle solution
/*#p2pu-Jan2011-javascript101*/