Archive for the 'JavaScript' Category

New version of JavaScript Environment

Monday, May 16th, 2005

JavaScript Development Environment 2.0 can run as a bookmarklet and lets you create Greasemonkey scripts in four clicks. I used it to make the Valid XHTML Greasemonkey script and I loved the shortened edit-test cycle.

Platypus and user scripts for Bonsai

Thursday, May 12th, 2005

Asa pointed me to a new Firefox extension called Platypus. Platypus lets you remove or isolate parts of a page a similar manner to Aardvark and lets you save sequences of page-modifying actions as Greasemonkey scripts. Platypus tries to identify elements by IDs in the scripts it generates and falls back on using XPath expressions such as /HTML[1]/BODY[1]/FORM[1].

I used Platypus on Bonsai CVS query form to remove useless parts of the page. I modified the script it generated to move Bonsai's menu to the bottom of the page instead of removing it from the page entirely. The resulting script is Bonsai Isolate Form. To use the script, you must have both Greasemonkey and Platypus installed.

Inspired, I made two other Greasemonkey scripts for the Bonsai form. Bonsai Date Option makes the "date" radio group selection change when you click a textbox associated with one of the radio buttons, and Bonsai Nightly Range makes the "date" section default to the range between the previous two nightly builds rather than the last two hours.

JavaScript Shell 1.2.1

Saturday, April 23rd, 2005

Entering a single-line comment on the last line no longer causes a syntax error. Updated shell, updated bookmarklet.

JavaScript Shell 1.2

Wednesday, April 20th, 2005

New features and bug fixes in JavaScript Shell 1.2:

  • Multiline input. You can now insert line breaks using Shift+Enter and paste multiline scripts. Edward Lee ("Mardak" on added this feature.
  • Fixed the bookmarklet version to work in Firefox 1.0.3 (it was broken due to the fix for bug 289074). Edward Lee was the first to point out this problem and helped me fix it.
  • Fixed print() to not bring up the File > Print dialog.
  • Fixed blink() to work in IE (using border instead of outline) and Opera.
  • Made the feature that lets you click anywhere to focus the input box work in Opera. It still doesn't work in IE.
  • Fixed multiline output in Opera. It still appears all on one line in IE.
  • Fixed the bookmarklet version to have a vertical scrollbar when needed on Firefox trunk. (I might be working around a bug in Firefox trunk.)

I'm aware of several new problems introduced by the multiline input feature:

  • In Opera, Enter inserts a line break and *then* submits.
  • In IE, multiline output isn't broken over lines.
  • In Firefox 1.0.x, but not on trunk, a scrollbar appears if you have a few dozen lines of input.

JavaScript Shell 1.1 is still available.

Winning an election with 22% of the popular vote

Monday, November 1st, 2004

A presidential candidate could be elected with as a little as 21.8% of the popular vote by getting just over 50% of the votes in DC and each of 39 small states. This is true even when everyone votes and there are only two candidates. In other words, a candidate could lose with 78.2% of the popular vote by getting just under 50% in small states and 100% in large states.

The optimal set of states to take (the one that lets a candidate win with the smallest popular vote) is not the N states with the smallest population. It's also not the N states with the smallest value for (population/electors), which would be optimal if you could get exactly 270 electoral votes that way.

The optimal solution happens to get exactly 270 electoral votes. In this solution, the winner takes DC, the 37 smallest states, the 39th smallest state, and the 40th smallest state. (The winner takes Alabama, Alaska, Arizona, Arkansas, Colorado, Connecticut, Delaware, DC, Hawaii, Idaho, Indiana, Iowa, Kansas, Kentucky, Louisiana, Maine, Maryland, Minnesota, Mississippi, Missouri, Montana, Nebraska, Nevada, New Hampshire, New Mexico, North Carolina, North Dakota, Oklahoma, Oregon, Rhode Island, South Carolina, South Dakota, Tennessee, Utah, Vermont, Virginia, Washington, West Virginia, Wisconsin, and Wyoming.)

Read on for my assumptions and algorithm.

Read the rest of this entry »

Simple JS learning environment

Friday, January 23rd, 2004

Leonard Lin is teaching animation students basic programming so they'll be able to use Maya's MELScript and Flash's ActionScript. He chose JavaScript as the first language for his students because JavaScript and ActionScript are both variants of ECMAScript.

I made a simple JS learning environment to cut out the save-switch-reload cycle and the "magic" HTML that surrounds a short JS program. If an error occurs, it highlights the line.

I reused a lot of code and UI ideas to make it. The overall UI comes from the Real-time HTML editor, the print() function comes from the JavaScript Shell, and the error-selecting idea and code come from the "blogidate XML well-formedness" bookmarklet. If you want to look at the code for the JS env, most of it is in the "buttons" frame.

JavaScript Shell 0.8

Tuesday, January 20th, 2004

I uploaded a new version of the JavaScript Shell today, along with a new version of the shell bookmarklet. In the new version, print() no longer returns its input. I added a new function, pr(), that acts like print() used to.

Mozilla 1.6b < Mozilla 1.0.1?

Monday, December 22nd, 2003

Warning, you are using Mozilla Version 1.6b.  The recommended browsers to view this page are Internet Explorer 4.0 or better, Netscape 4.0 or better, Mozilla 1.0.1 or better, or Opera 5.0 or better.

This USPS page uses the expression (browserName == "Mozilla" && browserVersion >= 1.0) to recognize acceptable versions of Mozilla. The string "1.6b" becomes NaN when coerced to a number, so the expression is false if you're using Mozilla 1.6b. If you're using Mozilla 1.5 instead of 1.6b, you won't see the warning. Ironically, "1.0.1", the minimum version they claim to support, coerces to NaN.

If the site had used parseFloat instead of implicit coercion, it wouldn't have hit this problem. parseFloat("1.6b") returns the number 1.6.